<?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: Helper function for date comparison in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4219755#M167174</link>
    <description>&lt;P&gt;Glad it works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The "explicit measure" requirement means that calculation groups only affect measures that are defined in the model. The measure must be included directly in the visual or the measure reference used directly in the DAX expression where the calculation item is applied.&lt;/P&gt;
&lt;P&gt;Here is a good explanation:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sqlbi.com/articles/understanding-calculation-groups/#:~:text=Introducing%20calculation%20item%20application" target="_blank"&gt;https://www.sqlbi.com/articles/understanding-calculation-groups/#:~:text=Introducing%20calculation%20item%20application&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Oct 2024 20:33:09 GMT</pubDate>
    <dc:creator>OwenAuger</dc:creator>
    <dc:date>2024-10-04T20:33:09Z</dc:date>
    <item>
      <title>Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217684#M167134</link>
      <description>&lt;P&gt;I have many formulas which calculate a measure. For instace: sales, itemQty, returns, returnQty etc. etc. Basiscally, they all follow the same pattern. For instance:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;itemSales = SUM(ob_sales[paidAmount])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In this report, I have a conditional 'last year' setup. When people select only 1 of 3 days, i need to shift back 7*52 days. When a month is selected (ie: aug 1 till aug 31), then I want to use SAMEPERIODLASTYEAR. So, the measure below is working fine:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;itemSalesLY = 
VAR SelectedDatesCount = COUNTROWS(VALUES('calendar'[Date]))
VAR IsSmallSelection = SelectedDatesCount &amp;lt;= 27
VAR DateRangeLastYear = 
    CALCULATE(
        SUM(ob_sales[paidAmount]),
        DATEADD('calendar'[Date], -52*7, DAY)  
    )
VAR SalesLastYearExactDates = 
    CALCULATE(
        SUM(ob_sales[BetaaldExclBtw]),
        SAMEPERIODLASTYEAR('calendar'[Date])  
    )
