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

Get top categories and concatenate as a text

I have a table like the following that includes a category column.

I'm looking for a measure to get the categories with the highest count and concatenate them together, while preserving all filters on the table.

 

IndexCategory
1A
2B
3A
4B
5A
6A
7C
8D

 

The measure should return "A" as a text, since it has repeated the most.

 

Another example:

 

IndexCategory
1A
2B
3A
4B
5A
6B
7C
8D

 

For this, the measure should return "A, B" as a text, since A and B are repeated the most.

 

Thanks for your help.

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

Measure = 
VAR __sum = SUMMARIZECOLUMNS( INFO[Category], "@Cnt", COUNTROWS( INFO ) )
VAR __max = MAXX( __sum, [@Cnt] )
RETURN
    CONCATENATEX( FILTER( __sum, [@Cnt] = __max ), INFO[Category], ", " )

Screenshot 2021-07-16 052652.png

 

Another solution by Power Query

let
    Source = Table.Group(INFO, "Category", {"Count", Table.RowCount}),
    Cols = Table.ToColumns(Source),
    Pos = List.PositionOf(Cols{1}, List.Max(Cols{1}), Occurrence.All),
    Custom1 = Text.Combine(List.Accumulate(Pos, {}, (s,c) => s & {Cols{0}{c}}), ", ")
in
    Custom1

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

Anonymous
Not applicable

Regarding the error I mentioned, I used a similar method explained in here to summarize the table. 

 

https://community.powerbi.com/t5/Desktop/Issue-with-Bar-chart-SummarizeColumns-and-AddMissingItems-m...

 

That solved my problem.

 

So, the final measure is:

 

Measure = 
// VAR __sum = SUMMARIZECOLUMNS( INFO[Category], "@Cnt", COUNTROWS( INFO ) )
VAR __sum = FILTER (
ADDCOLUMNS (
    SUMMARIZE (
        ALLSELECTED('INFO'),
        INFO[Category]
        ),
        "@Cnt", CALCULATE(COUNTROWS(INFO))),
        [@Cnt] > 0
        )
VAR __max = MAXX(__sum, [@Cnt])
RETURN
CONCATENATEX( FILTER( __sum, [@Cnt] = __max ), INFO[Category], ", " )

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

Measure = 
VAR __sum = SUMMARIZECOLUMNS( INFO[Category], "@Cnt", COUNTROWS( INFO ) )
VAR __max = MAXX( __sum, [@Cnt] )
RETURN
    CONCATENATEX( FILTER( __sum, [@Cnt] = __max ), INFO[Category], ", " )

Screenshot 2021-07-16 052652.png

 

Another solution by Power Query

let
    Source = Table.Group(INFO, "Category", {"Count", Table.RowCount}),
    Cols = Table.ToColumns(Source),
    Pos = List.PositionOf(Cols{1}, List.Max(Cols{1}), Occurrence.All),
    Custom1 = Text.Combine(List.Accumulate(Pos, {}, (s,c) => s & {Cols{0}{c}}), ", ")
in
    Custom1

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

This is working very well on this example data. However, when I use it on my own large real data, I'm getting this error:

 

 

Couldn't load the data for this visual

MdxScript(Model) (1046, 13) Calculation error in measure 'Table'[measure]: SummarizeColumns() and AddMissingItems() may not be used in this context.

 

 

Do you know why I'm getting this error? Any alternative method? Maybe the error is due to blank values in the table? How to ignore them if blanks are the cause?

 

Thanks.

Anonymous
Not applicable

Regarding the error I mentioned, I used a similar method explained in here to summarize the table. 

 

https://community.powerbi.com/t5/Desktop/Issue-with-Bar-chart-SummarizeColumns-and-AddMissingItems-m...

 

That solved my problem.

 

So, the final measure is:

 

Measure = 
// VAR __sum = SUMMARIZECOLUMNS( INFO[Category], "@Cnt", COUNTROWS( INFO ) )
VAR __sum = FILTER (
ADDCOLUMNS (
    SUMMARIZE (
        ALLSELECTED('INFO'),
        INFO[Category]
        ),
        "@Cnt", CALCULATE(COUNTROWS(INFO))),
        [@Cnt] > 0
        )
VAR __max = MAXX(__sum, [@Cnt])
RETURN
CONCATENATEX( FILTER( __sum, [@Cnt] = __max ), INFO[Category], ", " )

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.