Power Query to Concatenate Rows Based on Condition

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have column with Names, (unique) ID, and Address. The problem is some addresses have their street address in one row and the row below it is their city, state and zip. I would like to have their street address, city, state, and zip in concatenated in on line based on their ID. For example:


Name...….....ID...……...Address

Joe Smith....123...……..25 Apple Dr.

Joe Smith.....123...…...New York City, NY, 00972

Sue Brown....445...…...90 Bayview St. Miami, FL 32423

Tim Rice...….565...…..55 Pachino St.

Tim Rice...…..565...….Rockville, MD 64310



ExpectedResult:

Joe Smith...….123......25 Apple Dr. New York City, NY, 00972
Sue Brown....445...…...90 Bayview St. Miami, FL 32423
Tim Rice...….565...…..55 Pachino St. Rockville, MD 64310


How can I transform my data to show like this?
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Use Group with All Rows then List.Distinct and Extract with Space delimiter

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]Name[/td][td=bgcolor:#5B9BD5]ID[/td][td=bgcolor:#5B9BD5]Address[/td][td][/td][td=bgcolor:#70AD47]Name[/td][td=bgcolor:#70AD47]ID[/td][td=bgcolor:#70AD47]Address[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Joe Smith[/td][td=bgcolor:#DDEBF7]
123​
[/td][td=bgcolor:#DDEBF7]25 Apple Dr[/td][td][/td][td=bgcolor:#E2EFDA]Joe Smith[/td][td=bgcolor:#E2EFDA]123[/td][td=bgcolor:#E2EFDA]25 Apple Dr New York City, NY, 00972[/td][/tr]

[tr=bgcolor:#FFFFFF][td]Joe Smith[/td][td]
123​
[/td][td]New York City, NY, 00972[/td][td][/td][td]Sue Brown[/td][td]445[/td][td]90 Bayview St. Miami, FL 32423[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Sue Brown[/td][td=bgcolor:#DDEBF7]
445​
[/td][td=bgcolor:#DDEBF7]90 Bayview St. Miami, FL 32423[/td][td][/td][td=bgcolor:#E2EFDA]Tim Rice[/td][td=bgcolor:#E2EFDA]565[/td][td=bgcolor:#E2EFDA]55 Pachino St Rockville, MD 64310[/td][/tr]

[tr=bgcolor:#FFFFFF][td]Tim Rice[/td][td]
565​
[/td][td]55 Pachino St[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Tim Rice[/td][td=bgcolor:#DDEBF7]
565​
[/td][td=bgcolor:#DDEBF7]Rockville, MD 64310[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]
 
Last edited:
Upvote 0
So I did Group By on the ID field > All Rows, but I don't know how to apply the List.Distinct? Should I expand the new column once I did Group By? Where do I use List.Distinct? In a Custom Column?
 
Upvote 0
AS an alternative approach use Text.Combine:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="InputTable"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ID", Int64.Type}, {"Address", type text}}),
    Result=Table.Group(#"Changed Type", {"Name", "ID"}, {{"Count", each Text.Combine([Address],", "), type text}})
in
    Result

Peter
 
Last edited:
Upvote 0
After Group you should see Table column, then use
for ID: ID = Table.AddColumn(Group, "ID", each List.Distinct(Table.Column([Count],"ID"))),
and : ExtractID = Table.TransformColumns(ID, {"ID", each Text.Combine(List.Transform(_, Text.From)), type text}),

the same for Address
 
Upvote 0
AS an alternative approach use Text.Combine:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="InputTable"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ID", Int64.Type}, {"Address", type text}}),
    Result=Table.Group(#"Changed Type", {"Name", "ID"}, {{"Count", each Text.Combine([Address],", "), type text}})
in
    Result

Peter

Thank you both. I used your method as it was easy for me to understand and change the M code. Small issue, some address lines show like this in a cell:

123 Moonlight St.
Columbus, OH 57423

How can I move/display it in one line for the cell/field? Do I need to substitute hard space/line feed with regular space?
 
Last edited:
Upvote 0
Not sure what NVM means but I'm glad it worked and also you understood what the code was doing and helps you on your Power Query learning adventure!

Peter
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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