Sub topic 8
Year-over-year change and growth percentage
This chapter builds on the prior-year measure and turns it into a change and a growth percentage. It assumes you can write basic DAX measures with SUM and DIVIDE, and that your model has a marked date table related to a fact table in a star schema. It also assumes you have already written Total Sales and Sales PY, and that you can place a measure in a matrix or line chart.
The working model
Year-over-year (YoY) change compares a value for the current period with the same period one year earlier. There are two forms:
- Absolute change is current minus prior. It is in the same unit as the measure, such as currency.
- Growth percentage is the absolute change divided by the prior value. It is a ratio that you format as a percentage.
Both forms need two numbers in the same filter context: the current total and the prior-year total. You already have both as measures, so the new work is the arithmetic and the handling of missing prior values.
Total Sales = SUM ( Sales[Sales Amount] )
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Absolute change
The absolute change is a plain subtraction of two measures.
Sales YoY Change =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales PY]
RETURN
CurrentSales - PriorSalesIf PriorSales is blank, DAX treats it as 0 in a subtraction. The result then equals the current sales, which can look like a huge gain for a new product. Decide whether that is what you want. If not, return blank when there is no prior value:
Sales YoY Change (blank if no PY) =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales PY]
RETURN
IF ( NOT ISBLANK ( PriorSales ), CurrentSales - PriorSales )Growth percentage
Divide the change by the prior value. Use variables so each measure is evaluated once, and use DIVIDE so a missing or zero prior value does not cause an error.
Sales YoY % =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales PY]
RETURN
DIVIDE ( CurrentSales - PriorSales, PriorSales )DIVIDE returns blank when the denominator is 0 or blank. A plain / would return infinity or an error instead. Set the measure's format to percentage in the Measure tools ribbon so 0.125 displays as 12.5%.
The flow below shows how one cell in a matrix is evaluated.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Growth shows blank for the first year | No prior-year data exists | Expected. Do not replace it with 0 |
| Growth is wrong at the month level but right at the year level | Gaps in the date table | Rebuild the date table with one row per day |
| Percentage shows as 0.125 | Measure not formatted | Apply percentage format to the measure |
| Error or infinity | Used `/` instead of DIVIDE | Switch to DIVIDE |
| Values appear beyond the last sale date | Prior-year values exist for future dates | Blank future periods as covered in a later chapter |
Exercise
A product launched this year, so Sales PY is blank for it. Using the measures above, what do Sales YoY Change and Sales YoY % return for that product, and why?
Show answer
Sales YoY Change returns the full current sales amount, because DAX treats the blank prior value as 0 in subtraction. Sales YoY % returns blank, because DIVIDE returns blank when the denominator is blank. To make the change measure blank as well, wrap it in IF ( NOT ISBLANK ( PriorSales ), CurrentSales - PriorSales ).
Exercise
Write a measure called Sales YoY % YTD that compares year-to-date sales with the prior year's year-to-date sales, using DIVIDE and variables.
Show answer
Sales YoY % YTD = VAR CurrentYTD = TOTALYTD ( [Total Sales], 'Date'[Date] ) VAR PriorYTD = CALCULATE ( TOTALYTD ( [Total Sales], 'Date'[Date] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) RETURN DIVIDE ( CurrentYTD - PriorYTD, PriorYTD )