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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
lbendlin
Super User
Super User

CALCULATE() or iterator function

There seems to be some personal preference involved with the use of CALCULATE(MAX()) versus MAXX() etc.  I am still trying to understand the arguments pro and contra (as well as the context transition differences).  I looked at the query plans in DAX Studio for the following versions of the same measure

 

 

var p=SELECTEDVALUE('Table'[pid])
var c=CALCULATE(max('Table'[date]),all('Table'),'Table'[pid]=p,'Table'[value]="1")


var p=SELECTEDVALUE('Table'[pid])
var c1=maxx(filter(all('Table'),'Table'[pid]=p && 'Table'[value]="1"),'Table'[date])

 

 

They yield the same result. The query plan for the calculate() version has 21 lines and two scans. The final query is 

 

 

SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[pid] = 123 VAND
	'Table'[value] = '1';

 

 

The query plan for the maxx() version has only 12 lines, but the final query is 

 

 

SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[value] = '1' VAND
	 ( PFCASTCOALESCE ( 'Table'[pid] AS  INT ) = COALESCE ( 'CallbackDataID ( p ) ' ()  )  ) ;

 

 

And the CallbackDataID is all jumpy and colorful, telling me it rather shouldn't be there. 

 

Granted, my test data set is tiny, but isn't that a sign that in this scenario calculate() produces the better query?

 

Edit:  Adding TREATAS() to the mix. 

 

var p=SELECTEDVALUE('Table'[pid])
var c2=CALCULATE(max('Table'[date]),all('Table'),TREATAS({(p,"1")},'Table'[pid],'Table'[value]))

 

It produces the same final query as the filter enumeration, but  but seems to perform a little bit leaner.

 

 

2 REPLIES 2
Anonymous
Not applicable

If you read The Definitive Guide to DAX by The Italians, you'll find a chapter about optimization. CALCULATE with simple (Boolean) filters is ALWAYS preferred to FILTER due to internal optimizations. Simple as that.

Best
D
AlexisOlson
Super User
Super User

As a general rule of thumb, it's usually preferred to not use iterator functions where they aren't needed. For many expressions, they will be internally optimized to the same final query but it's much easier to inadvertently write poorly performing code with iterators (e.g. putting IF functions inside of one where using a filter would be more efficient).

 

I'd recommend reading up on SUM vs SUMX articles for more detail and context.

 

https://exceleratorbi.com.au/use-sum-vs-sumx/

https://radacad.com/sum-vs-sumx-what-is-the-difference-of-the-two-dax-functions-in-power-bi

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.