Menu
CoddyTech

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 icon

Challenge

Medium

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.

Try it yourself

Margin % =
CategoryMargin %
Coffee
Tea
Bakery
Merch
Total
Fields
  • Sales
    • Revenue
    • Total Units
    • Customers
  • Products
    • ProductID
    • Product
    • Category
    • UnitPrice
    • UnitCost
  • Stores
  • Customers
  • Dates
  • Targets

All lessons in DAX with CALCULATE