TutorialsPower BIVariables in DAX (VAR)

Variables in DAX (VAR)

Use VAR to write cleaner, faster, and easier-to-debug DAX formulas

Variables in DAX (introduced with VAR...RETURN syntax) make complex DAX formulas dramatically more readable and slightly faster. Before variables, complex measures required nested functions that were hard to read and debug. With variables, you store intermediate results and refer to them by name. Variables in DAX are not the same as variables in programming languages — they are evaluated once and are immutable (cannot change within the formula). But they are still one of the most powerful quality-of-life improvements in modern DAX.

Example

VAR...RETURN — before and after
WITHOUT VARIABLES (hard to read):
YoY Growth % =
  DIVIDE(
    SUM(Sales[Amount]) -
    CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Date[Date])),
    CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Date[Date]))
  )

WITH VARIABLES (clean and readable):
YoY Growth % =
  VAR CurrentSales = SUM(Sales[Amount])
  VAR LastYearSales =
    CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Date[Date]))
  RETURN
    DIVIDE(CurrentSales - LastYearSales, LastYearSales)

Benefits:
  1. LastYearSales is calculated ONCE (faster)
  2. Formula reads like pseudocode
  3. Easy to debug — change RETURN to a variable to inspect its value
💡 During debugging, change "RETURN DIVIDE(...)" to "RETURN LastYearSales" to check the intermediate value in a visual.

Key Points

  • VAR names are local — they only exist within the measure where defined
  • Variables are evaluated once — even if referenced multiple times in RETURN
  • This makes SUMX(table, VAR x = ... RETURN x * 2) technically possible but unusual
  • RETURN is required at the end — it is what the measure actually outputs
  • Use VAR for any measure with a repeated sub-expression

Practice Question

In a DAX measure using VAR...RETURN syntax, what does the RETURN statement do?