<?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 Query optimization help needed in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Query-optimization-help-needed/m-p/3229527#M118460</link>
    <description>&lt;H2&gt;Background&lt;/H2&gt;&lt;P&gt;I currently have three measures, each used by the previous to achieve an outcome. I am certain this could be done in a more efficient way and am looking for help to get there.&amp;nbsp;&lt;/P&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;The Data&lt;/H2&gt;&lt;P&gt;Here is some made-up data that includes sales, product changes and terminations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;Measures&lt;/H2&gt;&lt;P&gt;The goal is to see what the membership base "looks like" at any point in time dynamically, i.e. it should only see the most recent row for a given member key. I also need to be able to see the cumulative count at any point in time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the measures which are giving me the desired result, but the performance is very slow on real data.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1 - Current Row = 
    VAR _Current = SELECTEDVALUE('Members'[MemberKey])
    VAR _Date = MAXX(
        FILTER(
            ALLSELECTED('Members'), 
            'Members'[MemberKey] = _Current
            ), 
        RELATED('Date'[Date])
        )
     
RETURN    
  CALCULATE(
            COUNTROWS('Members'),
            'Members'[MemberKey] = _Current,
            'Date'[Date] = _Date
        )


2 - Member Count At Date = 
CALCULATE(
    COUNTROWS('Members'),
    FILTER('Members', [1 - Current Row] = 1),
    'Members'[MemberStatus] = "active",
    'Date'[Date] &amp;lt;= MAX('Date'[Date])
    )

3 - Cumulative Member Count At Date = 
CALCULATE(
    [2 - Member Count At Date],
    ALL('Date'),
    'Date'[Date] &amp;lt;= MAX('Date'[Date])
)&lt;/LI-CODE&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;Expected output&lt;/H2&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to upload the minimal example I created but it seems I do not have permission to do that yet...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 May 2023 00:43:07 GMT</pubDate>
    <dc:creator>Trystan</dc:creator>
    <dc:date>2023-05-11T00:43:07Z</dc:date>
    <item>
      <title>Query optimization help needed</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Query-optimization-help-needed/m-p/3229527#M118460</link>
      <description>&lt;H2&gt;Background&lt;/H2&gt;&lt;P&gt;I currently have three measures, each used by the previous to achieve an outcome. I am certain this could be done in a more efficient way and am looking for help to get there.&amp;nbsp;&lt;/P&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;The Data&lt;/H2&gt;&lt;P&gt;Here is some made-up data that includes sales, product changes and terminations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;Measures&lt;/H2&gt;&lt;P&gt;The goal is to see what the membership base "looks like" at any point in time dynamically, i.e. it should only see the most recent row for a given member key. I also need to be able to see the cumulative count at any point in time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the measures which are giving me the desired result, but the performance is very slow on real data.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1 - Current Row = 
    VAR _Current = SELECTEDVALUE('Members'[MemberKey])
    VAR _Date = MAXX(
        FILTER(
            ALLSELECTED('Members'), 
            'Members'[MemberKey] = _Current
            ), 
        RELATED('Date'[Date])
        )
     
RETURN    
  CALCULATE(
            COUNTROWS('Members'),
            'Members'[MemberKey] = _Current,
            'Date'[Date] = _Date
        )


2 - Member Count At Date = 
CALCULATE(
    COUNTROWS('Members'),
    FILTER('Members', [1 - Current Row] = 1),
    'Members'[MemberStatus] = "active",
    'Date'[Date] &amp;lt;= MAX('Date'[Date])
    )

3 - Cumulative Member Count At Date = 
CALCULATE(
    [2 - Member Count At Date],
    ALL('Date'),
    'Date'[Date] &amp;lt;= MAX('Date'[Date])
)&lt;/LI-CODE&gt;&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;&lt;H2&gt;Expected output&lt;/H2&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to upload the minimal example I created but it seems I do not have permission to do that yet...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 00:43:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Query-optimization-help-needed/m-p/3229527#M118460</guid>
      <dc:creator>Trystan</dc:creator>
      <dc:date>2023-05-11T00:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Query optimization help needed</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Query-optimization-help-needed/m-p/3230408#M118509</link>
      <description>&lt;P&gt;You can try the below. You will need to have a column which has unique values for each row. If your model doesn't have one you can use Power Query to add an index column. When you have the column, you need to go into modelling view and mark that column as the key for the table.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Active members at date =
VAR ReferenceDate =
    MAX ( 'Date'[Date] )
VAR SummaryTable =
    CALCULATETABLE (
        INDEX (
            1,
            Members,
            ORDERBY ( Members[Date], DESC ),
            PARTITIONBY ( Members[Member Key] )
        ),
        'Date'[Date] &amp;lt;= ReferenceDate
    )
VAR Result =
    COUNTROWS ( FILTER ( SummaryTable, [Member Status] = "active" ) )
RETURN
    Result
&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 11 May 2023 09:10:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Query-optimization-help-needed/m-p/3230408#M118509</guid>
      <dc:creator>johnt75</dc:creator>
      <dc:date>2023-05-11T09:10:38Z</dc:date>
    </item>
  </channel>
</rss>

