Ravindra BagaleCourses & study guides मराठी Track your progress

13. DAX: Data Analysis Expressions

13.18 VAR and RETURN in Power BI DAX with Example

VAR and RETURN in Power BI DAX with example — in 30 seconds

What: VAR stores a value once inside a measure. RETURN gives the final answer.

Why: The formula is easier to read and Power BI does not calculate the same thing twice.

Tiny example: This month ₹1,200. Last month ₹1,000. Growth = 20%.

First look at the data (before → after):

VAR and RETURN in Power BI DAX with example: before and after tables, fictional Blinkit Maharashtra data

Same before / after tables as text

Before — Two numbers

Item Value
Total Sales this month ₹1,200
Total Sales last month ₹1,000

About this data: This table uses columns: Item, Value. This table uses columns: Measure, Value. Values shown are the lesson examples already on this page (not a new dataset).

Formula / click path

MoM Growth % =
VAR Cur  = [Total Sales]
VAR Prev = CALCULATE([Total Sales], DATEADD('Date'[Date], -1, MONTH))
RETURN DIVIDE(Cur - Prev, Prev)

After — Result

Measure Value
MoM Growth % 20%

Do it step by step:

  1. New measure. Type the four lines. Use Shift + Enter for a new line.
  2. Format as percentage.
  3. To debug, change RETURN to RETURN Prev. Check ₹1,000. Then change it back.

Variables store an intermediate result, making formulas easier to read, easier to debug and often faster (a variable is calculated once, even if used several times).

Store Health =
VAR OrdersCount = [Total Orders]
VAR AvgTime = [Avg Delivery Time (mins)]
VAR CancelRate = [Cancellation Rate]
RETURN
    SWITCH(TRUE(),
        ISBLANK(OrdersCount), BLANK(),
        AvgTime <= 10 && CancelRate < 0.02, "Excellent",
        AvgTime <= 15 && CancelRate < 0.05, "Good",
        "Needs Attention")

(The thresholds are example values for practice, not industry benchmarks.)

Now look at this: rules: define variables with VAR name = expression; finish with one RETURN. A variable is evaluated where it is defined, in the context at that point – it does not change later, even inside CALCULATE.

Debugging with variables

To check an intermediate value, temporarily change RETURN … to RETURN AvgTime. You can also test measures in DAX query view using EVALUATE and SUMMARIZECOLUMNS.

EVALUATE
SUMMARIZECOLUMNS(
    DarkStore[City],
    Orders[Platform],
    "Orders", [Total Orders],
    "Avg Mins", [Avg Delivery Time (mins)],
    "Cancel %", [Cancellation Rate]
)
ORDER BY [Orders] DESC

Make using variables a habit. The formula becomes easier to read, and mistakes become easier to find.

Ravindra Bagale's Tip

A common mistake is defining a variable and expecting it to change inside a later CALCULATE. Variables are evaluated once, where they are defined. Use this on purpose to "freeze" a value, and return a variable on its own when you need to debug a long measure. Keep this in mind!

Loading DAX practice tasks...

  1. Reject rate with variables (Easy): Measure: Reject Rate = rejected units ÷ units made, using two variables.
  2. Good units (Easy): Measure: Good Units = units made minus units rejected, with variables.
  3. Production cost per country (Easy): Shown per Country. Measure: Prod Cost = UnitsMade × CostPerUnit added, stored in a variable.
  4. Cost per good unit (Medium): Shown per Part. Measure: Cost per Good = total cost ÷ good units.
  5. Share of all units (Medium): Shown per Plant. Measure: Unit Share = this plant's units ÷ units of all plants.
  6. Check or OK (Medium): Shown per Part. Measure: Quality Flag = "Check" if rejected ÷ made is above 0.02, else "OK".
  7. Germany share (Medium): Measure: Germany Share = units made in Germany ÷ all units made.
  8. Night minus day (Hard): Shown per Plant. Measure: Night Gap = Night shift units − Day shift units.
  9. Biggest part volume (Hard): Measure: Top Part Units = the highest total UnitsMade of any Part.
  10. Good percent column (Hard): Calculated column on Batches: Good Pct = (UnitsMade − UnitsRejected) ÷ UnitsMade, using a variable.

Ravi Sir's Cards for this lesson

मराठी cards