<?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 in SCD2 dimension - optimization in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487216#M28488</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second should do the same as the first. If the results are not right, it probably has to do with the relationships on the model. I made some assumptions, like no relationship between the date table and the Units table, that seemingly do not hold.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Nov 2020 11:30:19 GMT</pubDate>
    <dc:creator>AlB</dc:creator>
    <dc:date>2020-11-11T11:30:19Z</dc:date>
    <item>
      <title>Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484773#M28375</link>
      <description>&lt;P&gt;Hello PB community!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anybody please help me with DAX optimization in this scenario:&lt;/P&gt;&lt;P&gt;I have Tabular model with 10 dimension tables, no real facts. One of the user request is to count units in Units table through time.&amp;nbsp; This table is a SCD2 table type and only 2 dates in table are valid_from and valid_to. They will use year-month and year attributes on reports and in slicers. Beside date attribute they wan't to use attributes Units Type and Country of Origin.&lt;/P&gt;&lt;P&gt;I did't connect my Calendar table with Units table in Tabular model and I have combined this DAX code:&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;LI-CODE lang="markup"&gt;Cnt of units:=
CALCULATE (
   // DISTINCTCOUNT ( 'Units'[unit_id] ),
   SUMX(VALUES('Units'[unit_id] ),1),
    FILTER (
        'Units',
        COUNTROWS (
            FILTER (
                VALUES ( 'CalendarTable'[Date] ),         
                'CalendarTable'[Date]&amp;gt;= 'Units'[unit_valid_from]
                    &amp;amp;&amp;amp; 'CalendarTable'[Date] &amp;lt;= 'Units'[unit_valid_to]
            )
        )
    )
)&lt;/LI-CODE&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;So this code is doing some sort of cross join / cross aplly on date table and since Units table isn't really small (it has 500K+ rows) it's slow. Calendar table is from DB Model, not from PB and has data from 2015 to 2025.&lt;/P&gt;&lt;P&gt;Current server timing from dax studio are like this:&lt;/P&gt;&lt;P&gt;In grid: Year Month, Unit type, Country of origin and my metric&lt;/P&gt;&lt;P&gt;Filter: Year in (2020, 2019)&lt;/P&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I already replaced DISTINCOUNT with SUMX code. With using DISTINCOUNT function:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&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;&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;&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;So, are there are ideas how to optimize this code even more? Is there any way I can solve this differently (connections in Tabular with Calendar table, etc..?)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similar problems: &lt;A href="https://community.powerbi.com/t5/Desktop/How-to-find-number-of-customers-in-a-SCD2-type-table/m-p/97281" target="_self"&gt;https://community.powerbi.com/t5/Desktop/How-to-find-number-of-customers-in-a-SCD2-type-table/m-p/97281&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>Tue, 10 Nov 2020 11:09:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484773#M28375</guid>
      <dc:creator>bikeoholic</dc:creator>
      <dc:date>2020-11-10T11:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484901#M28380</link>
      <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suppose you cannot share the pbix?&lt;/P&gt;
