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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
sunilbeckham
Helper I
Helper I

Calculate function not filtering when a variable is used.

Hello everyone. I have a table that looks something like this:

School DivisionStudent enrollmentMonth
A10Oct
B12Oct
C15Nov
C10Dec

 

When I create a simple calculate measure such as this:

 

 

Students enrolled = 
CALCULATE(SUM('Table'[Student enrollment]),'Table'[Month] = "Oct")

 

 

I get the correct result, which in this example will be = 22

 

But when I create a variable to store the Sum aggregate, I get a different result that is without any filter specified within Calculate function. For example this DAX:

 

 

Students enrolled = 
VAR _studentsenrolled = SUM('Table'[Student enrollment])
RETURN
CALCULATE(_studentsenrolled,'Table'[Month] = "Oct")

 

 

In this example, it gives me the result as 47. Which is incorrect as it doesn't seem to be taking into account the filter parameter of Calculate function.

Can anyone help clarify what am I missing?

 

Thanks in advance!

1 ACCEPTED SOLUTION
Erokor
Resolver II
Resolver II

Echoing @Greg_Deckler on this one, but with a little more information. Variables are calculated only once and then stored only once and cannot be re-used in their current context. The only exception to this is if you create a variable that is a table: 
ie. VAR _SourceTable = FILTER(ALL('Date'[Year]),'Date'[Year]>=2010) 
and then later on you want to further filter this source table ie. VAR _MoreFiltering = FILTER(_SourceTable, [Year] >= 2013).

Measures are calculated in the current filter context (sometimes that's okay if you want to keep the value static) but you can't use CALCULATE() to adjust that after the fact.

View solution in original post

5 REPLIES 5
sunilbeckham
Helper I
Helper I

Thank you so much for your prompt and helpful responses @Erokor  and @Greg_Deckler 
This is why I love this community 🙂

I've been a longtime lurker, and only today decided to leave my hermit cave and start answering questions. I guess imposter syndrom has kept me from replying 🤣.  Time to go for that Superuser cape.

I can totally relate. Hopefully someday I'll gather enough wisdom from my lurking experience that I can leave my hermit cave too 😅
All the best in your quest for Superuser!

Erokor
Resolver II
Resolver II

Echoing @Greg_Deckler on this one, but with a little more information. Variables are calculated only once and then stored only once and cannot be re-used in their current context. The only exception to this is if you create a variable that is a table: 
ie. VAR _SourceTable = FILTER(ALL('Date'[Year]),'Date'[Year]>=2010) 
and then later on you want to further filter this source table ie. VAR _MoreFiltering = FILTER(_SourceTable, [Year] >= 2013).

Measures are calculated in the current filter context (sometimes that's okay if you want to keep the value static) but you can't use CALCULATE() to adjust that after the fact.

Greg_Deckler
Super User
Super User

@sunilbeckham Doesn't work that way. Variables are not variable, they are static once calculated. 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.