<?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: DAX measure evaluated globally and based on date in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4036786#M160007</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can use a combination of DAX functions to create a measure that checks the store's open and close dates against the entire date range selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like_for_like =&lt;BR /&gt;VAR MinDateSelected = MINX(ALLSELECTED(DIM_DATE), DIM_DATE[date])&lt;BR /&gt;VAR MaxDateSelected = MAXX(ALLSELECTED(DIM_DATE), DIM_DATE[date])&lt;BR /&gt;RETURN&lt;BR /&gt;IF(&lt;BR /&gt;COUNTROWS(&lt;BR /&gt;FILTER(&lt;BR /&gt;DIM_STORE,&lt;BR /&gt;DIM_STORE[opening_date] &amp;lt;= MinDateSelected &amp;amp;&amp;amp;&lt;BR /&gt;(ISBLANK(DIM_STORE[closing_date]) || DIM_STORE[closing_date] &amp;gt;= MaxDateSelected)&lt;BR /&gt;)&lt;BR /&gt;) = COUNTROWS(DIM_STORE),&lt;BR /&gt;1,&lt;BR /&gt;0&lt;BR /&gt;)&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jul 2024 09:44:28 GMT</pubDate>
    <dc:creator>Alican_C</dc:creator>
    <dc:date>2024-07-11T09:44:28Z</dc:date>
    <item>
      <title>DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4036529#M159980</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;&lt;P&gt;I am trying to solve the following problem:&lt;/P&gt;&lt;P&gt;I need a measure ("like_for_like") that shows whether a store (2; see screenshot) was open during the whole time selected by a date slicer (1). It should return "1" if the store was open the whole time and "0" otherwise.&lt;/P&gt;&lt;P&gt;In my example all of 2024 is selected. The store "store_1" was open the whole year, so it should get a "1", while the store "store_2" closed at the end of January 2024, so it should get a "0".&lt;/P&gt;&lt;P&gt;My measure works fine on level store (3). However, if I add month to the table, the measure is evaluated by month (4), and "store_2" gets a "1" in January since it was still open. This is not what I need.&lt;/P&gt;&lt;P&gt;I would like to have a measure that is&lt;/P&gt;&lt;P&gt;(a) only evaluated globally, but that is&lt;/P&gt;&lt;P&gt;(b) still dependent on the global slicer (1).&lt;/P&gt;&lt;P&gt;So I would like the measure to return a "0" for "store_2" for all the months since the store was not open for all of 2024.&lt;/P&gt;&lt;P&gt;I have tried many different formulae, none of which were successful. For some reason I only achieved either (a) or (b). I tried different approaches including functions such as ALL, ALLEXCEPT, REMOVEFILTERS, ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have an idea for how to solve this? Thanks a lot for any thoughts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example of an approach (that did not work out): temporary table with all the stores that are open during the selected period.&lt;/P&gt;&lt;P&gt;Problem: works globally, but is evaluated per month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;EM&gt;like_for_like =&lt;/EM&gt;&lt;/EM&gt;&lt;DIV&gt;&lt;EM&gt;&lt;EM&gt;VAR stores_open = SELECTCOLUMNS(&lt;/EM&gt;&lt;/EM&gt;&lt;DIV&gt;&lt;EM&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; FILTER('DIM_STORE', 'DIM_STORE'[opening_date] &amp;lt;= MIN(DIM_DATE[date]) &amp;amp;&amp;amp; 'DIM_STORE'[closing_date] &amp;gt;= MAX(DIM_DATE[date])),&amp;nbsp;"store_id_open", 'DIM_STORE'[store_id])&lt;BR /&gt;&lt;BR /&gt;&lt;/EM&gt;&lt;/EM&gt;&lt;DIV&gt;&lt;EM&gt;&lt;EM&gt;RETURN &amp;nbsp; &amp;nbsp;IF(CALCULATE(COUNTROWS(DIM_STORE), DIM_STORE[store_id] IN stores_open), 1, 0)&lt;/EM&gt;&lt;/EM&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt; &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 11 Jul 2024 07:51:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4036529#M159980</guid>
      <dc:creator>alexschindler</dc:creator>
      <dc:date>2024-07-11T07:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4036786#M160007</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can use a combination of DAX functions to create a measure that checks the store's open and close dates against the entire date range selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like_for_like =&lt;BR /&gt;VAR MinDateSelected = MINX(ALLSELECTED(DIM_DATE), DIM_DATE[date])&lt;BR /&gt;VAR MaxDateSelected = MAXX(ALLSELECTED(DIM_DATE), DIM_DATE[date])&lt;BR /&gt;RETURN&lt;BR /&gt;IF(&lt;BR /&gt;COUNTROWS(&lt;BR /&gt;FILTER(&lt;BR /&gt;DIM_STORE,&lt;BR /&gt;DIM_STORE[opening_date] &amp;lt;= MinDateSelected &amp;amp;&amp;amp;&lt;BR /&gt;(ISBLANK(DIM_STORE[closing_date]) || DIM_STORE[closing_date] &amp;gt;= MaxDateSelected)&lt;BR /&gt;)&lt;BR /&gt;) = COUNTROWS(DIM_STORE),&lt;BR /&gt;1,&lt;BR /&gt;0&lt;BR /&gt;)&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2024 09:44:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4036786#M160007</guid>
      <dc:creator>Alican_C</dc:creator>
      <dc:date>2024-07-11T09:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4038745#M160114</link>
      <description>&lt;P&gt;Hi Alican_C&lt;/P&gt;&lt;P&gt;Woah, that's amazing, works like a charm!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much - you totally made my day! Really appreciate it!&lt;/P&gt;&lt;P&gt;Alex&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2024 08:33:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4038745#M160114</guid>
      <dc:creator>alexschindler</dc:creator>
      <dc:date>2024-07-12T08:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4038980#M160129</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="769354" data-lia-user-login="Alican_C" class="lia-mention lia-mention-user"&gt;Alican_C&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for bothering you one more time, but actually I realized that the solution does not work yet for SAMEPERIODLASTYEAR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my use case users would select a time period in the current year, say, Jan 1, 2024 - March 31, 2024. They would then be shown the net sales for the 2024 period, but also for the corresponding 2023 period (Jan 1, 2023 - March 31, 2023).&lt;/P&gt;&lt;P&gt;So the like_for_like measure should be 1 if the store was open Jan 1, 2023 - March 31, 2024.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I made some small modifications to extend the period that is checked to the previous year. I have 2 parameters "_last_year_manual" and "_current_year_manual".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;VAR MinDateSelectedPrecalculation = MINX(ALLSELECTED(DIM_DATE), DIM_DATE[date])

