<?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: CALCULATE / FILTER DAX code issue in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3352542#M125903</link>
    <description>&lt;P&gt;Hi Owen, thanks for explaining this so clearly. The solution worked. Thanks, Sam&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jul 2023 06:50:35 GMT</pubDate>
    <dc:creator>SJHardeman</dc:creator>
    <dc:date>2023-07-27T06:50:35Z</dc:date>
    <item>
      <title>CALCULATE / FILTER DAX code issue</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3351525#M125857</link>
      <description>&lt;DIV&gt;&lt;SPAN&gt;Hi, I have been trying to use the below DAX code to do the following: &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;(1) take the Data table, sum the FTE column filtered whereby the date in the Period column equals a measure [Latest month]&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;(2) take the Data table, sum the FTE column filtered whereby the date in the Period column equals a measure [First month]&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;(3) subtract (2) from (1) to get the FTE_change&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;For some reason this isn't working, for each of (1) and (2) the result is the Sum of all FTE in Data, i.e. the Filter parts of the formulae are not working, and therefore (3) = (2) - (1) = 0.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I can't share my model or underlying data, but does anyone have any ideas what may be causing the issue here?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;FTE_change = &lt;/SPAN&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;SUM&lt;/SPAN&gt;&lt;SPAN&gt;(Data&lt;/SPAN&gt;&lt;SPAN&gt;[FTE]&lt;/SPAN&gt;&lt;SPAN&gt;),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;FILTER&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;Data&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;[Latest month]&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;'Data'&lt;/SPAN&gt;&lt;SPAN&gt;[Period]&lt;/SPAN&gt;&lt;SPAN&gt;)) - &lt;/SPAN&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;SUM&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;Data&lt;/SPAN&gt;&lt;SPAN&gt;[FTE]&lt;/SPAN&gt;&lt;SPAN&gt;),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;FILTER&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;Data&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;[First month]&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;'Data'&lt;/SPAN&gt;&lt;SPAN&gt;[Period]&lt;/SPAN&gt;&lt;SPAN&gt;))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;For info, the [Latest month] measure =&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;LASTDATE&lt;/SPAN&gt;&lt;SPAN&gt;(Data[Period]) and&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;the [First month] measure =&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;FIRSTDATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;Data&lt;/SPAN&gt;&lt;SPAN&gt;[Period]&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 26 Jul 2023 18:24:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3351525#M125857</guid>
      <dc:creator>SJHardeman</dc:creator>
      <dc:date>2023-07-26T18:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE / FILTER DAX code issue</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3352106#M125884</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="594667" data-lia-user-login="SJHardeman" class="lia-mention lia-mention-user"&gt;SJHardeman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, here's a bit of an explanation of what's going on:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;When a measure such as [Latest month] is invoked, it is automatically surrounded by a hidden CALCULATE (...).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The FILTER function is an iterator which evaluates the condition in the 2nd argument in the &lt;STRONG&gt;row context&lt;/STRONG&gt; of each row the table specified in the 1st argument.&lt;/LI&gt;
