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
- Group statistics using the EARLIER function (this blog)
- 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.
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:
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.