Power BI DAX Variables

Variables let a DAX formula store a value under a name and reuse it. They shorten long formulas, speed up calculations, and make code readable. Professional DAX authors use variables in almost every non-trivial measure.

The Scratchpad Analogy

Solving a long math problem on paper goes faster when you jot intermediate answers on a scratchpad. You write the subtotal once and look at it whenever needed. A DAX variable plays the role of that scratchpad note.

VAR  Sales2026 = ...   -- note 1 on the scratchpad
VAR  Sales2025 = ...   -- note 2 on the scratchpad
RETURN
     Sales2026 - Sales2025   -- final answer uses the notes

Syntax

Measure Name =
VAR VariableName = <expression>
RETURN
    <expression that uses VariableName>

A formula can hold several VAR lines before a single RETURN. Each variable can use the ones declared above it.

A Simple Example: Profit Margin

Without variables, the formula repeats the same calculation:

Margin Long =
DIVIDE(SUM(Sales[Revenue]) - SUM(Sales[Cost]), SUM(Sales[Revenue]))

With variables, the formula reads like a sentence:

Margin =
VAR Revenue = SUM(Sales[Revenue])
VAR Cost = SUM(Sales[Cost])
VAR Profit = Revenue - Cost
RETURN
    DIVIDE(Profit, Revenue)

Anyone who opens this measure understands the logic in seconds.

Benefits of Variables

Readability

Descriptive names replace nested functions. Reviewers follow the logic from top to bottom.

Performance

DAX evaluates each variable once and reuses the result. A formula that repeats an expensive calculation three times runs it only once when stored in a variable.

Easier Debugging

Replace the RETURN expression with a variable name to check that variable alone. Test each note on the scratchpad before trusting the final answer.

RETURN Profit   -- temporary check

Variables Freeze Their Context

A variable evaluates in the filter context where the VAR line sits. Later changes to context inside RETURN do not alter the stored value. This behavior supports comparison measures:

Share of Total =
VAR CurrentSales = SUM(Sales[Revenue])
VAR AllSales = CALCULATE(SUM(Sales[Revenue]), ALL(Sales))
RETURN
    DIVIDE(CurrentSales, AllSales)
Context in visual: Region = West
CurrentSales --> 250      (frozen with the West filter)
AllSales     --> 1000     (computed with filters removed)
Result            25%

Using Variables with Conditions

Sales Status =
VAR Actual = SUM(Sales[Revenue])
VAR Target = SUM(Targets[Amount])
RETURN
    IF(Actual >= Target, "On Track", "Behind")

The text result suits a card visual or conditional formatting rule.

Worked Example: Year-over-Year Growth

YoY Growth =
VAR ThisYear = SUM(Sales[Revenue])
VAR LastYear = CALCULATE(SUM(Sales[Revenue]), SAMEPERIODLASTYEAR('Date'[Date]))
RETURN
    DIVIDE(ThisYear - LastYear, LastYear)

The formula stores both years once and performs a single division. Authors read the logic straight from the variable names. A model with a marked date table supports this pattern.

Naming Tips

  • Choose names that describe the value, such as TotalCost.
  • Avoid names that match table or column names.
  • Keep variable names without spaces, since spaces force extra quoting.
  • Use one VAR per line with matching indentation.

Key Points

  • VAR stores a value, and RETURN delivers the final result.
  • Variables shorten formulas and improve speed.
  • Each variable evaluates once in the context where it sits.
  • Return a single variable to debug it.
  • Clear names turn complex DAX into readable steps.