WMA Hub

Tools / DAX library

Rank by sales

Give each product a rank (1 = best) that respects the slicers.

Ranking & Top N · Intermediate · Not yet reviewed

Product Sales Rank =
IF(
    HASONEVALUE(Product[Product Name]),
    RANKX(
        ALLSELECTED(Product[Product Name]),
        [Total Sales],
        ,
        DESC,
        DENSE
    )
)

How it works

RANKX evaluates [Total Sales] for every product in the list given by ALLSELECTED (all products allowed by slicers) and returns the position of the current product. DESC puts the highest sales first; DENSE gives tied products the same rank without leaving gaps.

HASONEVALUE makes the total row blank instead of showing a meaningless rank.

What your model needs

- A Product table with a Product Name column, related to Sales. - Total Sales = SUM(Sales[Amount]) (called [Total Sales]).

Small example

Input data

Product | Total Sales
A | 500
B | 300
C | 150
D | 50

Expected result

Product | Product Sales Rank
A | 1
B | 2
C | 3
D | 4

Common mistakes and limitations

- Ranking over ALL(Product[Product Name]) instead of ALLSELECTED ignores slicers, so ranks do not change when you filter. - The rank changes meaning if the visual shows a different column (for example Category); the measure ranks product names.

Tags: RANKX, ranking, ALLSELECTED