Power Query - Combine values in rows into one single cell CSV

Aldonin

New Member
Joined
Jan 27, 2012
Messages
28
Hi there Gurus.

I have a large data sate in the form below where same login appears multiple time, one for each different location.


LOGINLOCATIONS
PETERLoc1
JOHNLoc1
PETERLoc3
CHARLESLoc2
CHARLESLoc7
JOHNLoc5
PETERLoc9

I need to transform it into something that only has the login once, and aggregates all locations using comma as delimiter, (using power query)
LOGINLOCATIONS
PETERLoc1,Loc3,Loc9
JOHNLoc1,Loc5
CHARLESLoc2,Loc7

I tried merging columns and grouping without success as the login gets repeated after every LocX code.

Any help is much welcome, and... thank you!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"LOGIN"}, {{"Count", each _, type table [LOCATIONS]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [Count][LOCATIONS]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Accumulate([Custom], "", (state, current) => state & current & "," )),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each Text.Middle([Custom.1],0,Text.Length([Custom.1])-1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Count", "Custom", "Custom.1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "LOCATIONS"}})
in
    #"Renamed Columns"


Book4
ABCDE
1LOGINLOCATIONSLOGINLOCATIONS
2PETERLoc1PETERLoc1,Loc3,Loc9
3JOHNLoc1JOHNLoc1,Loc5
4PETERLoc3CHARLESLoc2,Loc7
5CHARLESLoc2
6CHARLESLoc7
7JOHNLoc5
8PETERLoc9
Sheet1
 
Upvote 0
Just pointing out that the List.Accumulate step is unnecessary... once you have the list, you can extract it into values:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"LOGIN"}, {{"Count", each _, type table [LOCATIONS]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [Count][LOCATIONS]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Removed Other Columns" = Table.SelectColumns(#"Extracted Values",{"LOGIN", "Custom"})
in
    #"Removed Other Columns"

Though if you want to get really tricky, you can combine the List & Extract into the Grouping:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"LOGIN"}, {{"Locations", each Text.Combine(List.Transform(_[LOCATIONS], Text.From), ",")}})
in
    #"Grouped Rows"
 
Upvote 0
Just pointing out that the List.Accumulate step is unnecessary... once you have the list, you can extract it into values:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"LOGIN"}, {{"Count", each _, type table [LOCATIONS]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [Count][LOCATIONS]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Removed Other Columns" = Table.SelectColumns(#"Extracted Values",{"LOGIN", "Custom"})
in
    #"Removed Other Columns"

Though if you want to get really tricky, you can combine the List & Extract into the Grouping:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"LOGIN"}, {{"Locations", each Text.Combine(List.Transform(_[LOCATIONS], Text.From), ",")}})
in
    #"Grouped Rows"
Thanks - that's a much better way (especially your second one)
 
Upvote 0
or almost the same, without the List.Transform.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"LOGIN"}, {{"Locations", each Text.Combine([LOCATIONS],","), type nullable text}})
in
    Group
 
Upvote 0
Yup - I got a little ahead of myself when I combined the steps ;). Nice catch!
 
Upvote 0
If it is a large table, maybe sort it, add index, then use the Fourth parameter in the grouping Group.KindLocal to speed things up. I did not test, but it could be beneficial.
 
Upvote 0

Forum statistics

Threads
1,223,701
Messages
6,173,920
Members
452,539
Latest member
deeme

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