Before: Repeated Calculations
Profit Margin =
DIVIDE(
SUM(Sales[Revenue]) - SUM(Sales[Cost]),
SUM(Sales[Revenue])
) * 100
Here, SUM(Sales[Revenue]) is calculated twice.
Advertisement
After: Using Variables
Profit Margin =
VAR TotalRevenue = SUM(Sales[Revenue])
VAR TotalCost = SUM(Sales[Cost])
VAR Profit = TotalRevenue - TotalCost
RETURN
DIVIDE(Profit, TotalRevenue) * 100
Benefits
- Performance: Each calculation runs only once
- Readability: Clear, self-documenting code
- Debugging: Hover over variables to see values
- Maintenance: Change logic in one place
Advanced Pattern: Conditional Logic
Sales Status =
VAR CurrentSales = [Total Sales]
VAR Target = [Sales Target]
VAR Achievement = DIVIDE(CurrentSales, Target)
RETURN
SWITCH(
TRUE(),
Achievement >= 1, "Exceeded",
Achievement >= 0.8, "On Track",
Achievement >= 0.5, "At Risk",
"Critical"
)
Tip: Use descriptive variable names. Future you will thank present you.