Sub topic 12
Sort order and ties: Skip vs Dense
This chapter covers the last two optional arguments of RANKX: the sort order and the tie handling. It assumes you can write a basic RANKX measure, you know how ALL and ALLSELECTED choose the ranking table, and you understand that a measure called inside an iterator is evaluated per row through context transition.
The full RANKX signature
Here is the complete syntax:
RANKX ( <table>, <expression> [, <value> [, <order> [, <ties>]]] )The earlier chapters used the first two arguments. The last three behave as follows:
| Argument | Purpose | Allowed values | Default |
|---|---|---|---|
| `value` | The value to look up in the ranked list | Any scalar expression | The `expression` evaluated in the current context |
| `order` | Which end of the list is rank 1 | `DESC` (or 0, FALSE), `ASC` (or 1, TRUE) | `DESC` |
| `ties` | What rank follows a group of tied items | `Skip`, `Dense` | `Skip` |
Because the arguments are positional, leaving value empty means writing two commas in a row. That is why the measures in this tutorial look like RANKX ( ..., [Sales Amount], , DESC, Dense ).
How RANKX builds a rank
RANKX works in three steps:
- It iterates over the table argument. For each row, it evaluates the expression, which gives a list of values.
- It evaluates
value, which is normally the same expression in the current filter context. This is the number whose position you want. - It returns the position of that number in the sorted list.
Sort order only decides which direction the list is sorted in. Tie handling only decides how positions are numbered after items with equal values.
Sort order: DESC and ASC
With DESC, the largest value gets rank 1. This suits sales, profit, and quantity, where bigger is better.
With ASC, the smallest value gets rank 1. This suits cost, defect rate, delivery time, or any measure where lower is better.
Cost Rank =
RANKX (
ALL ( Product[Product Name] ),
[Total Cost],
,
ASC,
Dense
)Here the cheapest product has rank 1. If you leave out the order argument, RANKX uses DESC, so a cost ranking written without ASC puts the most expensive product first. The measure still returns plausible numbers, so this mistake is easy to miss.
Ties: Skip and Dense
Two items tie when their expression values are equal. RANKX gives tied items the same rank. The ties argument controls the next rank after the group.
- Skip counts the tied items. If two items share rank 2, the next item gets rank 4, because three items rank above it. This is the same as
RANKin SQL and the usual sports convention. Skip is the default. - Dense does not leave gaps. If two items share rank 2, the next item gets rank 3. This is the same as
DENSE_RANKin SQL.
Take five products with these sales:
| Product | Sales Amount | Skip, DESC | Dense, DESC | Skip, ASC | Dense, ASC |
|---|---|---|---|---|---|
| A | 500 | 1 | 1 | 5 | 4 |
| B | 400 | 2 | 2 | 3 | 3 |
| C | 400 | 2 | 2 | 3 | 3 |
| D | 300 | 4 | 3 | 2 | 2 |
| E | 100 | 5 | 4 | 1 | 1 |
Read the table in two steps. First, in the descending columns, B and C tie at rank 2. Skip gives D rank 4, and Dense gives D rank 3. Second, in the ascending columns, the list is reversed. E is 1 and D is 2. B and C tie at 3, and A follows at 5 under Skip or 4 under Dense.
The two measures differ only in the last argument:
Product Rank Skip =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
Product Rank Dense =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)Choosing between Skip and Dense
The right choice depends on what the rank is used for.
- Report display. Use Skip if readers expect a conventional leaderboard. A reader who sees 1, 2, 2, 4 understands that two items share second place.
- Top N filtering. The choice changes how many items pass a filter such as
Rank <= 3. With Skip, the ranks are 1, 2, 2, 4, so the filter returns three items. With Dense, the ranks are 1, 2, 2, 3, so the same filter returns four items. Dense means "the top three distinct values", and Skip means "at most the top three items". - Counting levels. The largest Dense rank equals the number of distinct values. This is useful for building bands or percentile-style groupings.
- Axis labels and sort keys. Dense ranks have no holes, so they are easier to use as a continuous position.
Top 3 Sales =
IF (
[Product Rank Dense] <= 3,
[Sales Amount]
)When products tie at the boundary, this measure can show more than three products. If a report needs exactly three, a plain Skip filter will not guarantee it either. You need a tie-breaker, described below.
Ranks in total rows
The tie argument does not fix the total-row problem. In a total row, no single product is in the filter context, so RANKX has no meaningful position to look up. Keep the HASONEVALUE guard from the previous section:
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)
)The guard, the ALLSELECTED table, the order, and the tie rule are independent choices. Set each one deliberately.
Hidden ties and false ties
Ties often appear or disappear for reasons that are not obvious in the report.
Floating-point differences
Two products can both show 1,234.57 and still not tie. The stored values might be 1234.5671 and 1234.5668. RANKX compares the full values, so they get different ranks even though the report shows the same number. The reverse case also happens. Rounding in a visual does not create a tie.
To rank on what readers actually see, round inside the expression:
Product Rank Rounded =
RANKX (
ALL ( Product[Product Name] ),
ROUND ( [Sales Amount], 0 ),
,
DESC,
Dense
)Because value is omitted, RANKX evaluates the same rounded expression for the current product. This keeps the lookup consistent with the list. If you pass an explicit value, round it in the same way. Otherwise the lookup value may not match any entry in the list.
Blank results
Many products have no sales in a given filter. A blank expression value is not the same as a tie among real values, and the rank for a row with a blank measure can be misleading. If you only want to rank products that have sales, return blank for the others:
Product Rank Sold Only =
IF (
NOT ISBLANK ( [Sales Amount] ),
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)
)Test this against your data, because the way blanks sort affects the numbers.
Forcing a unique rank
If your report needs every item to have a different rank, for example to pick exactly one winner, neither Skip nor Dense is enough. The usual approach is to add a tie-breaker to the expression: a second value that differs between items, such as a product key, scaled so small that it only matters when the main values are equal. The tie-breaker must be added to both the table expression and the looked-up value, and it must be much smaller than the smallest real difference in the measure. Document the approach well, because the scaling is easy to get wrong.
Common errors
- Wrong direction. Ranking a "lower is better" measure without
ASC. - Positional arguments shifted. Writing
RANKX ( table, [Measure], DESC, Dense )putsDESCin thevalueposition. Use empty commas to skipvalue. - Assuming Dense is the default. The default is Skip, so write
Denseexplicitly when you want it. - Top N surprises. A
Rank <= Nfilter returns more or fewer than N items when ties sit at the boundary. - Invisible differences. Items look tied in the visual but rank differently, or the reverse, because of unrounded values.
Exercise
A measure returns these values for five items: 90, 80, 80, 80, 60. What ranks does each item get with DESC order under (a) Skip and (b) Dense? What does the filter Rank <= 3 return in each case?
Show answer
(a) Skip gives 1, 2, 2, 2, 5. The filter Rank <= 3 returns the first four items, because the three tied items all have rank 2. (b) Dense gives 1, 2, 2, 2, 3. The filter returns all five items, because the last item has rank 3. In both cases the filter returns more than three items, so a tie-breaker is needed if you must show exactly three.
Exercise
You want to rank products by Total Cost so that the cheapest product is rank 1, and tied products should not leave gaps in the numbering. Write the RANKX call.
Show answer
RANKX ( ALL ( Product[Product Name] ), [Total Cost], , ASC, Dense ). The empty third argument skips value, ASC makes the smallest cost rank 1, and Dense removes the gaps after ties.