Evaluation Context: Row Context vs Filter Context
Overview & Learning Objectives
As business logic grows complex, DAX measures can become sprawling, unreadable formulas. DAX Variables (`VAR ... RETURN`) transform your code: they allow you to break down multi-step calculations into named variables, dramatically improve readability, and boost calculation speed by evaluating an expression exactly once and reusing the cached result.
- Master the syntax of `VAR` and `RETURN` in DAX expressions.
- Eliminate duplicate calculations by caching intermediate results in variables.
- Understand how variable scoping freezes evaluation context at definition time.
Industry Standard
Standard: Enterprise DAX code reviews at Amazon and Deloitte require any measure exceeding 3 lines or repeating a calculation to use `VAR ... RETURN` structures.
Real-World Example: FreshBake Artisan Patisserie
FreshBake calculates YoY Growth %: `([Current Sales] - [Prior Sales]) / [Prior Sales]`. Without variables, `[Prior Sales]` is calculated twice.
| Approach | DAX Formulation | Evaluations of Prior Sales | Execution Efficiency |
|---|---|---|---|
| Without VAR | DIVIDE([Sales] - [Prior Sales], [Prior Sales]) | Calculated TWICE | Slower (redundant calculation) |
| With VAR ... RETURN | VAR CurrentSales = [Sales] VAR PriorSales = [Prior Sales] RETURN DIVIDE(CurrentSales - PriorSales, PriorSales) | Calculated ONCE (Cached) | Fast & High-Performance |
Syntax & Core Workflow
YoY Growth % =
VAR CurrentSales = [Total Sales]
VAR PriorSales = CALCULATE([Total Sales], SAMEPERIODLASTYEAR(Dim_Date[Date]))
RETURN
DIVIDE(CurrentSales - PriorSales, PriorSales)
Parameters Breakdown
VAR: Defines a local named variable holding an intermediate scalar number, text, or virtual table.RETURN: Terminates the variable definition block and returns the final evaluated result.Constant Scoping: A variable evaluates in the context where it is defined and remains static throughout the measure.
Sample Walkthrough (Q&A)
Question: Do DAX variables recalculate every time they are referenced inside the RETURN block?
Formula / Action: No; variables in DAX are constants evaluated once at definition time.
Result: The computed value is stored and reused without recalculation.
Explanation: In DAX, `VAR` acts like a constant. Once computed, its value is locked in memory for the duration of that specific query, preventing redundant CPU cycles.
Common Mistakes & Limitations
- Defining a variable outside a `CALCULATE()` statement and expecting it to respond to the modified filter context inside `CALCULATE()`.
- Forgetting the `RETURN` keyword after defining variables, causing a syntax error.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Auto-format DAX formula | Use DAX Beautifier / Shift + Enter for indentation | Shift + Enter |
| Comment line in DAX | Ctrl + / | Cmd + / |
Pro Tips & Modern Alternatives
- You can return intermediate variables during debugging by temporarily changing `RETURN FinalResult` to `RETURN MyVariable`.
- Variables can hold entire virtual tables (e.g. `VAR TopCustomers = TOPN(...)`), which can then be passed to iterator functions.