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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
KMont
Frequent Visitor

Ranking without ties

Hi,

 

I am learning DAX and am struggling with an issue ranking a table to get a Top N result.  I have many ties as the data set is not large.  The table is already filtered using the filtering in Power BI Desktop.  I have come across some tutorials, but am running into syntax issues.

 

I am trying to break the ties through alphabetizing, but I keep getting syntax errors.  It is forcing me to use FILTER(ALL in the DAX measure, but then the filter that is already on the table is removed.

 

This is what I am using...

 

Alphabetical Rank =
var CURRENTRX = SELECTEDVALUE('Rx Counts'[drug_generic_name])
VAR Result=
    COUNTROWS(
        FILTER(
            ALL('Rx Counts'[drug_generic_name]),'Rx Counts'[drug_generic_name]<=CURRENTRX))

RETURN
    Result
 
The FILTER(ALL is removing the filter from my table (which I do not want), but that is the only way that I do not get syntax errors.  It seems to be working otherwise.  As you can see my rx total has ties, the rank column numbers the rows skipping the ties the the alphabet rank breaks the ties, however, it is using my larger data set instead of my filtered data set.
 
AbbreviationRx TotalRankAlphabetical Rank
dox51141
mup42312
tre42439
tri42451
spi35400
cli2692
clo2699
des26127
amm1918
ben1943
cef1968
cic1978
des19128
flu19186
flu19193
flu19201
ket19264
met19299
pre19365
ure19456

 

Any help you could provide would be most appreciated!!  Thanks!

5 REPLIES 5
technolog
Super User
Super User

I hope this message finds you well. I've noticed that this solution remain unresolved. If any of you have managed to find a resolution to the issue, I kindly request that you share your solution for the benefit of the entire community. Alternatively, if you're still facing challenges, please do let us know as well.

Your insights and updates will greatly assist others who might be encountering the same challenge.

ERD
Community Champion
Community Champion

@KMont , first parameter of FILTER should be a table. So you can use VALUES('Rx Counts'[drug_generic_name]) instead of ALL.

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

KMont
Frequent Visitor

I just want the abbreviation column and the Rx Total column.  I am wanting a top N...the first 15 will do sorted descending.  The issue is that the data set is small, and there are Abbreviations where the number ties... the 3 below all have 4 records for example so when there is a tie, I would like to rank alphabetically.

 

mup

 

 

 

tre

 

 

 

tri

   

 

The base table ‘Rx Counts’ is a big table with 204,616 rows.  It includes a unique id on each row with the abbreviation, network, month, year, and complete date.  The visual I have summarizes the count of records by abbreviation, but only includes a specific network.

 

Here is an example of ‘Rx Counts’

 

Screenshot 2023-09-01 104336.png

@KMont thanks for that!

I take it that Rx Total is a measure.

 

I would suggest a measure like this, using the RANK function:

 

Rank = 
RANK (
    DENSE,
    -- Abbreviations values in overall context of visual
    CALCULATETABLE ( VALUES ( 'Rx Counts'[Abbreviation] ),  ALLSELECTED () ),
    -- Order by [Rx Total] and break ties with Abbreviation
    ORDERBY ( [Rx Total], DESC, 'Rx Counts'[Abbreviation], ASC ) 
)

 

Then apply a visual level filter (such as <= 15) on the Rank measure.

 

Does this work at your end? Please post back if needed.

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn
OwenAuger
Super User
Super User

Hi @KMont 

Either the RANK or ROWNUMBER functions should be useful here, as they allow ranking based on multiple measures/columns.

 

Could you clarify which columns/measures you want to appear in your final visual (aside from ranking measures), and also show a snapshot of the underlying 'RX Counts' table (santised if needed)?

 

e.g. Do you want to see

Abbreviation drug_generic_name Rx Total

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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