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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Filter context and variables (can someone explain different results?)

Using the following DAX I am trying to calculate the change in average monthly total sales over time at region level:

MA REGION =

VAR _mindate = MIN('Date'[Date])

VAR _table = CALCULATETABLE(ADDCOLUMNS(SUMMARIZE(Sheet1, 'Date'[Month Year], Sheet1[REGION]), "Sum Sales", CALCULATE(SUM(Sheet1[SALES]), REMOVEFILTERS(Sheet1[CITY]))), 'Date'[Date] <= _mindate)

RETURN

AVERAGEX(_table, [Sum Sales])

This gives me (what I think) are correct results:

brownrice_0-1686335406191.png


What I don't understand is why the following do not give me the same results:

MA REGION 2 =

VAR _mindate = MIN('Date'[Date])

VAR _table = ADDCOLUMNS(SUMMARIZE(Sheet1, 'Date'[Month Year], Sheet1[REGION]), "Sum Sales", CALCULATE(SUM(Sheet1[SALES]), REMOVEFILTERS(Sheet1[CITY])))

RETURN

AVERAGEX(CALCULATETABLE(_table, 'Date'[Date] <= _mindate), [Sum Sales])

brownrice_1-1686335600642.png

MA region 3 =

VAR _mindate = MIN('Date'[Date])

VAR _table = ADDCOLUMNS(SUMMARIZE(Sheet1, 'Date'[Month Year], Sheet1[REGION]), "Sum Sales", CALCULATE(SUM(Sheet1[SALES]), REMOVEFILTERS(Sheet1[CITY])))

RETURN

CALCULATE(AVERAGEX(_table, [Sum Sales]), 'Date'[Date] <= _mindate)

brownrice_2-1686335652162.png


I presume it is to do with the way the filter context is handled but would appreciate help in understanding why the first measure gives different results to the others.

Thank you.

1 REPLY 1
johnt75
Super User
Super User

Variables in DAX aren't really variables, they're constants which are only evaluated once, at the point where they are declared.

In your second 2 examples you are creating a static table and then applying a filter to 'Date'[Date], but the Date table cannot filter the variable because it isn't part of the model.

If you wanted to use the technique in your third example you would need to include in the summary table a numeric representation of month year and you would then be able to use that as a filter.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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