RETURN
IF(
    IsSmallSelection,
    DateRangeLastYear,  
    SalesLastYearExactDates
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works like a charm, but as I said, I have many measures which need to use this LY date logic. So, I thought I'll create a lastYearDateHelper expression, but this is where it gets ugly.&lt;/P&gt;&lt;P&gt;Whatever i try, it always ends in errors like&amp;nbsp;&lt;EM&gt;A function ‘PLACEHOLDER’ has been used in a True/False expression that is used as a table filter expression. This is not allowed.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The goal is to have something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;itemSalesLY = 
CALCULATE(
    SUM(ob_sales[paidAmount]),
    [LastYearDateHelper]
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this even possible?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 11:04:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217684#M167134</guid>
      <dc:creator>TutanRamon</dc:creator>
      <dc:date>2024-10-04T11:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217888#M167142</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="545514" data-lia-user-login="TutanRamon" class="lia-mention lia-mention-user"&gt;TutanRamon&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend using a calculation group for this instead. It's not possible to have a "measure" return a table expression then use that as a filter argument within CALCULATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a calculation group containing a "Last Year Dynamic" calculation item that handles the two variations of "last year".&lt;/P&gt;
&lt;P&gt;Here is the Microsoft &lt;A href="https://learn.microsoft.com/en-us/power-bi/transform-model/calculation-groups" target="_self"&gt;guide on creating calculation groups&lt;/A&gt;. You can use either Power BI Desktop or &lt;A href="https://www.sqlbi.com/tools/tabular-editor/" target="_blank" rel="noopener"&gt;Tabular Editor&lt;/A&gt; to create them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have attached an example PBIX containing one of my own models.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your "Last Year Dynamic" calculation item should have an expression similar to this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR SelectedDatesCount = COUNTROWS ( 'calendar' )
VAR IsSmallSelection = SelectedDatesCount &amp;lt;= 27
VAR Result =
    IF (
        IsSmallSelection,
        CALCULATE (
            SELECTEDMEASURE ( ),
            DATEADD ( 'calendar'[Date], -52 * 7, DAY )
        ),
        CALCULATE (
            SELECTEDMEASURE ( ),
            SAMEPERIODLASTYEAR ( 'calendar'[Date] )
        )
    )
RETURN
    Result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SELECTEDMEASURE() is a placeholder for any measure that the calculation item is applied to. Calculation items can only be applied to measures, not general expressions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you've created a calculation group and calculation item, you can apply the calculation item to measures by either:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Applying the calculation item as a filter in the report page.&lt;/LI&gt;
&lt;LI&gt;Applying the calculation item as a filter within a DAX expression.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Here is a report page showing both methods.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;"Last year" relative to 1-Feb-2021 is 3-Feb-2020&lt;/LI&gt;
&lt;LI&gt;"Last year" relative to Feb-2021 is Feb-2020&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The measure &lt;STRONG&gt;Sales Amount Last Year Dynamic&lt;/STRONG&gt; applies the "Last Year Dynamic" calculation item as follows:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;CALCULATE (
    [Sales Amount],
    'Time Intelligence'[Time Calc] = "Last Year Dynamic"
)&lt;/LI-CODE&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;There is an alternative method where you can create table functions using DETAILROWS, but I wouldn't recommend it as it's not intended for this purpose. But you can read up on it &lt;A href="https://www.sqlbi.com/articles/creating-table-functions-in-dax-using-detailrows/" target="_blank" rel="noopener"&gt;here.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 10:58:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217888#M167142</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-10-04T10:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217977#M167143</link>
      <description>&lt;P&gt;Wow, just wow &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks for the detailed answer.&lt;/P&gt;&lt;P&gt;It works. When I added the Calculation Group, I got a warning about implicit and explicit formulas. I get the difference, but how does it affect the calculation group? I mean, just doing SUM(x) (=implicit I believe) is still possible.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 11:31:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4217977#M167143</guid>
      <dc:creator>TutanRamon</dc:creator>
      <dc:date>2024-10-04T11:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4218076#M167148</link>
      <description>&lt;P&gt;How can I create a text, based on this calculated item measure?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;So, when people select Sep 1 till Sep 13, I want to show a text (in the header) which mentions "Compared to Sep 3 till Sep 15". I have this , but i aint'working.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;dateCompareText = 
VAR SelectedCalc = SELECTEDMEASURENAME()

VAR IsLastYearDynamic = SelectedCalc = "lastYearDynamic"

VAR MinCompareDate = CALCULATE(MIN('Date Logic'[dateCalc]), 'Date Logic'[dateCalc])
VAR MaxCompareDate = CALCULATE(MAX('Date Logic'[dateCalc]), 'Date Logic'[dateCalc])
VAR DateCount = CALCULATE(COUNTROWS(VALUES('Date Logic'[dateCalc])), 'Date Logic'[dateCalc])

VAR SingleDateText = 
    "Vergeleken met " &amp;amp; 
    FORMAT(MinCompareDate, "dddd d MMMM yyyy")  

VAR MultiDateText = 
    "Vergelijken met " &amp;amp; 
    FORMAT(MinCompareDate, "ddd d MMM yyyy") &amp;amp; " t/m " &amp;amp; 
    FORMAT(MaxCompareDate, "ddd d MMM yyyy") 

RETURN 
IF(
    IsLastYearDynamic, 
    IF(
        DateCount = 1, 
        SingleDateText,  
        MultiDateText    
    ),
    BLANK()
)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 04 Oct 2024 12:18:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4218076#M167148</guid>
      <dc:creator>TutanRamon</dc:creator>
      <dc:date>2024-10-04T12:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4219755#M167174</link>
      <description>&lt;P&gt;Glad it works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The "explicit measure" requirement means that calculation groups only affect measures that are defined in the model. The measure must be included directly in the visual or the measure reference used directly in the DAX expression where the calculation item is applied.&lt;/P&gt;
&lt;P&gt;Here is a good explanation:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sqlbi.com/articles/understanding-calculation-groups/#:~:text=Introducing%20calculation%20item%20application" target="_blank"&gt;https://www.sqlbi.com/articles/understanding-calculation-groups/#:~:text=Introducing%20calculation%20item%20application&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 20:33:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4219755#M167174</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-10-04T20:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4219784#M167176</link>
      <description>&lt;P&gt;Could you post a screenshot of the visual where you want to display this text?&lt;/P&gt;
&lt;P&gt;And just confirming, did you want to display this in the title of the visual?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some adjustment to the code is needed regardless. I'll have a proper look when I have time later today &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 20:51:23 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4219784#M167176</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-10-04T20:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Helper function for date comparison</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4230398#M167304</link>
      <description>&lt;P&gt;Hi again&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="545514" data-lia-user-login="TutanRamon" class="lia-mention lia-mention-user"&gt;TutanRamon&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one example of how you could set things up (updated PBIX attached):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Create a measure &lt;STRONG&gt;Date Range Text&lt;/STRONG&gt; that just returns the selected date range formatted appropriately:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Date Range Text = 
VAR DateMin =
    MIN ( 'Date'[Date] )
VAR DateMax =
    MAX ( 'Date'[Date] )
VAR DateCount = COUNTROWS ( 'Date')
VAR Result =
    IF (
        DateCount = 1,
        FORMAT ( DateMin, "dddd d MMMM yyyy" ),
        FORMAT ( DateMin, "ddd d MMM yyyy" )
            &amp;amp; " t/m " &amp;amp; FORMAT ( DateMax, "ddd d MMM yyyy" )
    )
RETURN
    Result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Create a measure &lt;STRONG&gt;Date Compare Text&lt;/STRONG&gt; that computes &lt;STRONG&gt;Date Range Text&lt;/STRONG&gt; with the "Last Year Dynamic" calculation item applied and the prefix "Vergeleken met":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Date Compare Text = 
"Vergeleken met " &amp;amp;
CALCULATE (
    [Date Range Text],
    'Time Intelligence'[Time Calc] = "Last Year Dynamic"
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Then you can use &lt;STRONG&gt;Date Compare Text&lt;/STRONG&gt; as required, such as in visual Title:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Variations on this are of course possible, but hopefully this is enough for you to go on with &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Owen&lt;/P&gt;</description>
      <pubDate>Sun, 06 Oct 2024 03:09:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Helper-function-for-date-comparison/m-p/4230398#M167304</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-10-06T03:09:40Z</dc:date>
    </item>
  </channel>
</rss>

