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

Merging queries based on a specific column

Hello Everyone,

 

Can someone please help me with the below scenario. I have a table which contains two columns like below.

 

 

House        Name

1ABC
1DEF
1GHI
2JKL
2MNO
3PQR
4STU
4VWX

 

Now what I'm trying to achieve is:

 

House         Name

1ABC, DEF, GHI
2JKL, MNO
3PQR
4STU, VWX
2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @Anonymous

 

You can create a new calculated table as follows, where Table1 is the upper table you show:

 

NewTable =
ADDCOLUMNS (
    DISTINCT ( Table1[House] ),
    "Name", CALCULATE ( CONCATENATEX ( DISTINCT ( Table1[Name] ), Table1[Name], ", " ) )
)

View solution in original post

Greg_Deckler
Super User
Super User

Here is a Power Query way to do it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0claK1YGwXVzd4Gx3D08w2wjI9vL2gbN9/fzBbGMgOyAwCMw2AbKDQ0Lh7LDwCKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [House = _t, Name = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"House", Int64.Type}, {"Name", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"House"}, {{"Name", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.ToList(Table.SelectColumns([Name],"Name"))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Name"})
in
    #"Removed Columns"

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

Here is a Power Query way to do it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0claK1YGwXVzd4Gx3D08w2wjI9vL2gbN9/fzBbGMgOyAwCMw2AbKDQ0Lh7LDwCKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [House = _t, Name = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"House", Int64.Type}, {"Name", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"House"}, {{"Name", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.ToList(Table.SelectColumns([Name],"Name"))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Name"})
in
    #"Removed Columns"

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thank you both @AlB @Greg_Deckler .. It worked quite well.

AlB
Super User
Super User

Hi @Anonymous

 

You can create a new calculated table as follows, where Table1 is the upper table you show:

 

NewTable =
ADDCOLUMNS (
    DISTINCT ( Table1[House] ),
    "Name", CALCULATE ( CONCATENATEX ( DISTINCT ( Table1[Name] ), Table1[Name], ", " ) )
)

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.