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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Ski900
Helper II
Helper II

How to calculate rank by count of occurances per category

I have a table where I am trying to assign a rank based on the count of each category. For example, below on the left is the original column and on the right is the desired results. One issue that I recognize is that I will have duplicate ranks, i.e. Category B and D both have a rank of 2. I am not sure how to handle this, or if it's even possible to handle. I am unable to use dates (to my knowledge) because the data set comes in with the date in Month Year format (April 2018). Any help is much appreciated.

(Original) (Desired Results) 
Category CategoryCount of CategoryRank
A A41
A B32
A C13
B D32
B    
B    
C    
D    
A    
D    
D    
1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

If all you have is a table listing categories, then perhaps something like this:

 

Measure = 
VAR __currentCategory = MAX(CategoryRanks[Category])
VAR __tmpTable = ALL(CategoryRanks)
VAR __tmpTable1 = SUMMARIZE(__tmpTable,CategoryRanks[Category],"__Count",COUNT(CategoryRanks[Category]))
VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__Rank",RANKX(__tmpTable1,[__Count]))
RETURN MAXX(FILTER(__tmpTable2,[Category]=__currentCategory),[__Rank])


Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

7 REPLIES 7
tlo11
Frequent Visitor

Hey there,

 

I have a similar issue that for some reason I cant figure out how to get it working with Dax.

 

So (part of) my data model looks like that:

  • Table Applicants with three columns: Name of Applicant, Code of Job Offering, Count of Applications (could be more than one)

What I want to generate is a rank that ranks a job offering based on the total count of applications (i.e. the job.  This should be something like this?

Ranking = RANK.EQ(SUM('Applications'[CountOfApplications]),'Applications'[CodeOfJobOffering],DESC)
 
Any help is greatly appreciated!
Ashish_Mathur
Super User
Super User

Hi,

 

Drag Category to the Table visual and try these measures

 

Category Count = COUNTROWS(Data)

Rank = RANKX(ALL(Data[Category]),[Category Count])

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

If all you have is a table listing categories, then perhaps something like this:

 

Measure = 
VAR __currentCategory = MAX(CategoryRanks[Category])
VAR __tmpTable = ALL(CategoryRanks)
VAR __tmpTable1 = SUMMARIZE(__tmpTable,CategoryRanks[Category],"__Count",COUNT(CategoryRanks[Category]))
VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__Rank",RANKX(__tmpTable1,[__Count]))
RETURN MAXX(FILTER(__tmpTable2,[Category]=__currentCategory),[__Rank])


Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

99% done. Change add "Dense" to RANKX:

Measure = 
VAR __currentCategory = MAX(CategoryRanks[Category])
VAR __tmpTable = ALL(CategoryRanks)
VAR __tmpTable1 = SUMMARIZE(__tmpTable,CategoryRanks[Category],"__Count",COUNT(CategoryRanks[Category]))
VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__Rank",RANKX(__tmpTable1,[__Count]))
RETURN MAXX(FILTER(__tmpTable2,[Category]=__currentCategory),[__Rank],,,Dense)

What does Dense do in RANKX?

Thank you so much! I was so far off base. Is there a benefit to creating var's versus nesting them?

Sometimes you definitely need to use VAR's for functionality but it's mostly readability. Just say no to Perl code...



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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