<?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: SamePeriodLastYear not working as expected in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3590447#M138609</link>
    <description>&lt;P&gt;It seems like you're encountering an issue with the SAMEPERIODLASTYEAR function in your DAX measure. The behavior you're describing suggests that the time intelligence functions might be affected by the filter context in your report.&lt;/P&gt;&lt;P&gt;When you use DATESMTD(Dimdate[Date]) in your original measure, it calculates the month-to-date period based on the filter context of the report. However, when you wrap it with SAMEPERIODLASTYEAR, it might not be behaving as expected due to the specific context in which it's being evaluated.&lt;/P&gt;&lt;P&gt;To ensure that SAMEPERIODLASTYEAR works correctly, you should create a separate table or column for your date calculations, and then use that table or column in your measure. This helps to control the filter context explicitly.&lt;/P&gt;&lt;P&gt;Here's an example of how you can modify your DAX:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;Create a new calculated column in your DimDate table to store the month-to-date value:&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;DAX:&lt;/P&gt;&lt;P&gt;MTDDate = DATESMTD(Dimdate[Date])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Now, modify your measure to use this new column:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DAX:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date ranges = CALCULATE(MIN(Dimdate[Date]) &amp;amp; "-" &amp;amp; MAX(Dimdate[Date]), Dimdate[MTDDate])&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Finally, use SAMEPERIODLASTYEAR with the new column:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DAX:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date ranges LY = CALCULATE(MIN(Dimdate[Date]) &amp;amp; "-" &amp;amp; MAX(Dimdate[Date]), SAMEPERIODLASTYEAR(Dimdate[MTDDate]))&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;By doing this, you ensure that the context is well-defined, and the time intelligence functions operate on the correct set of dates. This approach should help resolve the issue you're facing with SAMEPERIODLASTYEAR.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;If this post&amp;nbsp;helps, then please consider&amp;nbsp;Accepting it as the solution&amp;nbsp;to help the other members find it more quickly.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In case there is still a problem, please feel free and explain your issue in detail,&amp;nbsp;It will be my pleasure to assist you in any way I can.&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Dec 2023 05:39:35 GMT</pubDate>
    <dc:creator>123abc</dc:creator>
    <dc:date>2023-12-19T05:39:35Z</dc:date>
    <item>
      <title>SamePeriodLastYear not working as expected</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3589730#M138571</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I noticed something strange with my data model today and I am hoping someone can please explain the reason behind this behaviour and how to fix it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a simple data model with just DimDate as a table with records filtered up to current date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my data model, I have a measure&amp;nbsp;&lt;/P&gt;&lt;P&gt;date ranges = Calculate ( Min(date) &amp;amp; "-" &amp;amp; max(date), datesmtd(Dimdate[Date]))&lt;/P&gt;&lt;P&gt;the above measure returns the expected output I.e&lt;/P&gt;&lt;P&gt;01/12/2023 - 18/12/2023&lt;/P&gt;&lt;P&gt;when I change the above dax to&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Calculate ( Min(date) &amp;amp; "-" &amp;amp; max(date), sameperiodlastyear(datesmtd(Dimdate[Date])))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I get the result 01/12/2022 - 31/12/2022 which I don't understand.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if I use a date slicer and select 17/12/2023 as my filter the measure then works as expected 01/12/2022 - 17/12/2022&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;could you please explain why and also how to fix this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 20:10:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3589730#M138571</guid>
      <dc:creator>2366</dc:creator>
      <dc:date>2023-12-18T20:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: SamePeriodLastYear not working as expected</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3590447#M138609</link>
      <description>&lt;P&gt;It seems like you're encountering an issue with the SAMEPERIODLASTYEAR function in your DAX measure. The behavior you're describing suggests that the time intelligence functions might be affected by the filter context in your report.&lt;/P&gt;&lt;P&gt;When you use DATESMTD(Dimdate[Date]) in your original measure, it calculates the month-to-date period based on the filter context of the report. However, when you wrap it with SAMEPERIODLASTYEAR, it might not be behaving as expected due to the specific context in which it's being evaluated.&lt;/P&gt;&lt;P&gt;To ensure that SAMEPERIODLASTYEAR works correctly, you should create a separate table or column for your date calculations, and then use that table or column in your measure. This helps to control the filter context explicitly.&lt;/P&gt;&lt;P&gt;Here's an example of how you can modify your DAX:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;Create a new calculated column in your DimDate table to store the month-to-date value:&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;DAX:&lt;/P&gt;&lt;P&gt;MTDDate = DATESMTD(Dimdate[Date])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Now, modify your measure to use this new column:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DAX:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date ranges = CALCULATE(MIN(Dimdate[Date]) &amp;amp; "-" &amp;amp; MAX(Dimdate[Date]), Dimdate[MTDDate])&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Finally, use SAMEPERIODLASTYEAR with the new column:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DAX:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date ranges LY = CALCULATE(MIN(Dimdate[Date]) &amp;amp; "-" &amp;amp; MAX(Dimdate[Date]), SAMEPERIODLASTYEAR(Dimdate[MTDDate]))&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;By doing this, you ensure that the context is well-defined, and the time intelligence functions operate on the correct set of dates. This approach should help resolve the issue you're facing with SAMEPERIODLASTYEAR.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;If this post&amp;nbsp;helps, then please consider&amp;nbsp;Accepting it as the solution&amp;nbsp;to help the other members find it more quickly.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In case there is still a problem, please feel free and explain your issue in detail,&amp;nbsp;It will be my pleasure to assist you in any way I can.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 05:39:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3590447#M138609</guid>
      <dc:creator>123abc</dc:creator>
      <dc:date>2023-12-19T05:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: SamePeriodLastYear not working as expected</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3593132#M138766</link>
      <description>&lt;P&gt;Hi, &lt;STRONG&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="210416" data-lia-user-login="2366" class="lia-mention lia-mention-user"&gt;2366&lt;/a&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I ask if this is the expected output you are looking for? Based on your description, the SamePeriodLastYear function is not returning the expected result in the data model. Here is what might be happening:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.When you use &lt;SPAN&gt;‘&lt;/SPAN&gt;DATESMTD(DimDate[Date])&lt;SPAN&gt;’&lt;/SPAN&gt;, it returns the dates from the start of the month to the current date in the month.&lt;/P&gt;
