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
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])

@ 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!:
The Definitive Guide to Power Query (M)

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])

@ 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!:
The Definitive Guide to Power Query (M)

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...


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.