Sub topic 15
Ranking and lookup functions
This chapter assumes you can already write a basic visual calculation with RUNNINGSUM and PREVIOUS. It also assumes you understand the axis and reset parameters and have used COLLAPSE for percent of parent. Here you will add ranking, which numbers the rows by value. You will also add lookups, which pull a value from another row into the current one.
Why these functions come last
Ranking and lookup are the least used visual calculation functions. You will write them less often than a running sum, but they cover questions the earlier functions answer awkwardly:
- Which product is second in sales within each category?
- How far is each month from the first month of the year?
- What was the value three rows back, not one?
All of them work on the same data layout you already know. The visual is a grid of rows and columns, and each calculation runs once per cell. A ranking function compares a cell with the other cells on its axis. A lookup function reads a different cell on that axis.
Ranking with RANK
RANK returns the position of the current row among the rows on the axis, ordered by an expression you choose. Rank 1 is the first position in that order. With a descending sort on sales, rank 1 is the best seller.
Sales rank = RANK(DENSE, ROWS, ORDERBY([Sales Amount], DESC))The arguments, in order, mean this:
DENSEis the tie rule. Two rows with the same value get the same rank, and the next rank follows without a gap: 1, 1, 2. The other option isSKIP, which leaves a gap after a tie: 1, 1, 3. If you omit the tie rule, the default isSKIP.ROWSis the axis to rank across, the same wayROWSworks in COLLAPSE.ORDERBY([Sales Amount], DESC)is the value to sort by and the direction.
Ranks follow the visible rows
A visual calculation sees only the rows currently in the visual. If you filter the visual to three categories, the ranks run 1 to 3 across those three categories. If you drill down, the ranks restart for the new rows.
This is the main difference from a measure that uses RANKX over ALL. A model-level ranking can stay stable when a slicer changes. A visual calculation ranking cannot, because it ranks whatever is on screen.
Ranking within a group
On a matrix with Category on rows and Product under it, you usually want ranks inside each category. The ranking should restart at each category. That is the job of the reset parameter you met earlier. Set it to restart at the Category level, and the ranks begin again at 1 for each new category.
Read the ranking formula the way you read a running sum: first the axis, then the restart point. If the numbers look wrong, check these two things before you check the expression.
Lookups relative to the current row
PREVIOUS and NEXT read a neighbor row. Both take an optional step count, so you can look further away.
Sales two rows back = PREVIOUS([Sales Amount], 2)The first two rows have no row two steps back, so those cells are blank. Treat that blank deliberately. In arithmetic, a blank behaves like zero, so a difference on the first row can look like a huge jump.
Change vs two back =
VAR _back = PREVIOUS([Sales Amount], 2)
RETURN
IF(ISBLANK(_back), BLANK(), [Sales Amount] - _back)Returning BLANK() for the early rows keeps the chart from drawing a misleading first bar.
Lookups to a fixed row: FIRST and LAST
FIRST and LAST read the first or last row on the axis, no matter where the current row is. This gives you a baseline that every row compares against.
Change since first = [Sales Amount] - FIRST([Sales Amount])Share of last = DIVIDE([Sales Amount], LAST([Sales Amount]))The first formula answers "how much have we grown since the start of the period?" The second shows each row as a fraction of the final row, which is useful for indexed charts where the last value equals 1.
Like PREVIOUS and NEXT, FIRST and LAST accept an axis and a reset. With a reset at the Year level, FIRST means "first month of this year", and the comparison restarts for each year.
Choosing the right function
The diagram shows how to pick among the functions in this chapter.
The general LOOKUP function
Visual calculations also include a more general lookup function, LOOKUP, which finds a row by matching a value instead of by position. Use it when the row you need is defined by content, such as "the row where Month equals January", not by how far it is from the current row. Its argument list differs from PREVIOUS and FIRST, so read its entry in the function reference before you use it. This chapter does not give an example, because you should confirm the signature against your Desktop version.
Failure modes and gotchas
These are the common mistakes with ranking and lookup:
- Ranking the wrong axis. On a matrix with both rows and columns,
ROWSandCOLUMNSgive different results. If your ranks run across the columns when you wanted them down the rows, change the axis argument. - Forgetting the reset. Without a reset, ranks run across the entire visual, not within each group. Subtotal rows can also receive a rank that is not meaningful.
- Blank handling. Rows with no value may be placed first or last in the ranking, depending on the blanks setting. Check them before you trust the order.
- Ties. Two products with equal sales share a rank. With
SKIPthe numbers jump, which can break a "top 5" label if you count rows. PickDENSEwhen you want continuous rank numbers. - Changing the visual. Adding or removing a field, a filter, or a hidden field changes which rows exist, so every rank and every lookup changes with it.
- Totals. Lookup functions evaluated on a total row can return blank or an unexpected value. Test the total row, and hide it if it makes no sense.
When a measure is better
Visual calculations do not exist in the model. Another visual cannot reuse them, and you cannot use them in a different report. If you need a stable ranking that does not change when someone filters the visual, or one that appears in several visuals, write a measure with RANKX instead.
A visual calculation is the better tool when the ranking is part of one visual and should follow whatever that visual shows.
| Need | Visual calculation | Measure |
|---|---|---|
| Rank among visible rows | Yes | Possible but verbose |
| Rank stable under filtering | No | Yes |
| Reuse in other visuals | No | Yes |
| Compare with row N steps back | Yes, with PREVIOUS | Needs date logic |
| Change since first row | Yes, with FIRST | Needs date logic |
Worked example
Take a table with Product and Sales Amount, sorted by Sales Amount descending. Add these three visual calculations in turn.
Sales rank = RANK(DENSE, ROWS, ORDERBY([Sales Amount], DESC))Gap to top = FIRST([Sales Amount]) - [Sales Amount]Gap to next = [Sales Amount] - NEXT([Sales Amount])Because the table is sorted by sales, FIRST is the best seller. "Gap to top" shows how far each product trails it, and "Gap to next" shows the lead each product has over the product below. The last row's "Gap to next" is blank because no next row exists.
If you re-sort the table by Product name, the rank column stays correct, but "Gap to top" and "Gap to next" now compare alphabetical neighbors. Lookup functions follow the row order, and RANK follows its ORDERBY expression. Remember this difference when you change the sort order of a visual.
Exercise
A matrix shows Category on rows and Product under each category. Your RANK formula gives 1 to the top product in the whole matrix, and you want 1 for the top product in each category. What do you change, and why?
Show answer
Set the reset parameter so the calculation restarts at the Category level. Without a reset, the ranking runs across all rows on the axis. With the reset, each new category starts again at rank 1.
Exercise
A table has three rows with sales of 500, 500, and 300. What ranks does RANK return, in descending order of sales, with DENSE and with SKIP?
Show answer
With DENSE the ranks are 1, 1, 2. With SKIP the ranks are 1, 1, 3, because the tie uses up rank 2.
Exercise
Why does Change since first = [Sales Amount] - FIRST([Sales Amount]) show 0 on the first row, and what would you change to restart the baseline each year?
Show answer
On the first row, the current value and FIRST are the same cell, so the difference is 0. To restart the baseline each year, set the reset parameter to the Year level, so FIRST refers to the first row of each year.