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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
WouterV
Frequent Visitor

Double concatenate(x) with group by

Dear Community,

 

I have a report with a dataset in which I'm trying to group values (by concatenating) on 2 levels.

Imagine below dummy dataset and keep in mind that ColX would be in reality around 20 columns in the report.

 

ColXVehicleTypeBrand
1SUVMercedes
1FamilyFord
2ConvertiblePorsche
2Convertible

Volkswagen

3Family

Ford

3SUV

Ford

4SUV

Skoda

4SUV

Toyota

4Family

Skoda

4Family

Toyota

 

Should merge to

 

ColXVehicleTypeBrand
1SUVMercedes
1FamilyFord
2ConvertiblePorsche, Volkswagen
3Family, SUV

Ford

4SUV, Family

Skoda, Toyota

 

So, basically, case 1 should stay split and the rest should merge as they have overlap either on VehicleType or Brand, or both.

Using CONCATENATEX on both columns works for cases 2, 3, 4, but it's also merging case 1 into 1 record, which I don't want.

I also tried using some helper tables via SUMMARIZE, one for "VehicleType", one for "Brand" and then trying to put the things together again, but no avail.

 

Any  suggestions on possible solutions that might work?

Again, remember that "ColX" from the example is actually around 20 columns of different properties of my dataset, so preferably a suggestion that doesn't require putting all these 20 columns in a DAX function, but something that leverages the grouping of the matrix.

 

Thanks.

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Ok, there's a lot to unpack here.  First my proposed solution. Disclaimer: This is based on Calculated Tables. I don't think you can achieve this with measures.

lbendlin_0-1617411542518.png

 

First, some helper calculated columns in the main table:

 

CtR = CALCULATE(COUNTROWS(Groups),ALLEXCEPT(Groups,Groups[ColX]))
CtB = CALCULATE(DISTINCTCOUNT(Groups[Brand]),ALLEXCEPT(Groups,Groups[ColX]))
CtV = CALCULATE(DISTINCTCOUNT(Groups[VehicleType]),ALLEXCEPT(Groups,Groups[ColX]))

 

Next we identify the rows that need to stay unique and put them into a calculated table

 

Unique = FILTER(Groups,Groups[CtV]=Groups[CtR] && Groups[CtV]=Groups[CtB])

 

Then we concatenate what can be grouped into another calculated table

 

Groupable = SUMMARIZE(filter(Groups,Groups[CtV]<>Groups[CtR] || Groups[CtV]<>Groups[CtB]),Groups[ColX],"VehicleType",CONCATENATEX(Values(Groups[VehicleType]),Groups[VehicleType],","),"Brand",CONCATENATEX(values(Groups[Brand]),Groups[Brand],","),"CtR",MAX(Groups[CtR]),"CtB",max(Groups[CtB]),"CtV",max(Groups[CtV]))

 

And lastly we append both calculated tables for the final result

 

Combined = UNION(Unique,Groupable)

 

This will still require some cleanup but it works on the sample dataset.

 

Note: Please don't use the term "columns"  when you mean something else.

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

Ok, there's a lot to unpack here.  First my proposed solution. Disclaimer: This is based on Calculated Tables. I don't think you can achieve this with measures.

lbendlin_0-1617411542518.png

 

First, some helper calculated columns in the main table:

 

CtR = CALCULATE(COUNTROWS(Groups),ALLEXCEPT(Groups,Groups[ColX]))
CtB = CALCULATE(DISTINCTCOUNT(Groups[Brand]),ALLEXCEPT(Groups,Groups[ColX]))
CtV = CALCULATE(DISTINCTCOUNT(Groups[VehicleType]),ALLEXCEPT(Groups,Groups[ColX]))

 

Next we identify the rows that need to stay unique and put them into a calculated table

 

Unique = FILTER(Groups,Groups[CtV]=Groups[CtR] && Groups[CtV]=Groups[CtB])

 

Then we concatenate what can be grouped into another calculated table

 

Groupable = SUMMARIZE(filter(Groups,Groups[CtV]<>Groups[CtR] || Groups[CtV]<>Groups[CtB]),Groups[ColX],"VehicleType",CONCATENATEX(Values(Groups[VehicleType]),Groups[VehicleType],","),"Brand",CONCATENATEX(values(Groups[Brand]),Groups[Brand],","),"CtR",MAX(Groups[CtR]),"CtB",max(Groups[CtB]),"CtV",max(Groups[CtV]))

 

And lastly we append both calculated tables for the final result

 

Combined = UNION(Unique,Groupable)

 

This will still require some cleanup but it works on the sample dataset.

 

Note: Please don't use the term "columns"  when you mean something else.

Solution works for me, thanks!

Too bad it requires stating all my group fields (the "ColX" corresponding to 20+ fields in my real dataset) in multiple measures and calculated tables. Will be tough on maintenance, but at least I have a solution now and the client can be happy 🙂

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.