Select Page

VIDEO: Using Variables in DAX

by | Oct 21, 2020 | Video

In this video, we’ll continue learning about variables in Power BI.

Variables are an important feature that makes writing and reading DAX formulas easier, simplify debugging and improve performance.

Let’s take a look at syntax, which is very simple. Follow the two following steps:

1. Create a measure and add the variables, by writing VAR var_name = [Valid DAX formula]

2. Return the calculation for the set variable by writing RETURN [Another DAX formula with the variable used as part of the calculation.

In our example in the video we used the below DAX formula:

Profit in Russia =
VAR TotalSalesRussia = CALCULATE(
SUMX( Sales, Sales[UnitPrice] * Sales[SalesQuantity] ), Geography[RegionCountryName] = “Russia”)

VAR TotalCostRussia = CALCULATE(
SUMX( sales, Sales[UnitCost] * Sales[SalesQuantity]), Geography[RegionCountryName] = “Russia”)

RETURN
TotalSalesRussia – TotalCostRussia

The above example shows how variables can make the DAX code more readable. Instead of two long expressions that use repetitive parts, you can define the two variables once and then re-use them later after the return part. If needed, the variables can be used multiple times.

Summary

When you have the following scenarios, it would be best to introduce variables:

1. If you have to use a part of your code more than once.

2. If you code is long and hard to read.

3. If the calculation is slow.

Tags:

DAX | Power BI | video
Share This