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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Arshadjehan
Helper I
Helper I

Count of records Based on RANKX

I have dynamically filtered the based based on a slicer options of:

Top 3, Top 5 and Top 10. I want to display number of records filtered , e.g when I select Top 3 , records returned after RANKX function may be 3 or more based on ties (I have used Dense option in RANKX) . So how can I show count of records on a card?

 

1 ACCEPTED SOLUTION

@Arshadjehan - Given the form of that RANKX calculation it must be a calculated column. Therefore, this should be something like the following:

 

Male = COUNTROWS(FILTER('Table',[Top N] <= 3 && [Gender]="M"))

Female = COUNTROWS(FILTER('Table',[Top N] <= 3 && [Gender]="F"))

 

 



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

6 REPLIES 6
amitchandak
Super User
Super User

@Arshadjehan , refer if this can help

https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/3...

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Super User
Super User

@Arshadjehan -  I would think you should just be able to use:

Measure = COUNTROWS('Table')

Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2. 



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

Here is the sample data: tblResults

Auto IDRoll NoNameMarksGender
112324ABC515M
22323LMN525M
323234XYN535F
465655DEF525M
5345345PRS510F
.............    

Table has more than a million records.

 

As a first step I have to list only Top 3, Top 5 or Top 10 records

I am doing that by applying RANKX function as below:

Top N = RANKX(ALLSELECTED('tblResult'),[Marks],,,Dense)
 
I am getting the result for Top 3 in table visual as :
RankRoll NoNameMarksGender
312324ABC515M
22323LMN525M
123234XYN535F
265655DEF525M

 

Next I want to display number of student from Top N being Male or female on card visual as:

 

Male: 3

Female:1

 

Hope I have elobarated well now

@Arshadjehan - Given the form of that RANKX calculation it must be a calculated column. Therefore, this should be something like the following:

 

Male = COUNTROWS(FILTER('Table',[Top N] <= 3 && [Gender]="M"))

Female = COUNTROWS(FILTER('Table',[Top N] <= 3 && [Gender]="F"))

 

 



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

@Greg_Deckler  That worked like a charm! Thanks man.

Just one thing needed: Since I am using Dense parameter in RANKX function , so i am having ties in the result. How can I add sequential serial number in the table vaisual as below: 

Serial NoPositionNameMarks
11ABC545
22DEF535
32GEF535
43LMO525
53XYZ525

 

@Arshadjehan - So, generally adding an index in DAX is considered impossible. However, there is a method for doing it.

https://community.powerbi.com/t5/Quick-Measures-Gallery/The-Mythical-DAX-Index/m-p/1093214#M528

 

The other way would be to add a tiny random number to your rank/topn calculation

Top N = RANKX(ALLSELECTED('tblResult'),[Marks],,,Dense) + RANDBETWEEN(.001,.009)

and then you could have a column:

Index = COUNTROWS(FILTER('Table',[Top N]<=EARLIER([Top N])))

This will, in effect break your ties.



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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors
Top Kudoed Authors