๐ Data Analyst Roadmap โ Part 30
POWER BI LEVEL 9 โ DAX VARIABLES: VAR, RETURN & CLEANER DAX
As DAX calculations become more complex, writing everything in one expression can make your measures difficult to understand and maintain.
That's where VAR and RETURN become extremely useful.
๐น 1. What is VAR?
VAR allows you to store the result of a calculation in a variable.
Example:
Profit =
VAR Revenue = [Total Sales]
VAR Cost = [Total Cost]
RETURN
Revenue - Cost
Instead of repeating [Total Sales] and [Total Cost], we give them meaningful names. The calculation becomes easier to read.
๐น 2. What does RETURN do?
RETURN tells DAX which final result should be returned.
VAR โ Create temporary values
RETURN โ Give me the final result
Example:
Profit Margin =
VAR Profit = [Total Profit]
VAR Sales = [Total Sales]
RETURN
DIVIDE(Profit, Sales)
๐น 3. Why use Variables?
Without variables:
Profit Margin =
DIVIDE(
[Total Sales] - [Total Cost],
[Total Sales]
)
With variables:
Profit Margin =
VAR Sales = [Total Sales]
VAR Cost = [Total Cost]
VAR Profit = Sales - Cost
RETURN
DIVIDE(Profit, Sales)
The second version is easier to understand. You can immediately see: Sales, Cost, Profit, Profit Margin
๐น 4. Variables Can Store Numbers
Example:
Sales Target Status =
VAR Sales = [Total Sales]
VAR Target = 1000000
RETURN
IF(
Sales >= Target,
"Target Achieved",
"Below Target"
)
Now the business rule is much easier to read.
๐น 5. Variables Can Store Text
Variables don't have to contain numbers.
Example:
Region Message =
VAR Region =
SELECTEDVALUE(
Sales[Region],
"Multiple Regions"
)
RETURN
"Current Region: " & Region
If West is selected: "Current Region: West"
๐น 6. Variables Can Store Tables
This is where DAX starts becoming more powerful. A variable can also contain a table expression.
Example:
High Value Customers =
VAR Customers =
FILTER(
VALUES(Sales[CustomerID]),
[Total Sales] > 100000
)
RETURN
COUNTROWS(Customers)
Here: "Customers" stores a temporary table. Then COUNTROWS() counts how many customers are in that table.
๐น 7. Variables and FILTER()
Variables make complex filtering easier to understand.
Example:
High Value Sales =
VAR FilteredSales =
FILTER(
Sales,
Sales[SalesAmount] > 10000
)
RETURN
SUMX(
FilteredSales,
Sales[SalesAmount]
)
Instead of putting everything into one long expression, we separate the logic into meaningful steps.
๐น 8. Variables Are Evaluated Once
A useful performance benefit is that variables can avoid repeatedly evaluating the same expression.
For example, instead of repeatedly calculating [Total Sales] you can store it:
VAR Sales = [Total Sales]
and reuse Sales. This can make complex measures cleaner and, in some cases, more efficient.
๐น 9. Variables Improve Debugging
Suppose you have:
Profit Analysis =
VAR Sales = [Total Sales]
VAR Cost = [Total Cost]
VAR Profit = Sales - Cost
VAR Margin = DIVIDE(Profit, Sales)
RETURN
Margin
If the final result looks incorrect, you can temporarily change the RETURN statement to:
RETURN Profit
or
RETURN Cost
This makes it easier to understand where the calculation is going wrong.
๐น 10. Variables Don't Create Model Columns
This is important. A variable inside a measure VAR Sales = [Total Sales] does NOT create a permanent column in your Power BI model. It exists only while that measure is being evaluated.
So:
Calculated Column โ stored in the model
Measure Variable โ temporary value during calculation
๐น 11. Real Business Example
Suppose management wants to classify performance:
Sales โฅ โน10M โ Excellent
Sales โฅ โน5M โ Good
Sales โฅ โน2M โ Average
Below โน2M โ Needs Attention
You can write:
Sales Performance =
VAR Sales = [Total Sales]
RETURN
SWITCH(
TRUE(),
Sales >= 10000000, "Excellent",
Sales >= 5000000, "Good",
Sales >= 2000000, "Average",
"Needs Attention"
)
Post #3155
2.07K
- โค 3