VAR and RETURN in Power BI DAX
VAR stores a named result inside a DAX measure, and RETURN is the one answer you hand back to the visual — so a long formula becomes a short story you can debug.
Friends! Long measures fail in interviews because the author cannot point at the middle. Why? The same expression is copied three times. How? We name each piece with VAR on a fictional FreshBasket sales model, return one result, and temporarily return a single variable to see it on a card. Slow and clear.
मित्रांनो! लांब measures मुलाखतीत फसतात, कारण लेखक मधला तुकडा बोटाने दाखवू शकत नाही. का? तोच expression तीन वेळा कॉपी केलेला असतो. कसे? FreshBasket दुकानाच्या sales वर प्रत्येक तुकड्याला VAR ने नाव द्यायचे, एक निकाल परत द्यायचा, आणि चेक करण्यासाठी एक variable कार्डवर आणायचा. हळू आणि स्पष्ट.
मित्रों! लंबे measures इंटरव्यू में फिसल जाते हैं, क्योंकि लेखक बीच का टुकड़ा उंगली से नहीं दिखा पाता. क्यों? वही expression तीन बार कॉपी होता है. कैसे? FreshBasket दुकान की sales पर हर टुकड़े का नाम VAR से रखते हैं, एक नतीजा वापस देते हैं, और चेक के लिए एक variable कार्ड पर लाते हैं. धीरे और साफ.
Quick answer
Vertical shape of every variable measure:
- Keep a working base measure such as
[Total Sales]. - Start the new measure with
VARlines, one name each. - Finish with exactly one
RETURN. - The variable is fixed in the filter context where it is defined.
- To debug,
RETURNthat variable alone, read the card, then put the real answer back. - Use variables when you would otherwise repeat yourself.
VAR SalesAmt = [Total Sales]
VAR OrdersN = [Orders]
RETURN DIVIDE ( SalesAmt, OrdersN )
Why VAR, why FILTER, what comes out
A variable does not filter rows. It remembers a number you already calculated.
Tiny sales table, City slicer = Pune (fictional FreshBasket):
| Order ID | City | Amount |
|---|---|---|
| A | Pune | 100 |
| A | Pune | 40 |
| B | Pune | 60 |
Three rows are still in the visual. Sales are 200. Distinct orders are 2.
Why use VAR:
- You need 200 and 2 more than once (the ratio, then a label).
- Naming them beats copying
SUMthree times. - You can show one name on a card while you check your work.
Why FILTER is not inside the VAR line:
- The slicer already kept Pune. Those rows go in.
SUMandDISTINCTCOUNTwalk those rows and each return one number.VAR SalesAmt = [Total Sales]stores 200. It does not store the three rows.- A later
CALCULATEdoes not reopen that variable and filter it again.
When FILTER is needed with a variable:
- The question cuts rows by a test, for example Amount above 50.
FILTERkeeps the rows that pass. Here that is 100 and 60, not 40.COUNTROWSorSUMturns that smaller table into one number.- You may store that number in a VAR. The filter did the cutting. The variable only remembers the result.
What the AOV formula does, in order:
- Rows in: the three Pune rows.
SalesAmtout: 200.OrdersNout: 2.RETURNdivides. The card shows 100.- Change the slicer to another city and the same steps run on that city's rows.
Real example: two shops and the phone bill
Picture one small shop, FreshBasket, with a counter in Mumbai and a counter in Pune. March sales slips:
| Slip | City | What was sold | Amount |
|---|---|---|---|
| 1 | Mumbai | Rice | 500 |
| 2 | Mumbai | Oil | 200 |
| 3 | Mumbai | Atta | 300 |
| 4 | Pune | Rice | 100 |
| 5 | Pune | Oil | 40 |
| 6 | Pune | Sugar | 60 |
The same owner also pays a phone bill for each shop. That bill is not a sale. It is money going out.
| Month | City | Phone bill |
|---|---|---|
| Jan | Mumbai | 499 |
| Feb | Mumbai | 499 |
| Mar | Mumbai | 699 |
| Jan | Pune | 399 |
| Feb | Pune | 399 |
| Mar | Pune | 399 |
Why VAR here: you will say "average sale" and "average phone bill" more than once. Name the pieces.
- City slicer = Mumbai. Sales rows in: rice, oil, atta. Number out of
[Total Sales]: 1000. Store it asVAR SalesAmt. - Three slips, so
VAR BillsN = 3if each slip is one order.RETURN DIVIDE ( SalesAmt, BillsN )shows about 333 on the card. - Phone-bill rows in, same slicer: 499, 499, 699.
VAR BillAmtbecomes 1697.VAR MonthsNbecomes 3. Average bill out: about 566. - VAR did not choose Mumbai. The slicer did. The variable only remembers the number after the rows were already kept.
Why FILTER joins this story: "months where the phone bill is above 500" is a test. FILTER keeps only March (699). COUNTROWS then returns 1. You may store that 1 in a VAR. The filter cuts rows. The variable does not.
What you should see: click Pune on the slicer and both cards must drop (sales 200, phone bill 399). If they stay on Mumbai, the variable was frozen too early, before the city filter.
What do I need before this guide?
- A base measure from SUM vs SUMX or the DAX beginner guide.
- IF and SWITCH if the return value is a label.
- DIVIDE if the return value is a ratio.
- Course: VAR and RETURN.
Before and after (look at the tables first)
Before
आधी (Before)
पहले (Before)
After
नंतर (After)
बाद में (After)
Why variables exist
- A measure that repeats
[Total Sales]four times is hard to read out loud. - If you change the definition, you must hunt every copy.
VARgives the piece a name.RETURNis the only line the visual receives.- You can return a middle name on purpose while you are learning the measure.
Name the pieces with VAR, then finish the measure with one RETURN.
तुकड्यांना VAR ने नाव द्या, आणि measure शेवटी एकाच RETURN ने संपवा. तोच शेवट visual ला मिळतो.
टुकड़ों को VAR से नाम दो, और measure को आखिर में एक ही RETURN से खत्म करो. वही अंत visual को मिलता है.
Write AOV with names
Assume these base measures already exist:
Total Sales = SUM ( Sales[Amount] )
Orders = DISTINCTCOUNT ( Sales[Order ID] )
Now the ratio:
AOV =
VAR SalesAmt = [Total Sales]
VAR OrdersN = [Orders]
RETURN
DIVIDE ( SalesAmt, OrdersN )
Read it vertically:
SalesAmtcalls the sales measure in the current filter context (City slicer, visual, and so on).OrdersNcalls the orders measure in that same context.RETURNdivides them safely.- Format AOV as a decimal or currency.
DIVIDEdoes not format the measure for you.
A label that uses the names
FreshBasket (fictional) wants a simple band for the ops review. Thresholds below are practice numbers, not a company rule.
AOV Band =
VAR SalesAmt = [Total Sales]
VAR OrdersN = [Orders]
VAR AovValue = DIVIDE ( SalesAmt, OrdersN )
RETURN
SWITCH (
TRUE (),
ISBLANK ( OrdersN ), BLANK (),
AovValue >= 800, "Strong",
"Watch"
)
- The third variable may use the first two. Order matters: a name must be defined before you use it.
SWITCH ( TRUE () )still takes the first true test. Variables do not change that.ISBLANKon the order count avoids a band when there is nothing to average.
A variable is evaluated where it is defined and does not change later, even inside CALCULATE.
variable तिथे मोजला जातो जिथे तो लिहिला आहे, आणि नंतर बदलत नाही, अगदी CALCULATE च्या आतही.
variable वहीं गिना जाता है जहाँ वह लिखा है, और बाद में नहीं बदलता, CALCULATE के अंदर भी नहीं.
The rule students miss
A variable is evaluated where it is defined. It does not wake up again inside a later CALCULATE.
Sales Frozen =
VAR SalesAmt = [Total Sales]
RETURN
CALCULATE ( SalesAmt, Sales[Channel] = "Online" )
SalesAmtis already a number from the outer context.- The
CALCULATEthat follows does not recompute that variable under the Online filter. - If you wanted Online sales, call the measure inside
CALCULATE, or define the variable there. - This is useful when you mean to freeze a value. It is a bug when you expected the filter to apply.
Debug without guessing
To debug, temporarily RETURN one variable so the card shows that intermediate number.
चेक करण्यासाठी थोड्या वेळ RETURN मध्ये एकच variable ठेवा, म्हणजे कार्ड ती मधली संख्या दाखवेल.
चेक छोड़ो. थोड़ी देर RETURN में एक variable रखो, ताकि कार्ड वह बीच की संख्या दिखाए.
- Change
RETURNtoRETURN OrdersN. - Put the measure on a card and slice City.
- Confirm the count matches the table.
- Change
RETURNtoRETURN AovValueif you are in the band measure. - Put the real
SWITCHorDIVIDEback when the middle looks right.
Ghabru naka — a wrong band is usually a wrong middle number, not a mysterious visual.
Mistakes and calm fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Syntax error after VAR | Missing RETURN |
One RETURN at the end |
| Online filter ignored | Variable captured too early | Call the measure inside CALCULATE |
| Same number everywhere | Base measure ignores filters | Test [Total Sales] alone first |
| Band always "Watch" | Threshold or blank orders | RETURN the AOV variable and look |
Ravindra Bagale's Tip
Interview line: “A variable is evaluated once, where it is defined, so I use it to name pieces and to freeze a value. If I need the new filter, I calculate inside CALCULATE.” Then show one RETURN of a variable as your debug habit. Got it?
Ravindra Bagale's Tip – मराठी
मुलाखतीचे वाक्य: “variable एकदा मोजला जातो, जिथे तो लिहिला आहे, म्हणून मी तुकड्यांना नाव देतो आणि किंमत गोठवतो. नवा filter हवा असेल तर CALCULATE च्या आत मोजतो.” मग सवयीने एका variable चा RETURN दाखवा. समजलं का?
Ravindra Bagale's Tip – हिंदी
इंटरव्यू की लाइन: “variable एक बार गिना जाता है, जहाँ वह लिखा है, इसलिए मैं टुकड़ों को नाम देता हूँ और कीमत जमा देता हूँ. नया filter चाहिए तो CALCULATE के अंदर गिनता हूँ.” फिर आदत से एक variable का RETURN दिखाओ. समझ में आया?
Practice task
- Write
AOVwith two variables. - Slice one city and write the two middle numbers in a notebook.
- Add
AOV Bandwith a practice threshold. - Break it on purpose by freezing sales outside
CALCULATE, then fix it. - Restore the real
RETURN.
Got it? VAR names the pieces, one RETURN answers the visual, and the value stays fixed where you defined it. Next: which count function you actually meant. Let's continue.
समजलं का? VAR तुकड्यांना नाव देतो, एक RETURN visual ला उत्तर देतो, आणि किंमत तिथेच स्थिर राहते जिथे तुम्ही लिहिली. पुढे: कोणती count function खरंच हवी होती. आता पुढे जाऊया.
समझ में आया? VAR टुकड़ों को नाम देता है, एक RETURN visual को जवाब देता है, और कीमत वहीं स्थिर रहती है जहाँ आपने लिखी. आगे: कौन सी count function सच में चाहिए थी. आगे बढ़ते हैं.
Frequently asked questions
What is VAR in DAX?
VAR name = expression stores an intermediate result. You must finish the measure with RETURN.
How many RETURN statements?
One. You can declare several VAR lines, then a single RETURN.
Does a variable recalculate inside CALCULATE?
No. It is evaluated in the filter context where it is defined, and that value stays fixed later.
Why use variables?
The measure is easier to read, easier to debug, and the engine can reuse the value instead of repeating the same expression.
Can I RETURN a variable by itself?
Yes. That is the usual way to check an intermediate number on a card.
Course lesson?
Variables: VAR and RETURN, in the DAX chapter.