Stop 07 · DAX FundamentalsTopics · 10/10

Variables in DAX (VAR and RETURN)

Part of the Power BI Roadmap

In short

Long measures that repeat the same CALCULATE three times are slow to run and painful to read. Variables let you name each piece once, then combine them at the end.

VAR stores an intermediate result inside a measure and RETURN gives the final value. Variables make DAX easier to read, debug and often faster.

The jaadu sits at an exam desk jotting small labelled answers in the margin of a page, then circles one big final answer at the bottom.

Think of it like this

Rough work in the margin

In a maths exam you write intermediate answers in the margin: profit = 40, cost = 160. Then the final line uses them. VAR is the rough work, and RETURN is the final answer you underline.

Watch it happen

Building a growth measure step by step

enqueue(Sales = 12 L)
Sales = 12 L

Front of the queue is coloured

1/5VAR Sales stores this period's sales once.

The big ideas

What you need to know

VAR name = expression

Each VAR stores one value or table. Add as many as you need, then one RETURN.

Calculated once

A variable used three times is computed once, which can make measures faster.

Easy debugging

Temporarily RETURN a single variable to check its value in a card.

Fixed where defined

A variable is evaluated in the context where it's defined. CALCULATE later in the measure can't change it.

See it in code

Walk through the code

dax
1Sales Growth % =2VAR CurrentSales = [Total Sales]3VAR PrevYearSales =4    CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )5VAR Diff = CurrentSales - PrevYearSales6RETURN7    IF (8        NOT ISBLANK ( PrevYearSales ),9        DIVIDE ( Diff, PrevYearSales )10    )

1/4Sales in the current filter, stored once.

Year-on-year growth with variables. It needs a marked Date table related to Sales.

Save yourself the pain

Common mistakes

Oops

Writing VAR s = [Total Sales] and then CALCULATE ( s, 'Product'[Category] = "Snacks" ), expecting snack sales.

Do this instead

s was already calculated before CALCULATE runs, so the filter can't change it. Put the measure itself inside CALCULATE.

Oops

Repeating the same CALCULATE expression three times in one measure.

Do this instead

Store it in a VAR once and reuse the name.

Your mission

Refactor with variables

~20 min

Quick check

VAR x = [Total Sales] RETURN CALCULATE ( x, Store[City] = "Pune" ). What does this return?

Go deeper

  1. 1Rewrite a growth % measure using VAR for current and previous values.
  2. 2Debug a measure by temporarily returning one variable.
  3. 3Use variables in every measure longer than one line.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge