<?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: Count Distinct date based on current row condition in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/838036#M6280</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="184115" data-lia-user-login="renegar" class="lia-mention lia-mention-user"&gt;renegar&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Give something like this a try.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Date Count = 
VAR _RowDate = 'Table'[Date]
RETURN
CALCULATE ( 
    COUNTROWS ( VALUES ( 'Table'[Date] ) ), 
    ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] ),
    'Table'[Date] &amp;lt; _RowDate
)&lt;/LI-CODE&gt;
&lt;P&gt;VALUES ( 'Table'[Date] ) gives us the unique list of dates for that row&lt;BR /&gt;we expand it to the range of Cat 1 and Cat 2 with&amp;nbsp;ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] ) &lt;BR /&gt;then bring it back to just the lower dates with the&amp;nbsp;'Table'[Date] &amp;lt; _RowDate&lt;/P&gt;
&lt;P&gt;Then we count the rows left so the number of unique dates for that cat 1 and cat 2 that are lower than the current row date.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2019 01:08:22 GMT</pubDate>
    <dc:creator>jdbuchanan71</dc:creator>
    <dc:date>2019-11-06T01:08:22Z</dc:date>
    <item>
      <title>Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837775#M6267</link>
      <description>&lt;P&gt;I have a table [sample] with the following structure&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Category1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Category2&amp;nbsp; &amp;nbsp; &amp;nbsp;Result&lt;/P&gt;&lt;P&gt;1-1-19&amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Square&lt;/P&gt;&lt;P&gt;3-1-19&amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Square&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;3-1-19&amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Square&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;4-1-19&amp;nbsp; &amp;nbsp; &amp;nbsp;Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Square&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to know for a particular Category1 and Category 2 a measure column, that tell me the unique dates prior to the current row date&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the DAX that i'm using, which works&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;=CALCULATE(DISTINCTCOUNT([Date]),filter(all(sample),earlier(date)&amp;gt;[date] &amp;amp;&amp;amp; earlier([category1])=[category1] &amp;amp;&amp;amp; earlier(category2)=Earnings[category2]))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that when I use this on a large data set (2.6m rows), it pretty much hangs Power Pivot&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a more optimal way to write this DAX expression?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 18:41:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837775#M6267</guid>
      <dc:creator>renegar</dc:creator>
      <dc:date>2019-11-05T18:41:50Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837784#M6269</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="184115" data-lia-user-login="renegar" class="lia-mention lia-mention-user"&gt;renegar&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Try using var and return rather than earlier. Here is a good post.&amp;nbsp;&lt;A href="https://www.sqlbi.com/articles/variables-in-dax/" target="_blank"&gt;https://www.sqlbi.com/articles/variables-in-dax/&lt;/A&gt;&amp;nbsp;Using a variable can capture the date outside of the CALCULATE() and replace all the EARLIER functions which are no longer recommended for use. It should speed everything up.&lt;BR /&gt;Let me know if you have any questions.&lt;BR /&gt;&lt;BR /&gt;If this solves your issues, please mark it as the &lt;STRONG&gt;solution, &lt;/STRONG&gt;so that others can find it easily. &lt;STRONG&gt;Kudos &lt;/STRONG&gt;are nice too.&lt;BR /&gt;Nathaniel&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 18:51:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837784#M6269</guid>
      <dc:creator>Nathaniel_C</dc:creator>
      <dc:date>2019-11-05T18:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837822#M6271</link>
      <description>&lt;P&gt;Ended up doing this, but performance didn't change much, still hangs:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;= var CurrentDate = Table1[Date]&lt;/P&gt;&lt;P&gt;var CurrentCat1 = Table1[Category1]&lt;/P&gt;&lt;P&gt;var CurrentCat2=Table1[Category2]&lt;/P&gt;&lt;P&gt;return&lt;/P&gt;&lt;P&gt;calculate(DISTINCTCOUNT([Date]),filter(Table1,[Date]&amp;lt;CurrentDate &amp;amp;&amp;amp; [Category1]=CurrentCat1 &amp;amp;&amp;amp; CurrentCat2=Table1[Category2]))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 19:43:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837822#M6271</guid>
      <dc:creator>renegar</dc:creator>
      <dc:date>2019-11-05T19:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837836#M6274</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="184115" data-lia-user-login="renegar" class="lia-mention lia-mention-user"&gt;renegar&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;Strange... maybe relationships the tables that form the sub measures?&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="106637" data-lia-user-login="jdbuchanan71" class="lia-mention lia-mention-user"&gt;jdbuchanan71&lt;/a&gt;&amp;nbsp; any thoughts?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 20:00:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/837836#M6274</guid>
      <dc:creator>Nathaniel_C</dc:creator>
      <dc:date>2019-11-05T20:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/838036#M6280</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="184115" data-lia-user-login="renegar" class="lia-mention lia-mention-user"&gt;renegar&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Give something like this a try.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Date Count = 
VAR _RowDate = 'Table'[Date]
RETURN
CALCULATE ( 
    COUNTROWS ( VALUES ( 'Table'[Date] ) ), 
    ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] ),
    'Table'[Date] &amp;lt; _RowDate
)&lt;/LI-CODE&gt;
&lt;P&gt;VALUES ( 'Table'[Date] ) gives us the unique list of dates for that row&lt;BR /&gt;we expand it to the range of Cat 1 and Cat 2 with&amp;nbsp;ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] ) &lt;BR /&gt;then bring it back to just the lower dates with the&amp;nbsp;'Table'[Date] &amp;lt; _RowDate&lt;/P&gt;
&lt;P&gt;Then we count the rows left so the number of unique dates for that cat 1 and cat 2 that are lower than the current row date.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 01:08:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/838036#M6280</guid>
      <dc:creator>jdbuchanan71</dc:creator>
      <dc:date>2019-11-06T01:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct date based on current row condition</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/838872#M6308</link>
      <description>&lt;P&gt;This works, and it's amazingly quick too on 2.7m rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="106637" data-lia-user-login="jdbuchanan71" class="lia-mention lia-mention-user"&gt;jdbuchanan71&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="159166" data-lia-user-login="Nathaniel_C" class="lia-mention lia-mention-user"&gt;Nathaniel_C&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 15:59:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-Distinct-date-based-on-current-row-condition/m-p/838872#M6308</guid>
      <dc:creator>renegar</dc:creator>
      <dc:date>2019-11-06T15:59:28Z</dc:date>
    </item>
  </channel>
</rss>

