Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kingkong
Frequent Visitor

Measure not returning value when using a variable to calculate another variable

I want to calculate sales week over week by (sales of the current week selected - sales of the previous week) divided by sales of the previous week. But when I use the Sales variable in the SalesLW variable, the result returns a 0(Image 1).

 

But If I use sum(Query1[SalesAmount]) instead of variable Sales, I do get the accurate number. (Image 2) I thought that we can use a variable in another variable, why is it not returning the correct value? 

No Value returned.JPGreturned value.JPG

2 ACCEPTED SOLUTIONS
BetterCallFrank
Resolver IV
Resolver IV

Hey kingkong,

basically, a variable is evaluated one time and the resulting value is assigned to the variable; the variable expression is not evaluated over and over again when you use the variable.

So

VAR vSales = SUM( q1[sales] ) // evaluated one time, e.g. vSales = 10
VAR vSalesLWnotworking = CALCULATE ( vSales; DATEADD( dimdate[date], -7, day ) ) // will always return 10
VAR vSalesLWworks = CALCULATE ( SUM( q1[sales] ); DATEADD( dimdate[date], -7, day ) ) // will work as expected

You can re-use the value of the variable, but not the expression of the variable

Hope this helps 🙂

View solution in original post

v-yulgu-msft
Employee
Employee

Hi @kingkong ,

 

It is a Row Context related behavior.

 

Scenario 1: Definde Sum(Query1[SalesAmount]) in an outside variable 'Sales', and refer to this variable in another variable 'SalesLW'.

There existing two levels of row context in current DAX formula. The outside row context is determined by current row in table visual. The inside row context is determined by the DATEADD expression. The result of "Sum(Query1[SalesAmount])" is influenced by the outside row context (current row in table visual, "5/6/2019"), as it is defined outside. So, "SalesLW" returns 13 in this case, "SalesWoW" returns (13-13)/13=0.1.png

 

Scenario 2: Define Sum(Query1[SalesAmount]) directly in current variable 'SalesLW'.

Also, there existing two levels of row context in current DAX formula. But "Sum(Query1[SalesAmount])" is influenced by the inside row context (DATEADD('Date'[Date],-7,DAY)=4/29/2019). So, "SalesLW" returns 20 in this case, "SalesWoW" returns (13-20)/20=-35%.

2.png

 

Here are some links written about row context for your reference:

Context in DAX Formulas

Row context and Filter context in DAX

Row Context, Nested Functions, and EARLIER() in PowerPivot and DAX

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @kingkong ,

 

It is a Row Context related behavior.

 

Scenario 1: Definde Sum(Query1[SalesAmount]) in an outside variable 'Sales', and refer to this variable in another variable 'SalesLW'.

There existing two levels of row context in current DAX formula. The outside row context is determined by current row in table visual. The inside row context is determined by the DATEADD expression. The result of "Sum(Query1[SalesAmount])" is influenced by the outside row context (current row in table visual, "5/6/2019"), as it is defined outside. So, "SalesLW" returns 13 in this case, "SalesWoW" returns (13-13)/13=0.1.png

 

Scenario 2: Define Sum(Query1[SalesAmount]) directly in current variable 'SalesLW'.

Also, there existing two levels of row context in current DAX formula. But "Sum(Query1[SalesAmount])" is influenced by the inside row context (DATEADD('Date'[Date],-7,DAY)=4/29/2019). So, "SalesLW" returns 20 in this case, "SalesWoW" returns (13-20)/20=-35%.

2.png

 

Here are some links written about row context for your reference:

Context in DAX Formulas

Row context and Filter context in DAX

Row Context, Nested Functions, and EARLIER() in PowerPivot and DAX

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
BetterCallFrank
Resolver IV
Resolver IV

Hey kingkong,

basically, a variable is evaluated one time and the resulting value is assigned to the variable; the variable expression is not evaluated over and over again when you use the variable.

So

VAR vSales = SUM( q1[sales] ) // evaluated one time, e.g. vSales = 10
VAR vSalesLWnotworking = CALCULATE ( vSales; DATEADD( dimdate[date], -7, day ) ) // will always return 10
VAR vSalesLWworks = CALCULATE ( SUM( q1[sales] ); DATEADD( dimdate[date], -7, day ) ) // will work as expected

You can re-use the value of the variable, but not the expression of the variable

Hope this helps 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.