VAR MinDateSelected = IF(YEAR(MinDateSelectedPrecalculation) = [_current_year_manual], MINX(ALLSELECTED(DIM_DATE), DATEADD( DIM_DATE[date], -1, YEAR)), MinDateSelectedPrecalculation)


VAR MaxDateSelectedPrecalculation = MAXX(ALLSELECTED(DIM_DATE), DIM_DATE[date])

VAR MaxDateSelected = IF(YEAR(MaxDateSelectedPrecalculation) = [_last_year_manual], MAXX(ALLSELECTED(DIM_DATE), DATEADD( DIM_DATE[date], 1, YEAR)), MaxDateSelectedPrecalculation)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if I now run a SAMEPERIODLASTYEAR on my measures, it evaluates the MinDateSelected and MaxDateSelected on month level again rather than globally.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See screenshot: For the (A) measures it works great. For the (B) measures with SAMEPERIODLASTYEAR it evaluates by month again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have an idea how to fix this such that it also works with SAMEPERIODLASTYEAR?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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;</description>
      <pubDate>Fri, 12 Jul 2024 08:43:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4038980#M160129</guid>
      <dc:creator>alexschindler</dc:creator>
      <dc:date>2024-07-12T08:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4042031#M160282</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="769354" data-lia-user-login="Alican_C" class="lia-mention lia-mention-user"&gt;Alican_C&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;I have been doing more research and think I found out that the problem does not lie with the SAMEPERIODLASTYEAR, but rather with the CALCULATE. I think it is a problem of context.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I am now using the like_for_like measure for the net sales:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;net_sales = SWITCH(
    TRUE(),
        [like_for_like_selected] = "All in", SUM('FACT_SALES'[net_sales]),
        [like_for_like_selected] = "L4L", CALCULATE(SUM('FACT_SALES'[net_sales]), FILTER('DIM_STORE', DIM_STORE[_like_for_like] = 1) ),
        [like_for_like_selected] = "Non-L4L", CALCULATE(SUM('FACT_SALES'[net_sales]), FILTER('DIM_STORE', DIM_STORE[_like_for_like] = 0) )
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Those work great, but then I have a lot of measures that go something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;net_sales_last_year = CALCULATE([net_sales], SAMEPERIODLASTYEAR('DIM_DATE'[date] ) )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the CALCULATE messes up the like_for_like measure since it adds date context to the table again: then the like_for_like measure is again evaluated by month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you think this is correct? And if yes, how can it be avoided?&lt;/P&gt;&lt;P&gt;Many thanks for your time!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Alex&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2024 08:28:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4042031#M160282</guid>
      <dc:creator>alexschindler</dc:creator>
      <dc:date>2024-07-15T08:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure evaluated globally and based on date</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4043550#M160371</link>
      <description>&lt;P&gt;After more reading and researching I figured out that I can get the like_for_like measure to work if I add another ALLSELECTED:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;net_sales_last_year = CALCULATE([net_sales], SAMEPERIODLASTYEAR(ALLSELECTED('DIM_DATE'[date] ) ))&lt;/LI-CODE&gt;&lt;P&gt;This fixes the like_for_like problem. However, the problem is then that it no longer shows the net sales by month, but the same value for each month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I would need to make sure the ALLSELECTED is only fed to the variables of the like_for_like measures, but not to the part with the net sales.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 05:56:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-evaluated-globally-and-based-on-date/m-p/4043550#M160371</guid>
      <dc:creator>alexschindler</dc:creator>
      <dc:date>2024-07-16T05:56:36Z</dc:date>
    </item>
  </channel>
</rss>

