Forum Discussion
When does a variable gets calculated? Before or after RETURN?
Hi sahabatsuria
In DAX (Data Analysis Expressions) for Power BI, variables are calculated before the RETURN statement. When you define a variable using the VAR keyword, its value is computed immediately, and this value is then used in the expression that follows the RETURN statement
Here’s a simple example to illustrate:
Customer% =
VAR SalesAmount = SUMX(Sales, Sales[Quantity] * Sales[Net Price])
VAR NumCustomers = DISTINCTCOUNT(Sales[CustomerKey])
RETURN DIVIDE(SalesAmount, NumCustomers)
In this example:
- SalesAmount and NumCustomers are calculated first.
- The RETURN statement then uses these pre-calculated values to compute the final result.
This approach helps improve readability and performance of your DAX code
Still If you need more information, pls go through follwing documentation
https://learn.microsoft.com/en-us/dax/best-practices/dax-variables
https://www.sqlbi.com/articles/when-are-variables-evaluated-in-dax/
Thanks!
- sahabatsuria1 year agoFrequent Visitor
Hi suparnababu8 . Although your response says variables will be evaluated before the RETURN statement, the SQLBI link you attached stated otherwise, where it states near the end of the article:
"If the variable is defined but not referenced, then it is not evaluated."