DAX and data models
DAX Variables in Power BI: Complete Guide With Examples
A DAX variable stores a result once with VAR and reuses it after RETURN. Syntax, naming rules, examples, and why CALCULATE cannot change a variable.
Sajagan Thirugnanam
·
Updated
A DAX variable is a named result you define inside a measure or calculated column with VAR, and then use in the expression after RETURN. DAX calculates the variable once, where you define it, and reuses that value everywhere you reference it. Variables make long measures easier to read and debug, and they are faster when they replace an expression that would otherwise be calculated more than once.
What Are DAX Variables in Power BI?
A variable stores the result of an expression under a name. Instead of writing the same logic several times, you write it once and refer to it by name.
Measure Name =
VAR VariableName = <expression>
RETURN
<final expression>Key Parts:
VAR defines a variable. You can define as many as you need, one after another.
RETURN gives the result of the whole formula. A formula that defines variables must end with RETURN.
Variable names follow a few rules. They can use letters and numbers, cannot start with a number, and cannot contain spaces or other special characters. A double underscore is allowed as a prefix. They cannot be a reserved word or the name of a table in the model. So VAR Sales = ... fails in a model that has a Sales table. Use VAR TotalSales = ... instead.
Why Use Variables in DAX?
Readability. A well-named variable explains what each step calculates.
Performance. An expression stored in a variable is calculated once, however many times you reference it.
Debugging. You can return any single variable to check its value.
Fewer logic mistakes. A step defined once cannot drift out of step with a copy of itself elsewhere in the formula.
DAX Variables vs Measures: What's the Difference?
Measure
Exists in the model
Can be reused in any visual and by other measures
Is evaluated in the filter context of each cell it appears in
Variable
Exists only inside the one formula that defines it
Cannot be referenced from another measure
Holds a fixed value once calculated
A variable is a temporary value inside one measure. When the same logic is needed in several measures, make it a measure of its own. The measures vs calculated columns post covers where each kind of calculation belongs.
Simple Example of DAX Variables
Without a variable:
Total Sales = SUM ( Sales[SalesAmount] )With a variable, for demonstration only:
Total Sales =
VAR TotalSalesAmount = SUM ( Sales[SalesAmount] )
RETURN
TotalSalesAmountIn a one-line measure the variable adds nothing. It starts to pay off when a formula has several steps.
Real Example: Using Variables to Avoid Repeating Logic
A profit margin without variables:
Profit Margin % =
DIVIDE (
SUM ( Sales[Profit] ),
SUM ( Sales[SalesAmount] )
)The same measure with variables:
Profit Margin % =
VAR TotalSales = SUM ( Sales[SalesAmount] )
VAR TotalProfit = SUM ( Sales[Profit] )
RETURN
DIVIDE ( TotalProfit, TotalSales )Each step has a name, so the RETURN line reads like the business rule. Adding a condition later, such as returning blank when sales are zero, means changing one line.
Example: Using Variables With IF Logic
Sales Performance =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
IF ( SalesAmount > 100000, "High", "Low" )To add a Medium band, the variable is referenced twice but calculated once:
Sales Performance =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
SWITCH (
TRUE (),
SalesAmount > 100000, "High",
SalesAmount > 50000, "Medium",
"Low"
)The SWITCH function guide explains the SWITCH ( TRUE (), ... ) pattern.
Example: DAX Variables With CALCULATE
Year-over-year growth needs current sales and prior-year sales, and uses the prior-year figure twice:
Sales YoY Growth % =
VAR CurrentSales = SUM ( Sales[SalesAmount] )
VAR LastYearSales =
CALCULATE (
SUM ( Sales[SalesAmount] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)
RETURN
DIVIDE ( CurrentSales - LastYearSales, LastYearSales )Without variables, the CALCULATE expression would appear twice, once in the numerator and once in the denominator. SAMEPERIODLASTYEAR needs a proper date table. The time intelligence guide covers that setup.
Important Rule: Variables Are Evaluated Once
A variable is calculated once and its result is reused:
Double Sales =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
SalesAmount + SalesAmountSUM ( Sales[SalesAmount] ) runs once, not twice. This is where the performance benefit comes from. Microsoft's own example of a YoY measure rewritten with a variable runs in about half the query time.
Example: Using Variables for Dynamic Titles (Very Useful Trick)
Sales Title =
VAR SelectedRegion = SELECTEDVALUE ( Region[RegionName], "All Regions" )
RETURN
"Total Sales for " & SelectedRegionUse this measure as a visual title through conditional formatting (the fx button next to Title text). The title changes with the Region slicer. When more than one region is selected, SELECTEDVALUE returns the fallback text "All Regions".
Using Variables to Debug DAX Measures
Break a measure into variables, then return one of them to check its value:
Sales YoY Growth % =
VAR CurrentSales = SUM ( Sales[SalesAmount] )
VAR LastYearSales =
CALCULATE (
SUM ( Sales[SalesAmount] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)
VAR Growth = CurrentSales - LastYearSales
RETURN
-- DIVIDE ( Growth, LastYearSales )
LastYearSalesThe original RETURN line is commented out with --, so you can restore it when the check is done.
Common Mistakes When Using DAX Variables
Forgetting RETURN
This fails:
Result =
VAR X = 10
X + 5This works:
Result =
VAR X = 10
RETURN
X + 5Thinking Variables Work Like Excel Cells
In Excel, a cell recalculates when its inputs change. A DAX variable does not. It is calculated once, in the filter context where it is defined, and after that it is a fixed value. Wrapping it in CALCULATE later does nothing:
Blue Sales Wrong =
VAR TotalSales = SUM ( Sales[SalesAmount] )
RETURN
CALCULATE ( TotalSales, 'Product'[Color] = "Blue" )This returns sales for all colors. TotalSales was already calculated before CALCULATE added the Blue filter. To apply a filter, put CALCULATE inside the variable, or reference a measure, which is evaluated in the new filter context:
Blue Sales =
VAR BlueSales =
CALCULATE ( SUM ( Sales[SalesAmount] ), 'Product'[Color] = "Blue" )
RETURN
BlueSalesThe same variable does return different values in different cells of a visual. Each cell runs the whole measure again in its own filter context. Within one evaluation, the value is fixed. The CALCULATE guide explains how filter context is modified.
Defining Variables but Not Using Them
This often happens after copying code between measures. An unused variable is dead code that makes the measure harder to read. Remove it.
Best Practices for Writing DAX Variables
Use Clear Variable Names
Unclear:
VAR X = SUM ( Sales[SalesAmount] )Clear:
VAR TotalSales = SUM ( Sales[SalesAmount] )Group Variables in Logical Order
Define base values first, then the calculations that use them:
Profit =
VAR TotalSales = SUM ( Sales[SalesAmount] )
VAR TotalCost = SUM ( Sales[Cost] )
VAR ProfitAmount = TotalSales - TotalCost
RETURN
ProfitAmountA variable can only refer to variables defined above it.
Keep Measures Clean, Not Overcomplicated
Variables help, but a measure with a long chain of them is often doing work that belongs in the model or in separate measures. If a step is useful on its own, make it a measure.
Can You Use Variables in Calculated Columns?
Yes. This calculated column in the Sales table reads a value from the related Customer table and labels each sale:
Customer Segment =
VAR CustomerSpend = RELATED ( Customer[TotalSpend] )
RETURN
IF ( CustomerSpend > 5000, "VIP", "Regular" )RELATED works here because each sale has one customer. Variables are also the modern replacement for EARLIER in calculated columns, because a variable captures the current row's value before a FILTER starts a new iteration.
Do Variables Improve Performance in Power BI?
They do when they replace an expression that would otherwise be evaluated more than once. Typical cases:
the same aggregation appears several times in one formula
an expensive CALCULATE result is used in both the numerator and denominator
a value is compared against several thresholds
A variable referenced only once gives no speed benefit, only readability. Variables also do not fix a slow model. The DAX performance guide covers what to check when the model is the problem.
When Should You Use DAX Variables?
The measure has more than one step.
The same expression appears more than once.
The measure has several IF or SWITCH conditions on one value.
You use CALCULATE with results you need again later in the formula.
You need to debug a complex measure.
Final Thoughts
Take the longest measure in your model and split it into named variables, one per step. Then return each variable in turn and check its value in a table visual. That one exercise usually finds a bug or a repeated expression.
FAQ
What is VAR in DAX?
VAR is the keyword that defines a variable inside a DAX measure, calculated column or query. It stores the result of an expression under a name, which the formula then uses in the expression after RETURN.
Do DAX variables improve performance?
Yes, when the variable replaces an expression that would otherwise be evaluated more than once. DAX calculates the variable once and reuses the result. A variable that is referenced only once helps readability, not speed.
Can I use multiple variables in one DAX measure?
Yes. Define them one after another, each with its own VAR, before the single RETURN. Each variable can use the ones defined above it.
Can I reuse a DAX variable across measures?
No. A variable exists only inside the formula where it is defined. For logic you need in several measures, create a separate measure and reference it.
Sources
VAR keyword (DAX) - Microsoft Learn
Use variables to improve your DAX formulas - Microsoft Learn
A DAX variable is a named result you define inside a measure or calculated column with VAR, and then use in the expression after RETURN. DAX calculates the variable once, where you define it, and reuses that value everywhere you reference it. Variables make long measures easier to read and debug, and they are faster when they replace an expression that would otherwise be calculated more than once.
What Are DAX Variables in Power BI?
A variable stores the result of an expression under a name. Instead of writing the same logic several times, you write it once and refer to it by name.
Measure Name =
VAR VariableName = <expression>
RETURN
<final expression>Key Parts:
VAR defines a variable. You can define as many as you need, one after another.
RETURN gives the result of the whole formula. A formula that defines variables must end with RETURN.
Variable names follow a few rules. They can use letters and numbers, cannot start with a number, and cannot contain spaces or other special characters. A double underscore is allowed as a prefix. They cannot be a reserved word or the name of a table in the model. So VAR Sales = ... fails in a model that has a Sales table. Use VAR TotalSales = ... instead.
Why Use Variables in DAX?
Readability. A well-named variable explains what each step calculates.
Performance. An expression stored in a variable is calculated once, however many times you reference it.
Debugging. You can return any single variable to check its value.
Fewer logic mistakes. A step defined once cannot drift out of step with a copy of itself elsewhere in the formula.
DAX Variables vs Measures: What's the Difference?
Measure
Exists in the model
Can be reused in any visual and by other measures
Is evaluated in the filter context of each cell it appears in
Variable
Exists only inside the one formula that defines it
Cannot be referenced from another measure
Holds a fixed value once calculated
A variable is a temporary value inside one measure. When the same logic is needed in several measures, make it a measure of its own. The measures vs calculated columns post covers where each kind of calculation belongs.
Simple Example of DAX Variables
Without a variable:
Total Sales = SUM ( Sales[SalesAmount] )With a variable, for demonstration only:
Total Sales =
VAR TotalSalesAmount = SUM ( Sales[SalesAmount] )
RETURN
TotalSalesAmountIn a one-line measure the variable adds nothing. It starts to pay off when a formula has several steps.
Real Example: Using Variables to Avoid Repeating Logic
A profit margin without variables:
Profit Margin % =
DIVIDE (
SUM ( Sales[Profit] ),
SUM ( Sales[SalesAmount] )
)The same measure with variables:
Profit Margin % =
VAR TotalSales = SUM ( Sales[SalesAmount] )
VAR TotalProfit = SUM ( Sales[Profit] )
RETURN
DIVIDE ( TotalProfit, TotalSales )Each step has a name, so the RETURN line reads like the business rule. Adding a condition later, such as returning blank when sales are zero, means changing one line.
Example: Using Variables With IF Logic
Sales Performance =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
IF ( SalesAmount > 100000, "High", "Low" )To add a Medium band, the variable is referenced twice but calculated once:
Sales Performance =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
SWITCH (
TRUE (),
SalesAmount > 100000, "High",
SalesAmount > 50000, "Medium",
"Low"
)The SWITCH function guide explains the SWITCH ( TRUE (), ... ) pattern.
Example: DAX Variables With CALCULATE
Year-over-year growth needs current sales and prior-year sales, and uses the prior-year figure twice:
Sales YoY Growth % =
VAR CurrentSales = SUM ( Sales[SalesAmount] )
VAR LastYearSales =
CALCULATE (
SUM ( Sales[SalesAmount] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)
RETURN
DIVIDE ( CurrentSales - LastYearSales, LastYearSales )Without variables, the CALCULATE expression would appear twice, once in the numerator and once in the denominator. SAMEPERIODLASTYEAR needs a proper date table. The time intelligence guide covers that setup.
Important Rule: Variables Are Evaluated Once
A variable is calculated once and its result is reused:
Double Sales =
VAR SalesAmount = SUM ( Sales[SalesAmount] )
RETURN
SalesAmount + SalesAmountSUM ( Sales[SalesAmount] ) runs once, not twice. This is where the performance benefit comes from. Microsoft's own example of a YoY measure rewritten with a variable runs in about half the query time.
Example: Using Variables for Dynamic Titles (Very Useful Trick)
Sales Title =
VAR SelectedRegion = SELECTEDVALUE ( Region[RegionName], "All Regions" )
RETURN
"Total Sales for " & SelectedRegionUse this measure as a visual title through conditional formatting (the fx button next to Title text). The title changes with the Region slicer. When more than one region is selected, SELECTEDVALUE returns the fallback text "All Regions".
Using Variables to Debug DAX Measures
Break a measure into variables, then return one of them to check its value:
Sales YoY Growth % =
VAR CurrentSales = SUM ( Sales[SalesAmount] )
VAR LastYearSales =
CALCULATE (
SUM ( Sales[SalesAmount] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)
VAR Growth = CurrentSales - LastYearSales
RETURN
-- DIVIDE ( Growth, LastYearSales )
LastYearSalesThe original RETURN line is commented out with --, so you can restore it when the check is done.
Common Mistakes When Using DAX Variables
Forgetting RETURN
This fails:
Result =
VAR X = 10
X + 5This works:
Result =
VAR X = 10
RETURN
X + 5Thinking Variables Work Like Excel Cells
In Excel, a cell recalculates when its inputs change. A DAX variable does not. It is calculated once, in the filter context where it is defined, and after that it is a fixed value. Wrapping it in CALCULATE later does nothing:
Blue Sales Wrong =
VAR TotalSales = SUM ( Sales[SalesAmount] )
RETURN
CALCULATE ( TotalSales, 'Product'[Color] = "Blue" )This returns sales for all colors. TotalSales was already calculated before CALCULATE added the Blue filter. To apply a filter, put CALCULATE inside the variable, or reference a measure, which is evaluated in the new filter context:
Blue Sales =
VAR BlueSales =
CALCULATE ( SUM ( Sales[SalesAmount] ), 'Product'[Color] = "Blue" )
RETURN
BlueSalesThe same variable does return different values in different cells of a visual. Each cell runs the whole measure again in its own filter context. Within one evaluation, the value is fixed. The CALCULATE guide explains how filter context is modified.
Defining Variables but Not Using Them
This often happens after copying code between measures. An unused variable is dead code that makes the measure harder to read. Remove it.
Best Practices for Writing DAX Variables
Use Clear Variable Names
Unclear:
VAR X = SUM ( Sales[SalesAmount] )Clear:
VAR TotalSales = SUM ( Sales[SalesAmount] )Group Variables in Logical Order
Define base values first, then the calculations that use them:
Profit =
VAR TotalSales = SUM ( Sales[SalesAmount] )
VAR TotalCost = SUM ( Sales[Cost] )
VAR ProfitAmount = TotalSales - TotalCost
RETURN
ProfitAmountA variable can only refer to variables defined above it.
Keep Measures Clean, Not Overcomplicated
Variables help, but a measure with a long chain of them is often doing work that belongs in the model or in separate measures. If a step is useful on its own, make it a measure.
Can You Use Variables in Calculated Columns?
Yes. This calculated column in the Sales table reads a value from the related Customer table and labels each sale:
Customer Segment =
VAR CustomerSpend = RELATED ( Customer[TotalSpend] )
RETURN
IF ( CustomerSpend > 5000, "VIP", "Regular" )RELATED works here because each sale has one customer. Variables are also the modern replacement for EARLIER in calculated columns, because a variable captures the current row's value before a FILTER starts a new iteration.
Do Variables Improve Performance in Power BI?
They do when they replace an expression that would otherwise be evaluated more than once. Typical cases:
the same aggregation appears several times in one formula
an expensive CALCULATE result is used in both the numerator and denominator
a value is compared against several thresholds
A variable referenced only once gives no speed benefit, only readability. Variables also do not fix a slow model. The DAX performance guide covers what to check when the model is the problem.
When Should You Use DAX Variables?
The measure has more than one step.
The same expression appears more than once.
The measure has several IF or SWITCH conditions on one value.
You use CALCULATE with results you need again later in the formula.
You need to debug a complex measure.
Final Thoughts
Take the longest measure in your model and split it into named variables, one per step. Then return each variable in turn and check its value in a table visual. That one exercise usually finds a bug or a repeated expression.
FAQ
What is VAR in DAX?
VAR is the keyword that defines a variable inside a DAX measure, calculated column or query. It stores the result of an expression under a name, which the formula then uses in the expression after RETURN.
Do DAX variables improve performance?
Yes, when the variable replaces an expression that would otherwise be evaluated more than once. DAX calculates the variable once and reuses the result. A variable that is referenced only once helps readability, not speed.
Can I use multiple variables in one DAX measure?
Yes. Define them one after another, each with its own VAR, before the single RETURN. Each variable can use the ones defined above it.
Can I reuse a DAX variable across measures?
No. A variable exists only inside the formula where it is defined. For logic you need in several measures, create a separate measure and reference it.
Sources
VAR keyword (DAX) - Microsoft Learn
Use variables to improve your DAX formulas - Microsoft Learn
Show us your report nobody trusts.
1 · A 30-minute call.
2 · We look at your current reports together.
3 · We tell you what we would do.
Show us your report nobody trusts.
1 · A 30-minute call.
2 · We look at your current reports together.
3 · We tell you what we would do.
Show us your report nobody trusts.
1 · A 30-minute call.
2 · We look at your current reports together.
3 · We tell you what we would do.
CaseWhen is a Berlin BI consultancy that builds reporting that leaders can trust, on the Microsoft stack: Power BI, Fabric and Azure.
Berlin, Germany
Free tools
CaseWhen is a Berlin BI consultancy that builds reporting that leaders can trust, on the Microsoft stack: Power BI, Fabric and Azure.
Berlin, Germany
Free tools
CaseWhen is a Berlin BI consultancy that builds reporting that leaders can trust, on the Microsoft stack: Power BI, Fabric and Azure.
Berlin, Germany
