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
Anonymous
Not applicable

AVERAGEX with CALCULATE as a Quick Measure - why?

Hello friends,

I've just used a Quick Measure (AWESOME option!!!) to calculate Average per Category measure.

The DAX that was generated was:

AMT average per Dept =
AVERAGEX(KEEPFILTERS(VALUES('Fact'[Dept])), CALCULATE(SUM('Fact'[AMT])))

I does works fine, but I just don't understand why it uses CALCULATE inside AVERAGEX

If I omit CALCULATE: 

AVERAGEX(KEEPFILTERS(VALUES('Fact'[Dept])), SUM('Fact'[AMT]))

I get wrong answer, but WHY?

If you have a link or an explanation - I will appreciate it!

Thank you!

Michael

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Anonymous

 

CALCULATE is required whenever you want to turn a row context into an equivalent filter context (context transition).

 

In your case, AVERAGEX iterates over the rows of VALUES('Fact'[Dept]), creating a row context for each row, and evaluates the 2nd argument in each row context in turn.

 

Without CALCULATE, the expression SUM('Fact'[AMT]) would have the same value for every row, being the sum evaluated in the original filter context. An aggregate expression like SUM('Fact'[AMT]) is calculated in the current filter context, which is not impacted by row context.

 

With CALCULATE, the row context, which in this case corresponds to a particular value of 'Fact'[Dept], is turned into a filter and added to the filter context, so SUM('Fact'[AMT]) is evaluated in that modified filter context, i.e. the sum is now filtered to a particular Dept.

 

I would suggest getting a copy of The Definitive Guide to DAX, or reading these articles (or others on the same topic):

(I have ignored the KEEPFILTERS part of the expression btw)

 

Regards

Owen 🙂


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @Anonymous

 

CALCULATE is required whenever you want to turn a row context into an equivalent filter context (context transition).

 

In your case, AVERAGEX iterates over the rows of VALUES('Fact'[Dept]), creating a row context for each row, and evaluates the 2nd argument in each row context in turn.

 

Without CALCULATE, the expression SUM('Fact'[AMT]) would have the same value for every row, being the sum evaluated in the original filter context. An aggregate expression like SUM('Fact'[AMT]) is calculated in the current filter context, which is not impacted by row context.

 

With CALCULATE, the row context, which in this case corresponds to a particular value of 'Fact'[Dept], is turned into a filter and added to the filter context, so SUM('Fact'[AMT]) is evaluated in that modified filter context, i.e. the sum is now filtered to a particular Dept.

 

I would suggest getting a copy of The Definitive Guide to DAX, or reading these articles (or others on the same topic):

(I have ignored the KEEPFILTERS part of the expression btw)

 

Regards

Owen 🙂


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
Anonymous
Not applicable

Thank you for the detailed explanation Owen!

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.