&lt;LI&gt;Within a row context, CALCULATE triggers context transition, which transforms the row context into an equivalent filter context. All columns within the "current row" become filters.&lt;/LI&gt;
&lt;LI&gt;Applying this to your measure: [Latest month] and [First month] are evaluated within a filter context corresponding to each row of the Data table. This means the conditions&amp;nbsp;&lt;SPAN&gt;[Latest month]&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;'Data'&lt;/SPAN&gt;&lt;SPAN&gt;[Period] and [First month] = 'Data'[Period] are always TRUE.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;SPAN&gt;Here is a good article on this overall topic:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://www.sqlbi.com/articles/understanding-context-transition-in-dax/" target="_blank"&gt;https://www.sqlbi.com/articles/understanding-context-transition-in-dax/&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A couple of side points:&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;FIRSTDATE and LASTDATE return tables (1 row x 1 column), and are typically used as SetFilter arguments within CALCULATE. If you just want to return a scalar value, you can use MIN or MAX.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;For any date-based filtering, it is best to create a separate 'Date' dimension. However, I won't worry about this for the purpose of this question.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can fix this a few ways. I would personally recommend creating these measures:&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;FTE Sum = 
SUM ( data[FTE] )&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;FTE First Month = 
CALCULATE(
    [FTE Sum],
    FIRSTDATE ( data[Period] )
)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;FTE Last Month = 
CALCULATE(
    [FTE Sum],
    LASTDATE ( data[Period] )
)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;FTE_change = 
[FTE Last Month] - [FTE First Month]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this work for you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 00:53:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3352106#M125884</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-07-27T00:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE / FILTER DAX code issue</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3352542#M125903</link>
      <description>&lt;P&gt;Hi Owen, thanks for explaining this so clearly. The solution worked. Thanks, Sam&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 06:50:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3352542#M125903</guid>
      <dc:creator>SJHardeman</dc:creator>
      <dc:date>2023-07-27T06:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE / FILTER DAX code issue</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3353047#M125930</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;,&amp;nbsp;how would I modify the code if I wanted to also have another measure which calculated FTE_change for [FTE Last Month] - [FTE Penultimate Month]. Using the below&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;results in an error&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;FTE Penultimate Month = &lt;/SPAN&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;[FTE Sum]&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;EDATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;LASTDATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;Staff_Collated_HQ_WFPT&lt;/SPAN&gt;&lt;SPAN&gt;[Period]&lt;/SPAN&gt;&lt;SPAN&gt;),-&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Thu, 27 Jul 2023 10:56:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3353047#M125930</guid>
      <dc:creator>SJHardeman</dc:creator>
      <dc:date>2023-07-27T10:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: CALCULATE / FILTER DAX code issue</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3353141#M125937</link>
      <description>&lt;P&gt;Hi again&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="594667" data-lia-user-login="SJHardeman" class="lia-mention lia-mention-user"&gt;SJHardeman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;FTE Penultimate Month =
CALCULATE (
    [FTE Sum],
    PREVIOUSMONTH (
        LASTDATE ( Staff_Collated_HQ_WFPT[Period] )
    )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Notes:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;EDATE &lt;/STRONG&gt;returns a scalar value (not a table) so isn't suitable to be used directly as a SetFilter argument of &lt;STRONG&gt;CALCULATE&lt;/STRONG&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;PREVIOUSMONTH &lt;/STRONG&gt;instead&amp;nbsp;returns a table containing all dates of the month preceding the minimum date in the argument that it is passed.&lt;/LI&gt;
&lt;LI&gt;In this case, this will be the month preceding the maximum &lt;STRONG&gt;Period&lt;/STRONG&gt;&amp;nbsp;date in the filter context which is returned by &lt;STRONG&gt;LASTDATE&lt;/STRONG&gt; (as a 1x1 table).&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;DATEADD ( LASTDATE ( ... ), -1, MONTH&lt;/STRONG&gt; could also be an option here, but as you are not using a Date table at the moment, this would only work reliably if the &lt;STRONG&gt;Period&lt;/STRONG&gt; dates are aligned in their position within the month (e.g. always first or last day, or nth day of the month).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a general comment, I would highly recommend creating a Date/Calendar dimension table and using it for all date filtering.&lt;/P&gt;
&lt;P&gt;(See this article for example&amp;nbsp;&lt;A href="https://www.daxpatterns.com/standard-time-related-calculations/" target="_blank" rel="noopener"&gt;https://www.daxpatterns.com/standard-time-related-calculations/&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;This is because time intelligence functions such as &lt;STRONG&gt;LASTDATE&lt;/STRONG&gt; and &lt;STRONG&gt;PREVIOUSMONTH&lt;/STRONG&gt; base their logic on dates that exist in the date column provided (in your case the &lt;STRONG&gt;Period&lt;/STRONG&gt; column). With more complex date filters, things can go awry if you don't have a Date table with contiguous dates and complete months that is marked as a date table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 11:35:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/CALCULATE-FILTER-DAX-code-issue/m-p/3353141#M125937</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-07-27T11:35:30Z</dc:date>
    </item>
  </channel>
</rss>

