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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Concatenate duplicate values into one cell for a given ID using Power Query?

I have an excel table imported into Power Query shown below. 

 

Vendor_IDVendor_NameContact_FNContact_LNEmail
1234company 1JOHN                                    Smith                                                       johnsmith@company.com
1235company 2JANE                                    Smith                                                       janesmith@company.com
1236company 3JoanSmith                                                       joansmith@company.com
1236company 1JOHN                                    Smith                                                       johnsmith@company.com

 

My objective is to find contacts who have more than one Vendor_ID, and then, concatenate them into one cell like so:

 

 

Vendor_IDVendor_NameContact_FNContact_LNEmailConcatenate
1234company 1JOHN                                    Smith                                                       johnsmith@company.com   1234, 1236
1235company 2JANE                                    Smith                                                       janesmith@company.com   1235
1236company 3JoanSmith                                                       joansmith@company.com   1236
1236company 1JOHN                                    Smith                                                       johnsmith@company.com  1234, 1236

 

How can I achieve this using M, or Power Query only? 

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Anonymous 

 

 

Text.Combine(
      List.Transform(
        List.Distinct(
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
        ), 
        each Number.ToText(_)
      ), 
      ","
    )

 

 

if you don't want distinct

Text.Combine(
      List.Transform(
        /*List.Distinct(*/
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
       /* )*/, 
        each Number.ToText(_)
      ), 
      ","
    )

 

let
  Source = Web.BrowserContents(
    "https://community.powerbi.com/t5/Power-Query/Concatenate-duplicate-values-into-one-cell-for-a-given-ID-using/m-p/2277750#M67528"
  ), 
  #"Extracted Table From Html" = Html.Table(
    Source, 
    {
      {"Column1", "TABLE:nth-child(3) > * > TR > :nth-child(1)"}, 
      {"Column2", "TABLE:nth-child(3) > * > TR > :nth-child(2)"}, 
      {"Column3", "TABLE:nth-child(3) > * > TR > :nth-child(3)"}, 
      {"Column4", "TABLE:nth-child(3) > * > TR > :nth-child(4)"}, 
      {"Column5", "TABLE:nth-child(3) > * > TR > :nth-child(5)"}
    }, 
    [RowSelector = "TABLE:nth-child(3) > * > TR"]
  ), 
  #"Promoted Headers" = Table.PromoteHeaders(
    #"Extracted Table From Html", 
    [PromoteAllScalars = true]
  ), 
  #"Changed Type" = Table.TransformColumnTypes(
    #"Promoted Headers", 
    {
      {"Vendor_ID", Int64.Type}, 
      {"Vendor_Name", type text}, 
      {"Contact_FN", type text}, 
      {"Contact_LN", type text}, 
      {"Email", type text}
    }
  ), 
  #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Combine(
      List.Transform(
        /*List.Distinct(*/
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
       /* )*/, 
        each Number.ToText(_)
      ), 
      ","
    ))
in
  #"Added Custom"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
smpa01
Super User
Super User

@Anonymous 

 

 

Text.Combine(
      List.Transform(
        List.Distinct(
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
        ), 
        each Number.ToText(_)
      ), 
      ","
    )

 

 

if you don't want distinct

Text.Combine(
      List.Transform(
        /*List.Distinct(*/
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
       /* )*/, 
        each Number.ToText(_)
      ), 
      ","
    )

 

let
  Source = Web.BrowserContents(
    "https://community.powerbi.com/t5/Power-Query/Concatenate-duplicate-values-into-one-cell-for-a-given-ID-using/m-p/2277750#M67528"
  ), 
  #"Extracted Table From Html" = Html.Table(
    Source, 
    {
      {"Column1", "TABLE:nth-child(3) > * > TR > :nth-child(1)"}, 
      {"Column2", "TABLE:nth-child(3) > * > TR > :nth-child(2)"}, 
      {"Column3", "TABLE:nth-child(3) > * > TR > :nth-child(3)"}, 
      {"Column4", "TABLE:nth-child(3) > * > TR > :nth-child(4)"}, 
      {"Column5", "TABLE:nth-child(3) > * > TR > :nth-child(5)"}
    }, 
    [RowSelector = "TABLE:nth-child(3) > * > TR"]
  ), 
  #"Promoted Headers" = Table.PromoteHeaders(
    #"Extracted Table From Html", 
    [PromoteAllScalars = true]
  ), 
  #"Changed Type" = Table.TransformColumnTypes(
    #"Promoted Headers", 
    {
      {"Vendor_ID", Int64.Type}, 
      {"Vendor_Name", type text}, 
      {"Contact_FN", type text}, 
      {"Contact_LN", type text}, 
      {"Email", type text}
    }
  ), 
  #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Combine(
      List.Transform(
        /*List.Distinct(*/
          Table.SelectRows(#"Changed Type", (q) => q[Vendor_Name] = [Vendor_Name])[Vendor_ID]
       /* )*/, 
        each Number.ToText(_)
      ), 
      ","
    ))
in
  #"Added Custom"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Is there a way to use Email instead of the vendor name? The email addresses are unique, the vendor names unfortunately are not and have tons of repeat values.

 

Also, if there is an email address that has duplicate in two rows, with different vendor ID, what will the effect be? 

@Anonymous  change vendor_name with Email in the code

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
JM_BEI
Frequent Visitor

Hi, I tried adapting this solution to my own problem but got the folowing error:

 

Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

 

is there something I'm missing?

 

 

    #"Concatenate" = Text.Combine(
      List.Transform(
        List.Distinct(
          Table.SelectRows(#"Removed Columns", (q) => q[RepeatedValue] = [RepeatedValue])[TextToCombine]
        ), 
        each Number.ToText(_)
      ), 
      " / "
    )

 

 

JM_BEI
Frequent Visitor

Got the solution.

 

I was missing the Table.AddColumn code.

    #"Concatenate" = Table.AddColumn(#"Filtered Rows1", "New Column", each Text.Combine(
      List.Transform(
        List.Distinct(
          Table.SelectRows(#"Filtered Rows1", (q) => q[RepeatedValue] = [RepeatedValue])[TextToCombine]), 
        each Number.ToText(_)
        ), 
        " / "
    ))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors