How to Use RANKX in DAX in Power BI: Syntax and Examples
Use the
RANKX function in DAX to rank values in a table or expression dynamically. It requires a table and an expression to rank, and optionally supports ties and sorting order. For example, RANKX(ALL('Table'), 'Table'[Sales]) ranks sales across all rows.Syntax
The RANKX function ranks each row in a table based on an expression's value. It has these parts:
- Table: The table to rank over.
- Expression: The value to rank by.
- [Value]: Optional, the value to rank for the current row.
- [Order]: Optional,
0for descending (default),1for ascending. - [Ties]: Optional,
Skip(default) orDenseto handle ties.
DAX
RANKX(<Table>, <Expression>, [Value], [Order], [Ties])
Example
This example ranks products by their total sales in descending order. It uses ALL to ignore filters and rank all products.
DAX
Product Rank = RANKX(ALL('Sales'[Product]), CALCULATE(SUM('Sales'[Amount])))
Output
If 'Product A' has sales 1000, 'Product B' 700, and 'Product C' 700, ranks will be: Product A = 1, Product B = 2, Product C = 2 (ties handled by default Skip).
Common Pitfalls
- Not using
ALLor a similar function to remove filters can cause ranks to be calculated only within the current filter context. - Forgetting to specify
Orderleads to unexpected ascending or descending ranks. - Ignoring ties can cause gaps in ranking numbers.
DAX
/* Wrong: ranks only within filtered context */ Rank Wrong = RANKX('Sales', SUM('Sales'[Amount])) /* Right: ranks across all products */ Rank Right = RANKX(ALL('Sales'[Product]), CALCULATE(SUM('Sales'[Amount])))
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| Table | Table to rank over | Required |
| Expression | Value to rank by | Required |
| Value | Value for current row (optional) | Current row value |
| Order | 0 = Descending, 1 = Ascending | 0 (Descending) |
| Ties | 'Skip' or 'Dense' ranking for ties | 'Skip' |
Key Takeaways
Use RANKX to assign ranks dynamically based on an expression over a table.
Use ALL() inside RANKX to rank across all data ignoring filters.
Specify order to control ascending or descending ranking.
Handle ties with 'Skip' or 'Dense' options to avoid gaps or duplicates.
Always test ranking results to ensure correct filter context.