&lt;P&gt;Is Cnt of units&amp;nbsp; a measure? Are you using it with any filters applied? What are the active relationships?&lt;/P&gt;
&lt;P&gt;Please explain a bit more what you mean by&amp;nbsp;&lt;EM&gt;One of the user request is to count units in Units table through time&lt;/EM&gt; &lt;/P&gt;
&lt;P&gt;Do ee want the units that have &lt;STRONG&gt;both&lt;/STRONG&gt; 'Units'[unit_valid_from] and 'Units'[unit_valid_to] within the date period selected??&lt;/P&gt;
&lt;P&gt;Best if you can provide &lt;STRONG&gt;an example&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2020 12:17:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484901#M28380</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-10T12:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484949#M28383</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pending the response to my questions above, try this:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Cnt of units V2 :=
CALCULATE (
    SUMX ( VALUES ( 'Units'[unit_id] ), 1 ),
    VAR minDate_ =MIN ( 'CalendarTable'[Date] )
    VAR maxDate_ = MAX ( 'CalendarTable'[Date] )
    RETURN
        FILTER (
            ALL ( 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
            NOT ( maxDate_ &amp;lt; 'Units'[unit_valid_from]
                || minDate_ &amp;gt; 'Units'[unit_valid_to] )
        )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2020 12:35:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484949#M28383</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-10T12:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484962#M28384</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this could be better depending on the data distribution. It leverages the filters being applied to reduce the number of rows to scan&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Cnt of units :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            SUMMARIZE (
                'Units',
                'Units'[unit_id],
                'Units'[unit_valid_from],
                'Units'[unit_valid_to]
            ),
            NOT ( maxDate_ &amp;lt; 'Units'[unit_valid_from]
                || minDate_ &amp;gt; 'Units'[unit_valid_to] )
        )
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2020 12:38:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1484962#M28384</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-10T12:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1485344#M28400</link>
      <description>&lt;P&gt;Hi !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No, sorry, I cannot share PB file &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Yes, metric ofcourse, named "&lt;STRIKE&gt;Cnt&lt;/STRIKE&gt; Count of Units". I didn't wrote down as well that this DAX code gets the job done..it works, but it's slow. I mean 25s is not much, but table will get bigger and bigger.&lt;/P&gt;&lt;P&gt;There are only active relationships in Tabular model. To me it's a straight forward Tabular model. Nothing "fancy". This metric is defined in Tabular and not in PB.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is an example of&amp;nbsp;few &lt;STRONG&gt;random&lt;/STRONG&gt; rows and columns from "Units" table. For each unit there is a valid from and to date. Unit can be valid throuh many months or just 1 month. They can be valid 1 day, but I don't care for day/date level. I have to count 1st row only in Jan 2020, 2nd row in all next months..and so on.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is an example of a table visualisation on a report:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tnx, I will try your suggestions!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2020 15:43:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1485344#M28400</guid>
      <dc:creator>bikeoholic</dc:creator>
      <dc:date>2020-11-10T15:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487190#M28486</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="92178" data-lia-user-login="AlB" class="lia-mention lia-mention-user"&gt;AlB&lt;/a&gt;Thank you, your first code works like a charm &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Result after your DAX code:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In second code I don't get the right results. I didn't had time yet to go in deep why..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards, v&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 11:18:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487190#M28486</guid>
      <dc:creator>bikeoholic</dc:creator>
      <dc:date>2020-11-11T11:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487216#M28488</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second should do the same as the first. If the results are not right, it probably has to do with the relationships on the model. I made some assumptions, like no relationship between the date table and the Units table, that seemingly do not hold.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 11:30:19 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487216#M28488</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-11T11:30:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count in SCD2 dimension - optimization</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487258#M28491</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="269670" data-lia-user-login="bikeoholic" class="lia-mention lia-mention-user"&gt;bikeoholic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4 secs is still considerable and it's engaging the FE significantly.&amp;nbsp; I'm curious if this version will be faster:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Cnt of units V2_B :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            ALL ( 'Units'[unit_id], 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
            NOT ( maxDate_ &amp;lt; 'Units'[unit_valid_from]
                || minDate_ &amp;gt; 'Units'[unit_valid_to] )
        )
    )
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;or this:&lt;/P&gt;
&lt;PRE class="language-markup"&gt;&lt;CODE&gt;Cnt of units V2_C :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            ALL ( 'Units'[unit_id], 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
             maxDate_ &amp;gt;= 'Units'[unit_valid_from] &amp;amp;&amp;amp; minDate_ &amp;lt;= 'Units'[unit_valid_to]
        )
    )&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 11:49:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Count-in-SCD2-dimension-optimization/m-p/1487258#M28491</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-11T11:49:54Z</dc:date>
    </item>
  </channel>
</rss>

