Sub topic 12
Sort order and ties: Skip vs Dense
This chapter assumes you can write a basic RANKX measure and know how to pick the ranking table with ALL or ALLSELECTED. Here you control two more arguments: the sort order and the tie handling. They decide which number each product gets.
The full RANKX signature
RANKX ( <table>, <expression> [, <value> [, <order> [, <ties>]]] )orderisDESC(the default) orASC.tiesisSkip(the default) orDense.
To set order or ties and leave value out, keep its comma, as in ,,DESC,Dense. The earlier chapters did this by writing an empty argument.
Sort order
With DESC, the largest value gets rank 1. This suits sales, profit, and similar measures where bigger is better. With ASC, the smallest value gets rank 1. This suits measures such as defect counts, delivery days, or cost.
Cost Rank =
RANKX (
ALL ( Product[Product Name] ),
[Total Cost],
,
ASC
)The cheapest product gets rank 1. If you leave out ASC, the most expensive product gets rank 1.
What a tie is
A tie happens when two or more items in the ranking table produce the same value for the expression. Suppose four products have these sales:
| Product | Sales Amount | Rank with Skip | Rank with Dense |
|---|---|---|---|
| A | 100 | 1 | 1 |
| B | 100 | 1 | 1 |
| C | 80 | 3 | 2 |
| D | 50 | 4 | 3 |
Both settings give tied items the same rank. They differ in the rank that follows the tie.
- Skip counts the tied items. A and B take positions 1 and 2, so the next item is 3. This is how sports standings work.
- Dense never leaves a gap. The next distinct value always gets the next integer.
Choosing between them
Use Skip when the rank means "how many items are ahead of me, plus one". Use Dense when the rank is a tier, such as "the second-best sales level". Dense is also useful with a "Top N" filter. A visual filter of rank less than or equal to 3 can show more than three products if there are ties. With Skip, a tie at rank 3 can still show extra rows, but a tie at rank 2 pushes the next rank to 4, so rank 3 never appears.
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)This measure uses Dense, so the ranks are 1, 1, 2, 3 for the table above.
Ties and the slicer
Ties depend on the ranking table. If you use ALLSELECTED, the rank is recalculated against whatever products the slicers leave visible. Two products that were tied in the full list may be the only ones left, or a tie may disappear because one of its members was filtered out. The tie rule applies to the filtered list.
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)Here HASONEVALUE returns blank in total rows, where no single product is in context.
Common errors
- Writing
RANKX ( ..., [Sales Amount], DESC, Dense ). The third slot isvalue, soDESClands there. Add the empty argument. - Expecting rank 2 after a tie under the default
Skip. - Using
ASCfor a "best seller" ranking, which puts the weakest product first.
Exercise
Four products have sales of 90, 90, 90, and 40. What ranks does RANKX return in descending order with Skip, and with Dense?
Show answer
With Skip the ranks are 1, 1, 1, 4, because three items are ahead of the last product. With Dense the ranks are 1, 1, 1, 2, because no integers are skipped.