<?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 Re: How to optimize this DAX query? in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1073255#M15010</link>
    <description>&lt;P&gt;How is Logs table related to People table? Is there a PeopleID column on Logs table? If so, the calculation can be simplified to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR _days = 7&lt;BR /&gt;VAR _CurrentDate = MAX('DateTable'[Date])&lt;BR /&gt;RETURN&lt;BR /&gt;CALCULATE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; DISTINCTCOUNT(Logs[PeopleID]),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; FILTER(ALL('DateTable'[Date]), [Date] &amp;gt; _CurrentDate - _days, [Date] &amp;lt;= _CurrentDate)&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First try the above to see if perf is better. + 0 afterwards if you must. Watch this video which talks about the reasons for making the changes.&amp;nbsp;&lt;A href="https://www.youtube.com/watch?v=WSfc2hY1_dQ" target="_blank"&gt;https://www.youtube.com/watch?v=WSfc2hY1_dQ&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 May 2020 15:12:19 GMT</pubDate>
    <dc:creator>jeffrey_wang</dc:creator>
    <dc:date>2020-05-05T15:12:19Z</dc:date>
    <item>
      <title>How to optimize this DAX query?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1073171#M15006</link>
      <description>&lt;LI-CODE lang="markup"&gt;[Active People] = 
    
VAR days = 7

VAR result = CALCULATE(
	DISTINCTCOUNT(People[ID]),
	FILTER(People,
		CALCULATE(COUNTROWS(Logs),
			FILTER(Logs,
			AND(
				Logs[LogDate] 
					&amp;gt;
				MAX('DateTable'[Date])- days,
				Logs[LogDate] 
					&amp;lt;=
				MAX('DateTable'[Date])
				)
			)
		) &amp;gt;= 1
	)
) + 0

return result&lt;/LI-CODE&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;This counts the number of active people. Active people must have at least 1 log entry on the Logs table within the date range.&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Each people has thousand of logs and the way I'm checking is filtering the Log table with dates &amp;amp; People[ID] and checking if their number of logs is greater than 1.&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Is it possible to just get the first log that matches the filters so I won't have to do the COUNTROWS method?&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 14:43:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1073171#M15006</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-05-05T14:43:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to optimize this DAX query?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1073255#M15010</link>
      <description>&lt;P&gt;How is Logs table related to People table? Is there a PeopleID column on Logs table? If so, the calculation can be simplified to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR _days = 7&lt;BR /&gt;VAR _CurrentDate = MAX('DateTable'[Date])&lt;BR /&gt;RETURN&lt;BR /&gt;CALCULATE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; DISTINCTCOUNT(Logs[PeopleID]),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; FILTER(ALL('DateTable'[Date]), [Date] &amp;gt; _CurrentDate - _days, [Date] &amp;lt;= _CurrentDate)&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First try the above to see if perf is better. + 0 afterwards if you must. Watch this video which talks about the reasons for making the changes.&amp;nbsp;&lt;A href="https://www.youtube.com/watch?v=WSfc2hY1_dQ" target="_blank"&gt;https://www.youtube.com/watch?v=WSfc2hY1_dQ&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 15:12:19 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1073255#M15010</guid>
      <dc:creator>jeffrey_wang</dc:creator>
      <dc:date>2020-05-05T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to optimize this DAX query?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1074180#M15044</link>
      <description>&lt;LI-CODE lang="markup"&gt;// The measure below is the fastest way to
// get what you want on condition that your
// model is correctly built, which means
// that the fact table Logs joins to the dimension
// People on PersonID and the model has a
// proper calendar (date dimension) named Dates
// and joins to the fact table on [Date].

[# Active People] =
	CALCULATE(
		DISTINCTCOUNT( Logs[PersonID] ),
		DATESINPERIOD(
			MAX( Dates[Date] ),
			-7,
			DAY
		)
	)
	
// Your measure is slow because not only 
// do you unnecessarily count rows but
// also because you are putting in filters
// the whole fact table. Please learn the
// golden rule of DAX programming:
// NEVER FILTER A TABLE WHEN YOU CAN FILTER
// A COLUMN.&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And please remember that a correct model means fast and simple DAX. A bad model means complex and slow DAX. It's as easy as that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best&lt;/P&gt;
&lt;P&gt;D&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 00:39:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-optimize-this-DAX-query/m-p/1074180#M15044</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-05-06T00:39:54Z</dc:date>
    </item>
  </channel>
</rss>

