How to use the EARLIER function in DAX to sort, group, band and accumulate data
Part three of a five-part series of blogs

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.

  1. The EARLIER function in DAX
  2. Running totals using the EARLIER function in DAX
  3. Using the EARLIER function to rank data (this blog)
  4. Group statistics using the EARLIER function
  5. Banding using the EARLIER function

This blog is part of our online SSAS Tabular tutorial; we also offer lots of other Analysis Services training resources.

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.

Ranking by price

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:

=COUNTROWS(

FILTER(

Product,

EARLIER([FullPrice])<[FullPrice]

)

)+1

To see how this works, consider the example of the first product in alphabetical order, Crocky:

Example of Crocky product

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.

This blog has 0 threads Add post