DAX variables: VAR and RETURN with examples

How VAR and RETURN work in DAX, why variables make measures faster, easier to read and easier to debug, that a variable is evaluated once where it is defined, table variables, the CALCULATE trap, and examples for growth, ranking and filters.

Short answer: VAR names a value or a table inside a formula, and RETURN gives the result. A variable is calculated once, where it is defined, in that context, and then used as a constant. That makes formulas faster (no repeated calculation), easier to read (each step has a name) and easier to debug (return a variable to see its value). The trap: CALCULATE ( MyVar, ... ) does not recalculate a variable; it is already a fixed value.

A growth measure without variables repeats the last-year calculation three times. With variables it is calculated once, named, and the formula reads like the sentence you would say.

What does a measure with variables look like?

Revenue Growth % =
VAR Current = [Revenue]
VAR LastYear = CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
VAR Growth = Current - LastYear
RETURN
    IF ( NOT ISBLANK ( LastYear ), DIVIDE ( Growth, LastYear ) )

Without variables the same formula is:

Revenue Growth % Old =
IF (
    NOT ISBLANK ( CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) ),
    DIVIDE (
        [Revenue] - CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ),
        CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
    )
)

Three copies of the same expression. The engine may cache some of it, but the variable version guarantees one evaluation, and a change is made in one place.

What are the rules?

Evaluated oncewhere VAR stands, in that row and filter context
Constant after thatlater CALCULATE or filters do not change it
Any typea number, a text, a date, or a whole table
Scopeonly inside the formula, after its definition
RETURNone expression, which may use all variables
How variables behave

Variables can be defined anywhere an expression is allowed, also inside a function argument, and their names cannot be table names or contain spaces.

How do table variables help?

A variable can hold a table, then be used by several functions:

Top Customers Revenue =
VAR TopCustomers =
    TOPN ( 10, VALUES ( Customer[Customer] ), [Revenue] )
RETURN
    CALCULATE ( [Revenue], TopCustomers )

Share of Top 10 =
VAR TopCustomers = TOPN ( 10, VALUES ( Customer[Customer] ), [Revenue] )
VAR TopRevenue = CALCULATE ( [Revenue], TopCustomers )
RETURN
    DIVIDE ( TopRevenue, [Revenue] )

TopCustomers is a filter table computed once, then applied with CALCULATE.

Why does CALCULATE not change a variable?

Wrong Last Year =
VAR Rev = [Revenue]
RETURN
    CALCULATE ( Rev, SAMEPERIODLASTYEAR ( 'Date'[Date] ) )

This returns this year's revenue. Rev was evaluated when it was defined, in the current filter context. CALCULATE changes the filter context for its expression, but the expression is a constant. Put the measure itself inside CALCULATE: CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ).

The same applies inside iterators: a variable defined outside SUMX has the same value on every row. That is what you want for a threshold, and wrong for a per-row value.

How do variables help debugging?

Return an intermediate variable to see it in a card or table, then switch back:

Revenue Growth % =
VAR Current = [Revenue]
VAR LastYear = CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
    LastYear -- temporarily, to check last year's value

In DAX query view you can EVALUATE a table variable with DEFINE and see its rows.

What goes wrong?

  • Variables as lazy formulas. They are values, not formulas; context changes after the definition do not affect them.
  • Defining a row value outside an iterator. It is the same for every row; define it inside the iterator's expression.
  • Too many tiny variables. Name steps that mean something; a variable per operator hurts reading.
  • Name clashes. A variable name cannot be the same as a table name.

Where can you check this?

Where do you start?

Rewrite your longest measure with one variable per step, and return each variable once to check it. BLANK handling covers the IF ( NOT ISBLANK ( ... ) ) part of the growth measure.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.