Variables with VAR
Part of the DAX with CALCULATE section of Coddy's Power BI journey. Lesson 18 of 24.
VAR Name = expression names a step; RETURN gives the result.
Margin % =
VAR Cost = SUMX(Sales, Sales[Quantity] * RELATED(Products[UnitCost]))
VAR Profit = [Revenue] - Cost
RETURN DIVIDE(Profit, [Revenue])Each variable is worked out once, under the measure's filters, and can use the ones above it.
Challenge
Write Margin % with variables: a Cost variable (quantity times the product's unit cost, line by line), a Profit variable (Revenue minus Cost), then return Profit divided by Revenue. It is checked for each category.