BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
Although the EARLIER function in DAX is complicated, it's also very useful! This blog shows how the function works, and how to use it to create running totals, sort rows, create group statistics and divide data into bands.
- The EARLIER function in DAX
- Running totals using the EARLIER function in DAX
- Using the EARLIER function to rank data (this blog)
- Group statistics using the EARLIER function
- Banding using the EARLIER function
Posted by Andy Brown on 13 January 2016
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Using the EARLIER function to rank data
A previous blog showed how you can rank data in DAX using the RANKX function; this page gives an alternative method, using EARLIER.
We'll rank the products by their list price. As we've seen in earlier blogs, The Emperor is the most expensive and Wol the cheapest.
Here's a formula for the calculated column (not a measure, don't forget!) to achieve this:
To see how this works, consider the example of the first product in alphabetical order, Crocky:
The FILTER function returns the table of products, but filtered to show only those ones where the current row's FullPrice (11.50) is less than the product's full price. This gives the blue-shaded products: Pingu, Simon and The Emperor. The formula then counts how many rows there are in this set of rows, and adds one to it (this is needed because we want the top ranking product to be 1, not 0 or blank).
The calculation above is for one product; the formula will then be repeated again for the other 12 products. Although this look like it does 13 x 13 calculations, the database engine will optimise them to avoid repetition where possible.
The next worked example is on calculating group averages.