Merging rows

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a sample of a worksheet in its current state and just underneath it is the desired output, I need to merge these records and I don't mind if the Results (ie Math 50, Sci 45) are merged into one cell if that provides a solution.

IDNameSubjectsTotal marksResult 1Result 2Result 3Result 4
1Tom4174Biol 42Math 43Sci 45Eng 44
2Mary155Sci 55
2Mary2120Math 60Sci 60
IDNameSubjectsTotal marksResult 1Result 2Result 3Result 4
1Tom4174Biol 42Math 43Sci 45Eng 44
2Mary3175Sci 55Math 60Sci 60

I have tried Power Query but can't quite get there so am looking for anything that gets the job done.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
maybe
IDNameSubjectsTotal marksResult 1Result 2Result 3Result 4IDNameSubjectsTotal marksResult
1Tom4174Biol 42Math 43Sci 45Eng 441Tom4174Biol 42,Math 43,Sci 45,Eng 44
2Mary155Sci 552Mary3175Sci 55,Math 60,Sci 60
2Mary2120Math 60Sci 60

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Mrg = Table.CombineColumns(Source,{"Result 1", "Result 2", "Result 3", "Result 4"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    Trim = Table.AddColumn(Mrg, "Result", each Text.TrimEnd([Merged],",")),
    Grp = Table.Group(Trim, {"ID", "Name"}, {{"Subjects", each List.Sum([Subjects]), type number}, {"Total marks", each List.Sum([Total marks]), type number}, {"All", each _, type table}}),
    List = Table.AddColumn(Grp, "Result", each [All][Result]),
    Extract = Table.TransformColumns(List, {"Result", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    Extract
 
Upvote 0
Thanks Sandy for your solution.

I often receive the data in the following format whereby I need to link the 2 tables, output will be the same as per previous post. Could you (or anyone who knows how) show me how you'd connect all this data?

IDNameAge
1Tom18
2Mary20
IDSubjectsTotal marksResult 1Result 2Result 3Result 4
14174Biol 42Math 43Sci 45Eng 44
2155Sci 55
22120Math 60Sci 60
 
Upvote 0
like this?
IDNameAgeSubjectsTotal marksList
1Tom184174Biol 42,Math 43,Sci 45,Eng 44
2Mary203175Sci 55,Math 60,Sci 60

Rich (BB code):
let
    Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Mrg = Table.CombineColumns(Source2,{"Result 1", "Result 2", "Result 3", "Result 4"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    Trim = Table.AddColumn(Mrg, "Result", each Text.TrimEnd([Merged], ",")),
    Group = Table.Group(Trim, {"ID"}, {{"Subjects", each List.Sum([Subjects]), type number}, {"Total marks", each List.Sum([Total marks]), type number}, {"All", each _, type table}}),
    List = Table.AddColumn(Group, "List", each [All][Result]),
    Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Join = Table.NestedJoin(Source1,{"ID"},Extract,{"ID"},"Table",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Join, "Table", {"Subjects", "Total marks", "List"}, {"Subjects", "Total marks", "List"})
in
    Expand
 
Upvote 0
Thanks Sandy, can you please explain how you got
Join = Table.NestedJoin(Source1,{"ID"},Extract,{"ID"},"Table",JoinKind.LeftOuter)
was it through a PQ menu option, if so I couldn't find it or is it part of the M language?
 
Upvote 0

Forum statistics

Threads
1,215,265
Messages
6,123,961
Members
449,137
Latest member
yeti1016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top