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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
newpbiuser01
Helper V
Helper V

Combining values in multiple rows based on the Category

Hello,

 

I am trying to combine/concatenate values from different rows as long as they below to the same category. What I mean by that is, if we have a table with a list of shoppers, categories (fruit/vegetable) and the corresponding stuff they bought, I'd like to combine the data so that we get a list of the shoppers split by the category, where the type of fruits are combined in one row in a single column, and the colors are combined in a single row in the same column. 

 

So, if the original table is as follows:

ShopperCategoryTypeColor
BobFruitAppleRed
BobVegetableTomatoRed
BobVegetableSpinachGreen
AndyFruitCherryRed
AndyFruitGrapeGreen
JoeFruitOrangeOrange
JoeFruitAppleRed
JoeFruitBananaYellow
JoeVegetableCeleryGreen
SusanVegetablePotatoYellow
SusanVegetableCucumberGreen

 

The expected result would be:

ShopperCategoryType

Color

Bob

FruitAppleRed
BobVegetableTomato|SpinachRed|Green
AndyFruitCherry|GrapeRed|Green
JoeFruitOrange|Apple|BananaOrange|Red|Yellow
JoeVegetableCeleryGreen
SusanVegetablePotato|CucumberYellow|Green

 

Would anyone know how I could go ahead and do that? Any help would be greatly appreciated. 

Thank you! 

1 ACCEPTED SOLUTION
tackytechtom
Super User
Super User

Hi @newpbiuser01 ,

 

How about this:

tomfox_0-1657911522463.png

 

 

Here the M code to paste into the advanced editor:

tomfox_1-1657911660049.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZDNCoMwEITfJWdfQgMVemmppVDEw6qDCjEJ24Ti2zdYijEtZQ87MB+zP3UtCtOKTBzYTy703FqF0C/oRZN93BsGOGpX52pmcuYvUtlJUzcGVTKgVyrX/RLNkSOYlygl8Usmi13A0SDyT0x6wCa+ifSSvVuQDhXEHUqZZ0TEh0gorFtua1T+QTrBzsa9XxKF/eKk7/zcgrfA5gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shopper = _t, Category = _t, Type = _t, Color = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Shopper", type text}, {"Category", type text}, {"Type", type text}, {"Color", type text}}),
    #"Grouped Rows1" = Table.Group(#"Changed Type", {"Shopper", "Category"}, {"Type", each Text.Combine([Type], "|"), type text}),
    #"Grouped Rows2" = Table.Group(#"Changed Type", {"Shopper", "Category"}, {"Color", each Text.Combine([Color], "|"), type text}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows1", {"Shopper", "Category"}, #"Grouped Rows2", {"Shopper", "Category"}, "Grouped Rows2", JoinKind.LeftOuter),
    #"Expanded Grouped Rows2" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows2", {"Color"}, {"Grouped Rows2.Color"})

in
    #"Expanded Grouped Rows2"

Let me know if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

1 REPLY 1
tackytechtom
Super User
Super User

Hi @newpbiuser01 ,

 

How about this:

tomfox_0-1657911522463.png

 

 

Here the M code to paste into the advanced editor:

tomfox_1-1657911660049.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZDNCoMwEITfJWdfQgMVemmppVDEw6qDCjEJ24Ti2zdYijEtZQ87MB+zP3UtCtOKTBzYTy703FqF0C/oRZN93BsGOGpX52pmcuYvUtlJUzcGVTKgVyrX/RLNkSOYlygl8Usmi13A0SDyT0x6wCa+ifSSvVuQDhXEHUqZZ0TEh0gorFtua1T+QTrBzsa9XxKF/eKk7/zcgrfA5gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shopper = _t, Category = _t, Type = _t, Color = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Shopper", type text}, {"Category", type text}, {"Type", type text}, {"Color", type text}}),
    #"Grouped Rows1" = Table.Group(#"Changed Type", {"Shopper", "Category"}, {"Type", each Text.Combine([Type], "|"), type text}),
    #"Grouped Rows2" = Table.Group(#"Changed Type", {"Shopper", "Category"}, {"Color", each Text.Combine([Color], "|"), type text}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows1", {"Shopper", "Category"}, #"Grouped Rows2", {"Shopper", "Category"}, "Grouped Rows2", JoinKind.LeftOuter),
    #"Expanded Grouped Rows2" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows2", {"Color"}, {"Grouped Rows2.Color"})

in
    #"Expanded Grouped Rows2"

Let me know if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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