WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
Wise Owl Training
See 479 reviews for our classroom and online training
If you found this blog useful and youâ€™d like to say thanks you can click here to make a contribution. Thanks for looking at our blogs!

BLOGS BY TOPIC

BLOGS BY AUTHOR

BLOGS BY YEAR

How to use the EARLIER function in DAX to sort, group, band and accumulate data
Part four 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
4. Group statistics using the EARLIER function (this blog)
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.

# Group statistics using the EARLIER function

The worked example below shows how to calculate the average price of products by target gender:

What we're aiming for: showing the average price for each product by its target gender.

## Preparing the tables and columns

To get this case study to work, you'll first need to add the target gender table into your model:

You'll need to import this table into your model.

You'll also need to add the TargetGenderId column to the imported products table, and create a relationship between the Product and TargetGender tables:

Change the properties of the Product table to include the TargetGenderId column, and link the two tables together by this column.  It's all good practice!

Finally, in preparation, display the TargetGenderName column within the Product table, using a RELATED function:

Create a calculated column to show the target gender within the products table.

## The formula itself

Here's the formula to give the average price by target gender:

=AVERAGEX(

FILTER(

Product,

EARLIER([TargetGender])=[TargetGender]

),

[FullPrice]

)

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

For Crocky, the FILTER function will return the set of rows shown here shaded: those for which the target gender equals this individual product's target gender. The formula will then take the average price from this set of rows.

The usual caveats apply: remember to create a calculated column and not a measure, and bear in mind that the database engine will calculate the results more efficiently than the above table would suggest.

Finally in this look at the EARLIER function, I'll show how to band rows together.