Using Variables in DAX to Calculate Total Tax and Average Tax
In this article, we will learn how to use DEFINE,
VAR, and EVALUATE in DAX to calculate
tax on total sales and average sales. We will also understand how the
same logic can be used when creating a measure.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist
What Are Variables in DAX?
Variables allow us to store the result of a calculation or a value and
reuse it later in the same DAX expression. They make DAX code easier to
read, maintain, and understand.
In this example, we will use variables to store the tax percentage,
total sales, and average sales.
1. Defining the Variables
Query
// Defining the Variables
DEFINE
VAR TaxPercent = 18
VAR TotalSales = SUM(Sales[Sales Amount])
VAR AverageSales = AVERAGE(Sales[Sales Amount])
Step-by-step explanation
Step 1: DEFINE
DEFINE is used to define variables that can be used
in the DAX query.
Step 2: VAR TaxPercent = 18
This variable stores the tax percentage.
Since the tax rate is 18%, the value stored in
TaxPercent is 18.
Step 3: VAR TotalSales = SUM(Sales[Sales Amount])
This variable calculates the total sales amount from the
Sales[Sales Amount] column.
Step 4: VAR AverageSales = AVERAGE(Sales[Sales Amount])
This variable calculates the average sales amount from the
Sales[Sales Amount] column.
Understanding the variables
| Variable | Calculation | Purpose |
|---|---|---|
| TaxPercent | 18 | Stores the tax percentage |
| TotalSales | SUM(Sales[Sales Amount]) | Stores the total sales amount |
| AverageSales | AVERAGE(Sales[Sales Amount]) | Stores the average sales amount |
Variables store values or calculation results that can be reused
throughout the DAX query.
2. Finding the Tax for Total Sales
Query
// Find the Tax for Total Sales
EVALUATE
ROW(
"Total Sales", TotalSales,
"Total Tax", TotalSales * (TaxPercent/100)
)
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the result of the table expression.
In this example, the table expression is ROW.
Step 2: ROW()
ROW creates a table containing one row.
Each pair of arguments represents a column name and its value.
Step 3: “Total Sales”, TotalSales
The first output column is named Total Sales.
Its value comes from the TotalSales variable.
Step 4: “Total Tax”, TotalSales * (TaxPercent/100)
The second output column is named Total Tax.
The formula calculates tax by multiplying total sales by the tax percentage.
Understanding the tax calculation
The formula used is:
Total Tax = TotalSales * (TaxPercent/100)
Since TaxPercent is 18, the calculation
becomes:
Total Tax = TotalSales * (18/100)
Total Tax = TotalSales * 0.18
What will you get?
The query returns one row containing the total sales and the tax
calculated on that total.
| Total Sales | Total Tax |
|---|---|
| Actual total sales amount | Total sales × 18% |
The actual values depend on the data available in your Sales table.
The TotalSales variable is reused to calculate
the tax without repeating the original SUM expression.
3. Finding the Tax for Average Sales
Query
// Find the Tax for Average Sales
EVALUATE
ROW(
"Average Sales", AverageSales,
"Average Tax", AverageSales * (TaxPercent/100)
)
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the result of the table expression.
Step 2: ROW()
ROW creates a table containing one row with the
calculated values.
Step 3: “Average Sales”, AverageSales
The first output column is named Average Sales.
Its value comes from the AverageSales variable.
Step 4: “Average Tax”, AverageSales * (TaxPercent/100)
The second output column is named Average Tax.
The formula calculates tax on the average sales amount.
Understanding the tax calculation
The formula used is:
Average Tax = AverageSales * (TaxPercent/100)
Since TaxPercent is 18, the calculation
becomes:
Average Tax = AverageSales * (18/100)
Average Tax = AverageSales * 0.18
What will you get?
The query returns one row containing the average sales and the tax
calculated on that average.
| Average Sales | Average Tax |
|---|---|
| Actual average sales amount | Average sales × 18% |
The actual values depend on the data available in your Sales table.
The same tax percentage can be reused to calculate tax on
different sales calculations.
4. Understanding the Complete Query
The complete query defines the variables once and then uses them
in two separate EVALUATE statements.
// Defining the Variables
DEFINE
VAR TaxPercent = 18
VAR TotalSales = SUM(Sales[Sales Amount])
VAR AverageSales = AVERAGE(Sales[Sales Amount])
// Find the Tax for Total Sales
EVALUATE
ROW(
"Total Sales", TotalSales,
"Total Tax", TotalSales * (TaxPercent/100)
)
// Find the Tax for Average Sales
EVALUATE
ROW(
"Average Sales", AverageSales,
"Average Tax", AverageSales * (TaxPercent/100)
)
How the query works
- Define the tax percentage.
- Calculate and store the total sales.
- Calculate and store the average sales.
- Return the total sales and total tax.
- Return the average sales and average tax.
Understanding the two results
| Calculation | Variable Used | Tax Formula |
|---|---|---|
| Total Sales | TotalSales | TotalSales × 18% |
| Average Sales | AverageSales | AverageSales × 18% |
The same variable can be reused in multiple calculations,
making the query easier to maintain.
5. Creating a Measure Using Variables
The same logic can also be used when creating a measure.
Instead of using DEFINE and EVALUATE,
we define the measure name and return the calculation.
Measure
// Creating a Measure
Total Tax Amount =
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 18
RETURN TotalSalesAmount * TaxAmount
Step-by-step explanation
Step 1: Total Tax Amount =
This is the name of the measure.
The measure will return the calculated tax amount.
Step 2: VAR TotalSalesAmount = SUM(Sales[Sales Amount])
This variable calculates the total sales amount.
Step 3: VAR TaxAmount = 18
This variable stores the tax percentage as 18.
Step 4: RETURN TotalSalesAmount * TaxAmount
RETURN specifies the final result of the measure.
The measure multiplies total sales by the tax amount.
Important difference in the formula
In the query, the tax percentage is divided by 100:
TotalSales * (TaxPercent/100)
In the measure, the formula is:
TotalSalesAmount * TaxAmount
In this measure, TaxAmount is used directly as 18.
If you intend to calculate an 18% tax, the formula should divide
the percentage by 100.
Corrected percentage-based measure
Total Tax Amount =
VAR TotalSalesAmount = SUM(Sales[Sales Amount])
VAR TaxAmount = 18
RETURN TotalSalesAmount * (TaxAmount/100)
When using a percentage value such as 18, divide it by 100
before multiplying it with the sales amount.
6. Difference Between DEFINE Variables and Measure Variables
| DEFINE Variables | Measure Variables |
|---|---|
| Used inside a DAX query | Used inside a measure |
| Declared using DEFINE | Declared directly using VAR |
| Can be used by multiple EVALUATE statements | Used within the measure expression |
| Returns results through EVALUATE | Returns results through RETURN |
7. Important Notes About Variables
1. Variables improve readability
Instead of repeating the same calculation multiple times,
we can store it in a variable and reuse it.
2. Variables reduce repeated calculations
When the same expression is needed more than once,
a variable helps keep the code cleaner.
3. Variables make debugging easier
You can inspect the values of individual variables while
developing a DAX query or measure.
4. RETURN defines the final result
In a measure, the RETURN statement specifies
the expression that produces the final result.
📝 Practice Exercise
Try writing a DAX query to calculate:
- Total Sales
- Average Sales
- Total Tax at 18%
- Average Tax at 18%
Use variables to store the tax percentage, total sales,
and average sales.
Expected Query
DEFINE
VAR TaxPercent = 18
VAR TotalSales = SUM(Sales[Sales Amount])
VAR AverageSales = AVERAGE(Sales[Sales Amount])
EVALUATE
ROW(
"Total Sales", TotalSales,
"Average Sales", AverageSales,
"Total Tax", TotalSales * (TaxPercent/100),
"Average Tax", AverageSales * (TaxPercent/100)
)
Conclusion
In this article, we learned how to define and use variables
in DAX to calculate total tax and average tax.
- DEFINE is used to define variables in a DAX query.
- VAR stores a value or calculation result.
- SUM calculates total sales.
- AVERAGE calculates average sales.
- EVALUATE returns the result of a table expression.
- ROW creates a table containing one row.
- RETURN specifies the final result of a measure.
- Variables make DAX code easier to read and maintain.
The key idea is simple: define the calculation once,
store it in a variable, and reuse it wherever needed.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist