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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
bleow
Frequent Visitor

RANKX: using aggregate functions on the expression

I have the following data on ID and Type:

IDTypeType TopNRanking
1AA1
2BB2
3COthers3
4AA1
5AA1
6BB2
7COthers3
8DOthers4
9EOthers5

and I want to add the Type TopN column that shows the type for the top N types by count, and Others for the rest.

 

To do this, I tried adding a Ranking column that (theoretically) ranks the type by frequency of appearance, i.e. since A appears 3 times it has a ranking of 1, B ranking of 2 etc. From there, I can use the following code:

 

RETURN IF(
    Ranking<=3, 
    [Type], 
    "OTHERS"
)

 

To get Type TopN column.

 

The issue comes with trying to get the Ranking column. I tried:

 

RANKX(
    table,
    [Type],
    COUNT(table[Type]),
    DESC,
    Dense
)

 

but it gives me the error "Function 'RANKX' does not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values."

I know my requirement is possible to achieve by duplicating the table and using Group By to aggregate the count, apply the RANKX filter, then do a left join, but I'm wondering if RANKX can do this in one step and if there's anything I'm not understanding about this function. Thanks

1 ACCEPTED SOLUTION
v-angzheng-msft
Community Support
Community Support

Hi, @bleow 

 

To create 2 column with this:

_RankColumn =
RANKX (
    'Table',
    CALCULATE ( COUNT ( [Type] ), ALLEXCEPT ( 'Table', 'Table'[Type] ) ),
    ,
    DESC,
    DENSE
)
_TopN =
IF ( 'Table'[_RankColumn] <= 3, [Type], "Others" )

 Result:

vangzhengmsft_0-1625022534646.png

Please refer to the attachment below for details

 

For references:

RANKX  RANKX 

 

Hope this helps.

 

Best Regards,
Community Support Team _ Zeon Zheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-angzheng-msft
Community Support
Community Support

Hi, @bleow 

 

To create 2 column with this:

_RankColumn =
RANKX (
    'Table',
    CALCULATE ( COUNT ( [Type] ), ALLEXCEPT ( 'Table', 'Table'[Type] ) ),
    ,
    DESC,
    DENSE
)
_TopN =
IF ( 'Table'[_RankColumn] <= 3, [Type], "Others" )

 Result:

vangzhengmsft_0-1625022534646.png

Please refer to the attachment below for details

 

For references:

RANKX  RANKX 

 

Hope this helps.

 

Best Regards,
Community Support Team _ Zeon Zheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@bleow , You can try measure like

RANKX(
table,
calculate(COUNT(table[Type])), ,
DESC,
Dense
)

 

if you are using above measure then this should also be measure

 

RETURN IF(
    Ranking<=3, 
    [Type], 
    "OTHERS"
)

 

or consider top N

 

TOPN: https://www.youtube.com/watch?v=QIVEFp-QiOk&list=PLPaNVDMhUXGaaqV92SBD5X2hk3TMNlHhb&index=42

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.