Sub topic 5
Variables and DIVIDE for readable, safe measures
This chapter assumes you can write basic DAX measures with SUM and have seen DIVIDE used once. It covers two tools that nearly every time intelligence measure in this tutorial uses: variables (VAR and RETURN) and DIVIDE.
Why growth measures need both
A year-over-year growth measure compares two numbers: this period's sales and the prior period's sales. Written without variables, each number appears twice, and the division can fail when the prior period has no sales.
Sales YoY % (fragile) =
([Total Sales] - [Sales PY]) / [Sales PY]This measure has two problems:
- It repeats
[Sales PY], so the reader has to compare both copies to be sure they match. - When
[Sales PY]is blank or zero, the/operator returnsInfinityorNaNinstead of a blank cell. These values show up in matrices and break chart axes.
Variables with VAR and RETURN
A variable stores the result of an expression under a name. You declare it with VAR and finish the measure with RETURN.
Sales YoY % =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales PY]
RETURN
DIVIDE ( CurrentSales - PriorSales, PriorSales )Variables give you three things:
- Readability.
CurrentSales - PriorSalessays what the calculation does. - Single evaluation. DAX calculates each variable once, even if you use it several times.
- Easy debugging. To check a value, temporarily change
RETURNto returnPriorSalesalone.
DIVIDE
DIVIDE ( numerator, denominator [, alternate] ) returns the alternate result when the denominator is zero or blank. If you leave out the third argument, the alternate result is blank. A blank cell is usually what you want, because visuals skip blanks instead of drawing a misleading point.
| Prior-year sales | Current sales | `/` operator | `DIVIDE` |
|---|---|---|---|
| 100 | 120 | 0.2 | 0.2 |
| 0 | 120 | Infinity | blank |
| blank | 120 | Infinity | blank |
Use the third argument only when a number is the correct answer. For a growth percentage, 0 would claim "no change", which is wrong when there was nothing to compare against. Blank is the honest result.
Variables keep the context where they are defined
A variable is calculated in the filter context that exists at the line where you declare it. After that its value is fixed. Wrapping a variable in CALCULATE does not recalculate it.
Sales PY (wrong) =
VAR CurrentSales = [Total Sales]
RETURN
CALCULATE ( CurrentSales, SAMEPERIODLASTYEAR ( 'Date'[Date] ) )This measure returns the current period's sales. CurrentSales was already a number before CALCULATE ran, so the date shift has nothing to change. Put the expression that must be re-evaluated inside CALCULATE, as in CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ).
The same rule works in your favor. The moving-average measures later in this tutorial declare LastDate = MAX ( 'Date'[Date] ) first. That variable keeps the last visible date even when a later step changes the date filter.
Common errors
- Putting
RETURNbefore the last variable. EveryVARmust come beforeRETURN. - Using a variable name that matches a column or table name. This makes the measure hard to read. Use names like
CurrentSalesrather thanSales. - Using
/in a measure that visuals will show for every date. Some dates will have no prior-year data, and you will seeInfinityin the visual.
Exercise
A measure is written as VAR Total = [Total Sales] RETURN CALCULATE ( Total, DATESYTD ( 'Date'[Date] ) ). In a matrix by month, it shows each month's own sales, not the year-to-date total. Why, and how do you fix it?
Show answer
The variable Total is calculated in the matrix's month filter context before CALCULATE runs, so it is already a fixed number. DATESYTD cannot change it. Remove the variable from the CALCULATE call and write CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) ), so [Total Sales] is evaluated in the year-to-date context.