Mastering the RANKX Function in Power BI – From Common Errors to Dynamic Rankings

Power BI offers powerful DAX functions, and RANKX is one of the most useful when you want to assign ranks based on measures like sales, profit, or quantity. But if you're new to it, it might not work as expected at first. In this blog, I'll walk you through a real-world scenario where I explored RANKX, faced common issues, and how I solved them.

Syntax of RANKX

RANKX(
    table,
    expression,
    [value],
    [order],
    [ties]
)
Parameter Description
table The list (table or column) over which ranking is done.
expression The expression to evaluate for each row (usually a measure like [total sale]).
Value(optional) A value to rank (rarely used; DAX infers automatically).
order(optional) ASC (ascending) or desc (descending). Default is asc
ties(optional) How to handle ties: skip, Dense or leave blank (default is Skip).

our goal is we want to calculate a rank for products based on total sales using DAX.

Sample data:


Create a total sale measure:
Total Sale = Sum(Sale[Sales])

First Attempt – RANKX on Sales Table

Product Rank =
RANKX(
    Sale,
    [Total Sale],
    ,
    DESC
)

Problem: All rows returned blank or all ranks were 1.

Root Cause

I was ranking on Sale, which was the fact table. In Power BI, when you rank over a column from a fact table, DAX often doesn't get the full list of distinct values due to row context - especially when writing a measure.

Fix - Use a Separate Dimension Table

I created a separate Product table with distinct product names:

Product = DISTINCT(Sale[Product])
create a relationship as shown in below image:

In table values add product column from Product table that we created using distinct DAX function.
Then we are getting proper rank as shown below.





Comments

Post a Comment

Popular posts from this blog

Step-by-Step Guide: Power Automate Custom Connector Using Graph API from Azure App Service

Calling Microsoft Graph API from Power Automate Using Azure App Services – Step-by-Step Guide

Step-by-Step: Give Unique Permissions to OneDrive Files Using Power Automate and Graph API (No Premium License Needed)