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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
RodgerB
Frequent Visitor

RANKX and ALL Functions

I am struggling to figure out how to use the RANKX function.  I've made my DAX work using 

RANKX(ALLSELECTED(Invoices[Product Name]),CALCULATE([Total Sales]),,desc)). 

What I don't understand is all the forums that tell you to use the ALL() function.  Whenever I do, all I get is 1's.  The above works, I just don't understand why one works and not the other.

Also, is there a way to have any dimension field not show a value, so just the rank results from the ALLSELECTED field display a value?
 
Thanks!
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi Rodger, 

 

The ALLSELECTED makes sure that you only consider the "selected" Invoice[Product Names] when the rank is calculated. When you use ALL, as you can guess, the total sales for all product names are considered and you may end up gaps in your ranking sequence. So normally this does not explain why you get 1's when using ALL, so that could be related to your data model and definition of [Total Sales].

 

But typically you get all 1's from RankX when you use the measure in the wrong context. For instance if you use you measure in a matrix with rows per country, you will get 1's. 

 

The reasons is that the lookup table used for [total sales] contains the total sales amounts by country and by product and the lookup table used to determine the rank only contains values per country. Therefore the total per country (used for ranking) is always higher (or equal at best when there is just one product in the country) than any the total per product within a given country.

I also see that you use the parameter 'desc', which is not necessary because it is the default setting, and you wrap the [Total Sales] measure with CALCULATE which is not necessary since a measure already contains an implicit CALCULATE.  So RANKX(ALLSELECTED(Invoices[Product Name]),[Total Sales]) should give the same result.

Hope this helps.

 

jan 

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi Rodger, 

 

The ALLSELECTED makes sure that you only consider the "selected" Invoice[Product Names] when the rank is calculated. When you use ALL, as you can guess, the total sales for all product names are considered and you may end up gaps in your ranking sequence. So normally this does not explain why you get 1's when using ALL, so that could be related to your data model and definition of [Total Sales].

 

But typically you get all 1's from RankX when you use the measure in the wrong context. For instance if you use you measure in a matrix with rows per country, you will get 1's. 

 

The reasons is that the lookup table used for [total sales] contains the total sales amounts by country and by product and the lookup table used to determine the rank only contains values per country. Therefore the total per country (used for ranking) is always higher (or equal at best when there is just one product in the country) than any the total per product within a given country.

I also see that you use the parameter 'desc', which is not necessary because it is the default setting, and you wrap the [Total Sales] measure with CALCULATE which is not necessary since a measure already contains an implicit CALCULATE.  So RANKX(ALLSELECTED(Invoices[Product Name]),[Total Sales]) should give the same result.

Hope this helps.

 

jan 

 

Anonymous
Not applicable

please check below link for better understanding.

 

https://community.powerbi.com/t5/Desktop/What-s-the-difference-between-ALL-vs-ALLSELECTED-with-formu...

 

https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept...

 

 

Thanks & regards,

Pravin Wattamwar

 

 

if it resolves your problem Give kudos and mark it as solution

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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