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

want to know the difference

what is the difference between CALCULATE(SUM('Fact Sale'[Quantity])) and SUM('Fact Sale'[Quantity])?

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

@Hi @Herndon_powerbi 

 

The only difference between these expressions is that, in the expression with CALCULATE, context transition occurs before the SUM(...) expression is evaluated.

 

"Context transition" means all row contexts are converted into equivalent filters.

 

This means that these expressions would return different results only if one or more row contexts existed, such as if they were evaluated in a calculated column or within the row context of an iterator.

 

For example, if you created calculated columns with these expressions in (say) a Customer table, the expression with CALCULATE would convert the current row of the Customer into an equivalent filter, and would give you something like the sum of Quantity for that Customer. Without CALCULATE, the expression would return an unfiltered sum of Quantity. 

 

One thing to note is that a reference to a measure automatically "wraps" the measure's expression in CALCULATE.

 

This is a good article on this topic:

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

 

Regards,

Owen


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

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

@Hi @Herndon_powerbi 

 

The only difference between these expressions is that, in the expression with CALCULATE, context transition occurs before the SUM(...) expression is evaluated.

 

"Context transition" means all row contexts are converted into equivalent filters.

 

This means that these expressions would return different results only if one or more row contexts existed, such as if they were evaluated in a calculated column or within the row context of an iterator.

 

For example, if you created calculated columns with these expressions in (say) a Customer table, the expression with CALCULATE would convert the current row of the Customer into an equivalent filter, and would give you something like the sum of Quantity for that Customer. Without CALCULATE, the expression would return an unfiltered sum of Quantity. 

 

One thing to note is that a reference to a measure automatically "wraps" the measure's expression in CALCULATE.

 

This is a good article on this topic:

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

 

Regards,

Owen


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

CALCULATE does a few things. It can add and remove filters. However it's big power is "Context Transition" which takes the current row ("Row Context") and adds all of it's columns as filters (adds them to the "Filter Context")

 

If you use SUM('Fact Sale'[Quantity]) in a calculated column in a product table it will return the total of all quantities in the fact table.

 

If you use CALCULATE ( SUM ('Fact Sale'[Qantity] ) in a calculated column in a product table it will return the total of quantites in the fact table filtered by that product.

 

That's the most obvious place you'd see the difference. However you also get a row context in an iterator like SUMX so would need to use CALCULATE within the expression there if you needed to move the row into a filter. (Note an explicit measure [Total Quanity] has an implicit calculate).

This probably explains it better than me: Understanding Context Transition - SQLBI



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
sokg
Solution Supplier
Solution Supplier

It depends.

 

Where do you use this measure???
In a calculated column???

 

Regards,

Sokratis

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.