Dax Function: RANKX
Category: Statistical Functions
The RANKX function in Power BI is a DAX function that ranks values in a dataset based on a specified expression and optional sorting order. Unlike RANK.EQ, it allows dynamic control over the ranking logic and context, making it highly versatile for advanced scenarios.
Purpose of the Function
To rank rows or values in a table based on a given measure or column.
Type of Calculations
Assigns a rank to each row or value based on sorting logic.
Practical Use Cases
- Ranking sales figures dynamically by region, category, or product.
- Identifying top performers in datasets with custom filters.
- Building leaderboards or percentile-based analysis.
RANKX(<table>, <expression>, [<value>], [<order>], [<ties>])</ties></order></value></expression></table>
| Parameter | Type | Description |
|---|---|---|
<Table> | Table | The table or dataset to evaluate. |
<Expression> | Scalar | The expression or measure to rank. |
[<Value>] | Scalar | (Optional) The value to rank. If omitted, evaluates the current row context. |
[<Order>] | Boolean | (Optional) Sorting order: 0 (default) for descending, 1 for ascending. |
[<Ties>] | String | (Optional) Defines tie behavior: “Skip” (default), “Dense”. |
How Does RANKX Dax Works
Mathematical Principle
The function evaluates <Expression> for each row in <Table> and assigns a rank based on the specified <Order>. When ties occur:
Skip: Tied rows share the same rank, and the subsequent rank skips the tied count.
Dense: Tied rows share the same rank, and subsequent ranks increment by 1.
Example:
Dataset: {50, 40, 40, 30}
Skip (Default):
Descending: Ranks = {1, 2, 2, 4}
Ascending: Ranks = {4, 2, 2, 1}
Dense:
Descending: Ranks = {1, 2, 2, 3}
Ascending: Ranks = {3, 2, 2, 1}
What Does It Return?
Returns a scalar value representing the rank of a value or row within the specified
<Table>.The output varies based on the tie-handling logic:
Skip: Assigns the same rank to tied values and skips subsequent ranks.
Dense: Assigns the same rank to tied values without skipping.
When Should We Use It?
Custom Ranking Logic: When specific expressions or filters are required for ranking.
Dynamic Contexts: Ranking values based on slicers or measures.
Complex Calculations: Combining ranking with advanced filters or calculated measures.
Examples
Basic Usage :
Rank products by total sales in descending order:
RANKX(ALL(Products), SUM(Sales[TotalSales]), , 0)
Output: Highest sales receive rank 1.
Column Usage
Add a rank column to a dataset:
ADDCOLUMNS(
Products,
"SalesRank",
RANKX(Products, SUM(Sales[TotalSales]), , 0)
)
Use Case: Adds a SalesRank column to rank products based on sales.
Advanced Usage
Rank sales dynamically within a specific region:
RANKX(
FILTER(Sales, Sales[Region] = "North"),
SUM(Sales[TotalSales]),
, 0
)
Use Case: Ranks sales within the “North” region.
Tips and Tricks
Context Awareness: Ensure the
<Table>parameter reflects the desired evaluation context.Tie Handling: Choose tie logic (
SkiporDense) based on business requirements.Combine with Filters: Use
FILTERto rank within subsets or categories dynamically.Performance: Minimize large table scans by pre-aggregating data when possible.
Performance Impact of RANKX DAX Function:
Evaluate
<Expression>efficiently to avoid recalculating heavy measures repeatedly.For large datasets, pre-aggregate or use summary tables to improve performance.
Related Functions You Might Need
RANK.EQ: Simpler ranking function with less flexibility.
TOPN: Retrieves the top N values from a dataset.
PERCENTILE.EXC: Calculates percentiles for more granular rankings.
Want to Learn More?
For more information, check out the official Microsoft documentation for RANKX You can also experiment with this function in your Power BI reports to explore its capabilities.
Discover how Power BI can revolutionize your data by converting it into meaningful, results-driven insights with guidance from our expert consultants. Whether you need help with advanced DAX computations, crafting intuitive and engaging dashboards, or boosting the efficiency of your data models, our Power BI professionals offer solutions specifically tailored to meet your organization’s unique requirements. Visit our Power BI Consulting Services page to explore how we can support smarter, data-led decision-making in your business.
It ranks rows or values in a dataset based on a specified expression and sorting logic.
RANKX provides more flexibility in defining the evaluation context and tie behavior.
By default, it uses the “Skip” logic, but you can specify “Dense” for continuous ranking.
Yes, it supports dynamic ranking based on slicer selections and filters.
Use 0 for descending order (default) and 1 for ascending order.