Power Query to Concatenate Rows Based on Condition

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,069
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:

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
3,822
Use Group with All Rows then List.Distinct and Extract with Space delimiter

NameIDAddressNameIDAddress
Joe Smith
123​
25 Apple DrJoe Smith12325 Apple Dr New York City, NY, 00972
Joe Smith
123​
New York City, NY, 00972Sue Brown44590 Bayview St. Miami, FL 32423
Sue Brown
445​
90 Bayview St. Miami, FL 32423Tim Rice56555 Pachino St Rockville, MD 64310
Tim Rice
565​
55 Pachino St
Tim Rice
565​
Rockville, MD 64310
 
Last edited:

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,069
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?
 

peter789

Board Regular
Joined
Nov 20, 2016
Messages
112
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:

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
3,822
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
 

peter789

Board Regular
Joined
Nov 20, 2016
Messages
112
Sorry Sandy666
for crossing my post with yours. I hope I haven't confused the issue.

Peter
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
3,822
no prob...
 

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,069
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:

peter789

Board Regular
Joined
Nov 20, 2016
Messages
112
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
 

Forum statistics

Threads
1,082,305
Messages
5,364,406
Members
400,801
Latest member
julievandermeulen

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top