Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Power Query to Concatenate Rows Based on Condition
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jun 2014
    Posts
    1,059
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Power Query to Concatenate Rows Based on Condition

    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 by legalhustler; Oct 15th, 2019 at 01:06 PM.

  2. #2
    Board Regular sandy666's Avatar
    Join Date
    Oct 2015
    Posts
    3,023
    Post Thanks / Like
    Mentioned
    33 Post(s)
    Tagged
    1 Thread(s)

    Cool Re: Power Query to Concatenate Rows Based on Condition

    Use Group with All Rows then List.Distinct and Extract with Space delimiter

    Name ID Address Name ID Address
    Joe Smith
    123
    25 Apple Dr Joe Smith 123 25 Apple Dr New York City, NY, 00972
    Joe Smith
    123
    New York City, NY, 00972 Sue Brown 445 90 Bayview St. Miami, FL 32423
    Sue Brown
    445
    90 Bayview St. Miami, FL 32423 Tim Rice 565 55 Pachino St Rockville, MD 64310
    Tim Rice
    565
    55 Pachino St
    Tim Rice
    565
    Rockville, MD 64310
    Last edited by sandy666; Oct 15th, 2019 at 01:45 PM.
    I know you know but I forgot my Crystal Ball and don't know what you know



    In the first post, show the type of machine (PC / Mac) and the Office version you are working on
    I don't use vba in any form!

  3. #3
    Board Regular
    Join Date
    Jun 2014
    Posts
    1,059
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    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?

  4. #4
    Board Regular
    Join Date
    Nov 2016
    Posts
    109
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    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 by peter789; Oct 15th, 2019 at 02:16 PM.

  5. #5
    Board Regular sandy666's Avatar
    Join Date
    Oct 2015
    Posts
    3,023
    Post Thanks / Like
    Mentioned
    33 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    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
    I know you know but I forgot my Crystal Ball and don't know what you know



    In the first post, show the type of machine (PC / Mac) and the Office version you are working on
    I don't use vba in any form!

  6. #6
    Board Regular
    Join Date
    Nov 2016
    Posts
    109
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    Sorry Sandy666
    for crossing my post with yours. I hope I haven't confused the issue.

    Peter

  7. #7
    Board Regular sandy666's Avatar
    Join Date
    Oct 2015
    Posts
    3,023
    Post Thanks / Like
    Mentioned
    33 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    no prob...
    I know you know but I forgot my Crystal Ball and don't know what you know



    In the first post, show the type of machine (PC / Mac) and the Office version you are working on
    I don't use vba in any form!

  8. #8
    Board Regular
    Join Date
    Jun 2014
    Posts
    1,059
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    Quote Originally Posted by peter789 View Post
    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 by legalhustler; Oct 15th, 2019 at 03:00 PM.

  9. #9
    Board Regular
    Join Date
    Jun 2014
    Posts
    1,059
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    NVM, I was able to apply CLEAN and it worked. TRIM did not work.

  10. #10
    Board Regular
    Join Date
    Nov 2016
    Posts
    109
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Power Query to Concatenate Rows Based on Condition

    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

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •