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
hideakisuzuki01
Helper II
Helper II

Question on DAX

Hi,

I have this very simple table named "Sales",

hideakisuzuki01_0-1663743628136.png

 

And I am trying to add a calculated column that gives "Last Year Sales" and I dont know why one DAX formua works and the other one does not.

 

This one works:

 

LastYearSales2 =
VAR LastYear = 'Sales'[Year] -1
VAR LastYearSales = CALCULATE(
    SUM(Sales[SalesAmount]),
    FILTER('Sales','Sales'[Year] = LastYear))
RETURN LastYearSales

 

 

This one does not work:

 

LastYearSales =
VAR LastYear = 'Sales'[Year] -1
VAR LastYearSales = CALCULATE(
    SUM(Sales[SalesAmount]),
    'Sales'[Year] = LastYear)
RETURN LastYearSales
 
Even without using the FILTER function, I thought the DAX engine automatically adds the FILTER functions before it gets processed so I dont know why one works and the other does not.
 

hideakisuzuki01_1-1663743818254.png

 

 

 

 

 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@hideakisuzuki01,

 

This is due to context transition (see article below). LastYearSales overrides the filter context of only the Year column, leaving in place the other column filters that result from context transition. LastYearSales2 filters the entire table for the specified condition, and then uses that table filter in CALCULATE. If you add "ALL(Sales)" to LastYearSales, it removes the filters resulting from context transition and returns the correct result:

 

LastYearSales = 
VAR LastYear = Sales[Year] - 1
VAR LastYearSales =
    CALCULATE (
        SUM ( Sales[SalesAmount] ),
        Sales[Year] = LastYear,
        ALL ( Sales )
    )
RETURN
    LastYearSales

 

https://www.sqlbi.com/articles/understanding-context-transition-in-dax/ 

 

You can achieve the same result with SUMX:

 

LastYearSales SUMX = 
VAR vYear = Sales[Year]
VAR vTable =
    FILTER ( Sales, Sales[Year] = vYear - 1 )
VAR vResult =
    SUMX ( vTable, Sales[SalesAmount] )
RETURN
    vResult

 

DataInsights_0-1663767525072.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
hideakisuzuki01
Helper II
Helper II

oh, that does make sense.  Just when I thought I understood context transition, something like this happens and realize I still have a lot to learn. 

Thanks so much !

DataInsights
Super User
Super User

@hideakisuzuki01,

 

This is due to context transition (see article below). LastYearSales overrides the filter context of only the Year column, leaving in place the other column filters that result from context transition. LastYearSales2 filters the entire table for the specified condition, and then uses that table filter in CALCULATE. If you add "ALL(Sales)" to LastYearSales, it removes the filters resulting from context transition and returns the correct result:

 

LastYearSales = 
VAR LastYear = Sales[Year] - 1
VAR LastYearSales =
    CALCULATE (
        SUM ( Sales[SalesAmount] ),
        Sales[Year] = LastYear,
        ALL ( Sales )
    )
RETURN
    LastYearSales

 

https://www.sqlbi.com/articles/understanding-context-transition-in-dax/ 

 

You can achieve the same result with SUMX:

 

LastYearSales SUMX = 
VAR vYear = Sales[Year]
VAR vTable =
    FILTER ( Sales, Sales[Year] = vYear - 1 )
VAR vResult =
    SUMX ( vTable, Sales[SalesAmount] )
RETURN
    vResult

 

DataInsights_0-1663767525072.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.