13. DAX: Data Analysis Expressions
13.17 Ranking: RANKX and TOPN
RANKX
RANKX(<table>, <expression>, [value], [order], [ties])
Product Rank =
IF(
HASONEVALUE(Product[Product Name]),
RANKX(ALL(Product[Product Name]), [Total Sales], , DESC, DENSE)
)
-- Rank dark stores by speed: fastest (lowest time) = rank 1
Store Speed Rank =
IF(
HASONEVALUE(DarkStore[Store Name]),
RANKX(ALL(DarkStore[Store Name]), [Avg Delivery Time (mins)], , ASC, DENSE)
)
Customer Rank in City =
RANKX(ALLSELECTED(Customer[Customer Name]), [Total Sales], , DESC)
- Use
ALL(...)so every item is ranked against all items, not only itself. HASONEVALUEhides the meaningless rank on the total row.- Order:
DESC(highest = 1) orASC(lowest = 1, useful for delivery time). - Ties:
SKIP(default: 1, 2, 2, 4) orDENSE(1, 2, 2, 3).
TOPN
TOPN(<n>, <table>, <orderBy expression>, [order]) returns a table with the top N rows.
Top 5 Products Sales =
CALCULATE(
[Total Sales],
TOPN(5, ALL(Product[Product Name]), [Total Sales], DESC)
)
Top 5 Share % = DIVIDE([Top 5 Products Sales], CALCULATE([Total Sales], ALL(Product)))
-- Calculated table of the 10 busiest dark stores
Top 10 Stores =
TOPN(
10,
ADDCOLUMNS(VALUES(DarkStore[Store Name]), "Orders", [Total Orders]),
[Orders], DESC
)
Top N without DAX
For a simple "show fakt the top 10 products in this chart", use the Top N filter in the Filters pane (Module 22). Use DAX TOPN when you need a number (like the share of the top 5) or a table.
Ravindra Bagale's Tip
Friends, many students use RANKX(Product, …) and get rank 1 for every row, because the table argument still has the current filter. Use ALL(Product[Product Name]) or ALLSELECTED as the table, and remember that RANKX ties are Skip by default, so pass Dense if you want 1, 2, 2, 3. Never forget this.
Ravindra Bagale's Tip – मराठी
मित्रांनो, बरेच students RANKX(Product, …) वापरतात आणि प्रत्येक row ला rank 1 मिळतो, कारण table argument वर अजून current filter असतो. Table म्हणून ALL(Product[Product Name]) किंवा ALLSELECTED वापरा, आणि लक्षात ठेवा की RANKX मध्ये ties default ने Skip असतात, म्हणून 1, 2, 2, 3 हवं असेल तर Dense द्या. हे अजिबात विसरू नका.
Ravindra Bagale's Tip – हिंदी
दोस्तों, बहुत से students RANKX(Product, …) इस्तेमाल करते हैं और हर row को rank 1 मिलता है, क्योंकि table argument पर अभी भी current filter होता है. Table के रूप में ALL(Product[Product Name]) या ALLSELECTED इस्तेमाल करो, और याद रखो कि RANKX में ties default से Skip होते हैं, इसलिए 1, 2, 2, 3 चाहिए तो Dense दो. यह बिल्कुल मत भूलना.