Sub topic 15
Ranking and lookup functions
This chapter assumes you can already create a visual calculation, know the axis and reset parameters, and have used functions such as PREVIOUS. Ranking and lookup functions are a small, lower-priority group. You will use them less often than running sums or percent of total, but they cover two common requests: "what position is this row in?" and "what is the value on some other row?"
What this group does
Both kinds of function read the visual's own rows, like the relative functions you have already used. They differ in what they return:
| Need | Function | Returns |
|---|---|---|
| Position of the current row among its peers | `RANK` | A number: 1, 2, 3, and so on |
| Value from a row you choose | `LOOKUP` | The expression evaluated on that other row |
| Same lookup, evaluated with the totals row included | `LOOKUPWITHTOTALS` | The expression evaluated on that other row, totals included |
Ranking rows with RANK
RANK numbers the rows along an axis, ordered by a value you choose. Suppose a matrix shows Product Category on rows and Sales Amount as values. To rank categories by sales, with the largest first, add this visual calculation:
Sales rank = RANK(DENSE, ROWS, ORDERBY([Sales Amount], DESC))The arguments mean the following:
DENSEhandles ties without gaps. Two categories tied for first are both 1, and the next one is 2.SKIPleaves a gap instead, so the next one would be 3.ROWSis the axis from the previous chapters. It ranks down the rows of the visual.ORDERBY([Sales Amount], DESC)says which value to sort on and in which direction.DESCputs the highest value at rank 1.
If the visual has a reset, the ranking restarts at each new group. With Year on an outer level and Category inside it, ranking within each year gives every year its own 1, 2, 3.
Looking up a value on another row
LOOKUP returns the result of an expression on a row you pick, rather than on the current row. A typical use is showing each category next to the sales of the top-ranked category, so you can compare every row with the leader. LOOKUPWITHTOTALS does the same but includes the totals row when it evaluates.
The argument lists for these functions are longer than for PREVIOUS and NEXT, and they have changed between Power BI releases. Look up the current signature in the DAX function reference before you write one. Then check the results against a few rows you can calculate by hand.
Common mistakes
- Ranking a field that is not in the visual.
ORDERBYcan only use fields that are in the visual, including hidden ones. If the field is not there, add it and hide it. - Expecting the totals row to get a rank. Totals usually show a blank or an unhelpful number, so format or ignore that row.
- Forgetting that the result changes with the layout. If someone swaps the rows and columns, or adds a field to the rows, the rank is recalculated against the new layout.
- Using
RANKto filter to a top N. A visual calculation cannot filter rows out of its own visual. Use a Top N filter on the visual, or a measure if the ranking must feed other calculations.
Exercise
A matrix shows Year on the outer row level and Product Category inside it, with Sales Amount as the value. You want each category ranked 1, 2, 3 within its own year, with the highest sales first and no gaps after ties. Which parts of the RANK call and the visual calculation settings control this?
Show answer
Use DENSE for the ties argument so tied rows do not leave a gap. Use ORDERBY([Sales Amount], DESC) so the highest sales get rank 1. Set the axis to ROWS, and set the reset to restart at the Year level so each year is ranked separately.