Using Variables in DAX Queries and Measures
In this article, we will learn how to use VAR and
RETURN in DAX to store intermediate calculations,
simplify formulas, and create a reusable sales tax measure.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist
What Are Variables in DAX?
Variables in DAX allow you to store the result of an expression and
reuse it later in the same calculation. They make complex formulas
easier to read, understand, and maintain.
Variables are created using VAR and the final result
is returned using RETURN.
VAR VariableName = Expression
RETURN
ResultExpression
Understanding VAR and RETURN
VAR: Creates a variable and stores the result of an
expression.
RETURN: Specifies the final expression whose result
will be returned.
Variables help you break a calculation into smaller, meaningful steps
instead of writing one long formula.
1. Using Variables in a DAX Query
Query
EVALUATE
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 18
VAR TotalTaxAmount = TotalSalesAmount * (TaxAmount/100)
RETURN
ROW(
"Total Tax", TotalTaxAmount
)
Step-by-step explanation
Step 1: EVALUATE
EVALUATE tells DAX to return the result of a table
expression. In this query, the final result is created using
ROW.
Step 2: VAR TotalSalesAmount
TotalSalesAmount stores the result of
SUM(Sales[Sales Amount]).
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
This calculates the total sales amount from the
Sales[Sales Amount] column.
Step 3: VAR TaxAmount
TaxAmount stores the value 18,
representing an 18% tax rate.
VAR TaxAmount = 18
Step 4: VAR TotalTaxAmount
This variable calculates the tax amount by multiplying the total sales
by the tax rate divided by 100.
VAR TotalTaxAmount = TotalSalesAmount * (TaxAmount/100)
The calculation is:
Total Tax = Total Sales × (18 / 100)
Step 5: RETURN
RETURN specifies the final expression that DAX should
evaluate and return.
Step 6: ROW
ROW creates a table containing one row and one column.
The output column is named Total Tax.
ROW(
"Total Tax", TotalTaxAmount
)
What will you get?
The query returns a single-row table containing the calculated tax amount.
| Total Tax |
|---|
| 18% of Total Sales |
The exact value depends on the total sales amount in your
Sales table.
The query uses three variables to calculate the tax and then returns
the result as a table using ROW.
2. Creating a Measure Using Variables
Measure
Total Tax Amount =
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 18
RETURN TotalSalesAmount * TaxAmount
Step-by-step explanation
Step 1: Measure name
Total Tax Amount is the name of the measure.
Step 2: VAR TotalSalesAmount
This variable calculates the total sales amount using
SUM(Sales[Sales Amount]).
Step 3: VAR TaxAmount
This variable stores the tax rate as 18.
Step 4: RETURN
The RETURN statement specifies the final calculation
that the measure should return.
RETURN TotalSalesAmount * TaxAmount
Important: Correcting the Tax Calculation
The measure above multiplies the total sales amount by
18, not 18%.
To calculate an 18% tax amount, the tax rate must be divided by 100.
The corrected measure is:
Total Tax Amount =
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 18
RETURN TotalSalesAmount * (TaxAmount/100)
This ensures that the measure calculates 18% of Total Sales.
When a percentage is stored as a whole number such as 18,
divide it by 100 before multiplying it with the amount.
3. Understanding the Difference Between the Query and Measure
| Feature | DAX Query | DAX Measure |
|---|---|---|
| Purpose | Returns a result for a query | Creates a reusable calculation |
| Uses VAR | Yes | Yes |
| Uses RETURN | Yes | Yes |
| Final result | Table expression | Scalar value |
| Example | EVALUATE ROW(…) | Total Tax Amount = … |
4. Why Should You Use Variables?
1. Improved readability
Variables give meaningful names to intermediate calculations,
making the formula easier to understand.
2. Easier maintenance
If you need to change the tax rate, you can update the value of
TaxAmount in one place.
3. Reuse intermediate results
A variable can be used multiple times in the final calculation,
avoiding repeated expressions.
4. Simplifies complex calculations
Instead of writing a long formula, you can divide the calculation
into smaller steps using multiple variables.
📝 Practice Exercise
Try creating a DAX measure to calculate 10% Tax
on the total sales amount.
Use the following steps:
- Create a variable for total sales.
- Create a variable for the tax rate.
- Calculate the tax amount.
- Return the final result.
Expected Measure
Total Tax Amount =
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 10
RETURN TotalSalesAmount * (TaxAmount/100)
Conclusion
In this article, we learned how to use variables in DAX queries
and measures.
- VAR creates a variable and stores an expression result.
- RETURN specifies the final expression.
- Variables make DAX formulas easier to read and maintain.
- EVALUATE returns the result of a table expression.
- ROW creates a single-row table for the query result.
- Measures return a scalar value and can be reused in reports.
- Percentage calculations should divide the rate by 100.
Variables are an essential part of writing clean and efficient DAX
calculations. Once you understand VAR and
RETURN, you can start building more advanced
business calculations with confidence.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist