<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic CALCULATE() or iterator function in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1210338#M19476</link>
    <description>&lt;P&gt;There seems to be some personal preference involved with the use of CALCULATE(MAX()) versus MAXX() etc.&amp;nbsp; I am still trying to understand the arguments pro and contra (as well as the context transition differences).&amp;nbsp; I looked at the query plans in DAX Studio for the following versions of the same measure&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;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 &amp;amp;&amp;amp; 'Table'[value]="1"),'Table'[date])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;They yield the same result. The query plan for the calculate() version has 21 lines and two scans. The final query is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[pid] = 123 VAND
	'Table'[value] = '1';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The query plan for the maxx() version has only 12 lines, but the final query is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[value] = '1' VAND
	 ( PFCASTCOALESCE ( 'Table'[pid] AS  INT ) = COALESCE ( 'CallbackDataID ( p ) ' ()  )  ) ;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the CallbackDataID is all jumpy and colorful, telling me it rather shouldn't be there.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Granted, my test data set is tiny, but isn't that a sign that in this scenario calculate() produces the better query?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp; Adding TREATAS() to the mix.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;var p=SELECTEDVALUE('Table'[pid])
var c2=CALCULATE(max('Table'[date]),all('Table'),TREATAS({(p,"1")},'Table'[pid],'Table'[value]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It produces the same final query as the filter enumeration, but&amp;nbsp; but&amp;nbsp;seems to perform a little bit leaner.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jul 2020 11:39:22 GMT</pubDate>
    <dc:creator>lbendlin</dc:creator>
    <dc:date>2020-07-09T11:39:22Z</dc:date>
    <item>
      <title>CALCULATE() or iterator function</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1210338#M19476</link>
      <description>&lt;P&gt;There seems to be some personal preference involved with the use of CALCULATE(MAX()) versus MAXX() etc.&amp;nbsp; I am still trying to understand the arguments pro and contra (as well as the context transition differences).&amp;nbsp; I looked at the query plans in DAX Studio for the following versions of the same measure&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;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 &amp;amp;&amp;amp; 'Table'[value]="1"),'Table'[date])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;They yield the same result. The query plan for the calculate() version has 21 lines and two scans. The final query is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[pid] = 123 VAND
	'Table'[value] = '1';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The query plan for the maxx() version has only 12 lines, but the final query is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;SELECT
MAX ( 'Table'[date] )
FROM 'Table'
WHERE
	'Table'[value] = '1' VAND
	 ( PFCASTCOALESCE ( 'Table'[pid] AS  INT ) = COALESCE ( 'CallbackDataID ( p ) ' ()  )  ) ;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the CallbackDataID is all jumpy and colorful, telling me it rather shouldn't be there.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Granted, my test data set is tiny, but isn't that a sign that in this scenario calculate() produces the better query?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp; Adding TREATAS() to the mix.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;var p=SELECTEDVALUE('Table'[pid])
var c2=CALCULATE(max('Table'[date]),all('Table'),TREATAS({(p,"1")},'Table'[pid],'Table'[value]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It produces the same final query as the filter enumeration, but&amp;nbsp; but&amp;nbsp;seems to perform a little bit leaner.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 11:39:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1210338#M19476</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2020-07-09T11:39:22Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE() or iterator function</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1219398#M19780</link>
      <description>&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd recommend reading up on SUM vs SUMX articles for more detail and context.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://exceleratorbi.com.au/use-sum-vs-sumx/" target="_blank"&gt;https://exceleratorbi.com.au/use-sum-vs-sumx/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://radacad.com/sum-vs-sumx-what-is-the-difference-of-the-two-dax-functions-in-power-bi" target="_blank"&gt;https://radacad.com/sum-vs-sumx-what-is-the-difference-of-the-two-dax-functions-in-power-bi&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 20:45:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1219398#M19780</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2020-07-13T20:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE() or iterator function</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1223972#M19897</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;Best&lt;BR /&gt;D</description>
      <pubDate>Wed, 15 Jul 2020 08:26:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-or-iterator-function/m-p/1223972#M19897</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-15T08:26:02Z</dc:date>
    </item>
  </channel>
</rss>