&lt;P&gt;2.When you wrap &lt;SPAN&gt;‘&lt;/SPAN&gt;SamePeriodLastYear&lt;SPAN&gt;’&lt;/SPAN&gt; around &lt;SPAN&gt;‘&lt;/SPAN&gt;DATESMTD&lt;SPAN&gt;’&lt;/SPAN&gt;, it attempts to find the same period last year. However, if the current month is not complete, it may return the full month from the previous year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix this issue, you can create a measure that checks if the current period is complete and then apply &lt;SPAN&gt;‘&lt;/SPAN&gt;SamePeriodLastYear&lt;SPAN&gt;’&lt;/SPAN&gt; accordingly. Here is how you can modify your measure:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Measures:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Date Ranges1 =
VAR CurrentPeriod =
    DATESMTD ( DimDate[Date] )
VAR LastYearPeriod =
    SAMEPERIODLASTYEAR ( CurrentPeriod )
VAR MinDateCurrentPeriod =
    MINX ( CurrentPeriod, DimDate[Date] )
VAR MaxDateCurrentPeriod =
    MAXX ( CurrentPeriod, DimDate[Date] )
VAR MinDateLastYear =
    MINX ( LastYearPeriod, DimDate[Date] )
VAR MaxDateLastYear =
    IF (
        MAX ( DimDate[Date] ) = TODAY (),
        MaxDateCurrentPeriod - 1,
        MAXX ( LastYearPeriod, DimDate[Date] )
    )
RETURN
    CALCULATE (
        MIN ( DimDate[Date] ) &amp;amp; "-"
            &amp;amp; MAX ( DimDate[Date] ),
        LastYearPeriod
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this does not work, could you please share some sample data without sensitive information and expected output.&lt;BR /&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216" target="_blank"&gt;How to provide sample data in the Power BI Forum - Microsoft Fabric Community&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Best Regards,&lt;BR /&gt;Yang&lt;BR /&gt;Community Support Team&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is any post&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;helps&lt;/EM&gt;&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept it as the solution&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;to help the other members find it more quickly.&lt;BR /&gt;If I misunderstand your needs or you still have problems on it, please feel free to let us know.&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Thanks a lot!&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490" target="_blank"&gt;How to get your questions answered quickly&lt;/A&gt;&amp;nbsp;--&amp;nbsp;&lt;A href="https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216" target="_blank"&gt;&amp;nbsp;How to provide sample data in the Power BI Forum&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2023 08:13:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/SamePeriodLastYear-not-working-as-expected/m-p/3593132#M138766</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-12-20T08:13:13Z</dc:date>
    </item>
  </channel>
</rss>

