Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Group ligns together and concatenate them in a specific order (Power Query)

Hi everyone,

 

I am having some trouble resolving this with power Query :

 

I have a list of customers, with name of cities where they went and with the order in which they went in each city (all customers don't go to the same cities and the order in which they go between cities is not always the same).

 

What I need Is a table where I could see the customer ID and a concatened column where I could see where the customers went in the right order.

 

For instance, this is what i have : 

 

Customer IdCityOrder
1Los Angeles1
1New York3
1Paris2
2Los Angeles1
2New York2
2Miami3
2Londres4
3Toronto1
3Vancouver2
3Mexico3
3New York4
4Paris1
4Toronto2

 

And this is what I need :

 

Customer IdCities
1Los Angeles - Paris - New York
2Los Angeles - New York - Miami - Londres
3Toronto - Vancouver - Mexico
4Paris-Toronto

 

Thank you !

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLJL1ZwzEtPzUktBvIMlWJ1IOJ+qeUKkflF2UCmMVwwILEoE6TMCCxihEO7Eap2hGLfzMTcTLiBEO15KUVgrSZgMWMgKyS/KD+vJB9uHEgsLDEvOb+0LLUIbh5I1De1IjM5H26gMaq9EBNNkJxtCBdB2AE0LRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Id" = _t, City = _t, Order = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Id", Int64.Type}, {"City", type text}, {"Order", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer Id", Order.Ascending}, {"Order", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Customer Id"}, {{"Rows", each _, type table [Customer Id=nullable number, City=nullable text, Order=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Text.Combine([Rows][City]," - ")),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Customer Id", "Custom"})
in
    #"Removed Other Columns"

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLJL1ZwzEtPzUktBvIMlWJ1IOJ+qeUKkflF2UCmMVwwILEoE6TMCCxihEO7Eap2hGLfzMTcTLiBEO15KUVgrSZgMWMgKyS/KD+vJB9uHEgsLDEvOb+0LLUIbh5I1De1IjM5H26gMaq9EBNNkJxtCBdB2AE0LRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Id" = _t, City = _t, Order = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Id", Int64.Type}, {"City", type text}, {"Order", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer Id", Order.Ascending}, {"Order", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Customer Id"}, {{"Rows", each _, type table [Customer Id=nullable number, City=nullable text, Order=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Text.Combine([Rows][City]," - ")),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Customer Id", "Custom"})
in
    #"Removed Other Columns"

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.