<?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: How can I turn this into a loop? in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3238396#M119089</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="460868" data-lia-user-login="FreemanZ" class="lia-mention lia-mention-user"&gt;FreemanZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the update. This method is my first attempt as well, but it doesn't provide the correct result. Specifically, it can only guarantee the correct identification of the second batch start date, but the third batch start date won't be correct if the dates are not structured perfectly.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Please see the example below, which is part of the actual data I'm working with. I've put the correct result in the third column and the result of your method in the second column. Now I think you could see the problem I'm running into, which is why it seems to me I have to iteratively find the first day in each batch.&lt;/P&gt;&lt;PRE&gt;6/1/2022       6/1/2022       6/1/2022&lt;BR /&gt;6/28/2022      6/1/2022       6/1/2022&lt;BR /&gt;7/30/2022      6/1/2022       6/1/2022&lt;BR /&gt;8/11/2022      8/11/2022      8/11/2022&lt;BR /&gt;9/20/2022      8/11/2022      8/11/2022&lt;BR /&gt;10/6/2022      10/6/2022      8/11/2022&lt;BR /&gt;10/12/2022     10/6/2022      10/12/2022&lt;BR /&gt;11/25/2022     10/6/2022      10/12/2022&lt;BR /&gt;11/29/2022     11/29/2022     10/12/2022&lt;BR /&gt;1/2/2023       11/29/2022     1/2/2023&lt;BR /&gt;2/7/2023       2/7/2023       1/2/2023&lt;BR /&gt;3/6/2023&amp;nbsp;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/7/2023 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/6/2023&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, in your calculation, when it comes to the date 10/6/2022, you will get&lt;/P&gt;&lt;PRE&gt;var BatchStart = 6/1/2022 + 2 * 60&lt;BR /&gt;var result = 10/6/2022&lt;/PRE&gt;&lt;P&gt;That is, your method thinks that since 10/6/2022 is more than 120 days apart from 6/1/2022, so it must be in the third or more batch. However, since 10/6/2022 is within 60 days from 8/11/2022, it should belong to the second batch.&lt;/P&gt;</description>
    <pubDate>Tue, 16 May 2023 16:27:52 GMT</pubDate>
    <dc:creator>Fluffy_Skye</dc:creator>
    <dc:date>2023-05-16T16:27:52Z</dc:date>
    <item>
      <title>How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236606#M118980</link>
      <description>&lt;P&gt;I am writing DAX for a custom column. Long story short: I have a column of dates. My goal is to separate these dates based on 60 day interval rule and identify the starting date of the interval that each date falls in. For example, suppose I have the following list of dates.&lt;/P&gt;&lt;PRE&gt;1/1/2023&lt;BR /&gt;1/2/2023&lt;BR /&gt;5/1/2023&lt;BR /&gt;5/2/2023&lt;BR /&gt;5/30/2023&lt;BR /&gt;1/1/2024&lt;BR /&gt;2/2/2024&lt;BR /&gt;7/30/2024&lt;BR /&gt;8/30/2024&lt;/PRE&gt;&lt;P&gt;The custom column I'm looking for should produce the following result (the column on the right)&lt;/P&gt;&lt;PRE&gt;1/1/2023    1/1/2023&lt;BR /&gt;1/2/2023    1/1/2023&lt;BR /&gt;5/1/2023    5/1/2023&lt;BR /&gt;5/2/2023    5/1/2023&lt;BR /&gt;5/30/2023   5/1/2023&lt;BR /&gt;1/1/2024    1/1/2024&lt;BR /&gt;2/2/2024    1/1/2024&lt;BR /&gt;7/30/2024   7/30/2024&lt;BR /&gt;8/30/2024   7/30/2024&lt;/PRE&gt;&lt;P&gt;The first date is always going to be the first date in the first 60 day interval. The second date 1/2/2023 is within 60 days of the first date, so it falls into the first 60 day interval as well. So the second row I'm also expecting 1/1/2023 in my custom column as it is the starting date of the 60 day interval that the second date falls into. Now, the third date 5/1/2023 is more than 60 days apart from the first date, and so it becomes the first day in the second 60 day interval. The forth date 5/2/2023 is withink 60 days of the third date, so it is also in the second interval and should return 5/1/2023 in the custom column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Despite my best effort, I can only think of this in an iterative fashion. I've looked up a lot of articles and youtube videos on how to simulate a while loop in DAX, but those only showcased simple examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, if I simply write out the code for each iteration, I would get my desired result, but it won't be scalable. My code is listed below. &lt;STRONG&gt;curtable&lt;/STRONG&gt; contains a column of dates and &lt;STRONG&gt;ranktable&lt;/STRONG&gt; is &lt;STRONG&gt;curtable&lt;/STRONG&gt; with an additional column ranking the dates in order.&lt;/P&gt;&lt;PRE&gt;result column =&lt;BR /&gt;var curtable = ...&lt;BR /&gt;var ranktable = ...&lt;BR /&gt;var min1date = MINX(ranktable, [from_service_date])&lt;BR /&gt;var batch1 = COUNTX(FILTER(curtable, DATEDIFF(min1date, [from_service_date], DAY) &amp;lt; 60), [from_service_date])&lt;BR /&gt;var min2date = MINX(FILTER(ranktable, [rank] &amp;gt; batch1), [from_service_date])&lt;BR /&gt;var batch2 = COUNTX(FILTER(curtable, DATEDIFF(min2date, [from_service_date], DAY) &amp;lt; 60), [from_service_date])&lt;BR /&gt;var min3date = MINX(FILTER(ranktable, [rank] &amp;gt; batch2), [from_service_date])&lt;BR /&gt;var batch3 = COUNTX(FILTER(curtable, DATEDIFF(min3date, [from_service_date], DAY) &amp;lt; 60), [from_service_date])&lt;BR /&gt;var min4date = MINX(FILTER(ranktable, [rank] &amp;gt; batch3), [from_service_date])&lt;BR /&gt;var batch4 = COUNTX(FILTER(curtable, DATEDIFF(min4date, [from_service_date], DAY) &amp;lt; 60), [from_service_date])&lt;BR /&gt;var min5date = MINX(FILTER(ranktable, [rank] &amp;gt; batch4), [from_service_date])&lt;BR /&gt;var batch5 = COUNTX(FILTER(curtable, DATEDIFF(min5date, [from_service_date], DAY) &amp;lt; 60), [from_service_date])&lt;BR /&gt;return&lt;BR /&gt;IF(DATEDIFF(min1date, [from_service_date], DAY) &amp;lt; 60, min1date,&lt;BR /&gt;IF(DATEDIFF(min2date, [from_service_date], DAY) &amp;lt; 60, min2date,&lt;BR /&gt;IF(DATEDIFF(min3date, [from_service_date], DAY) &amp;lt; 60, min3date,&lt;BR /&gt;IF(DATEDIFF(min4date, [from_service_date], DAY) &amp;lt; 60, min4date,&lt;BR /&gt;IF(DATEDIFF(min5date, [from_service_date], DAY) &amp;lt; 60, min5date&lt;BR /&gt;)))))&lt;/PRE&gt;&lt;P&gt;As you can see, &lt;STRONG&gt;minxdate&lt;/STRONG&gt;&amp;nbsp;is the first date in the &lt;STRONG&gt;x&lt;/STRONG&gt;-th interval. I am able to get the correct result this way but if the date range is much larger, I would need much more than just 5 batches like I coded above.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help!&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 05:54:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236606#M118980</guid>
      <dc:creator>Fluffy_Skye</dc:creator>
      <dc:date>2023-05-16T05:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236657#M118989</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="561052" data-lia-user-login="Fluffy_Skye" class="lia-mention lia-mention-user"&gt;Fluffy_Skye&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;not sure if i fully get you, try&lt;/P&gt;&lt;P&gt;1) create a calculated table like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;dates = CALENDAR(date(2023,1,1), date(2023,12,31))&lt;/LI-CODE&gt;&lt;P&gt;2) add a calculated column like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;rank = MOD(RANKX(dates, [Date],,ASC), 60)&lt;/LI-CODE&gt;&lt;P&gt;it worked like:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 01:56:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236657#M118989</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2023-05-16T01:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236698#M118997</link>
      <description>&lt;P&gt;Hi, I've edited my question. I hope it is more clear!&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 02:16:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236698#M118997</guid>
      <dc:creator>Fluffy_Skye</dc:creator>
      <dc:date>2023-05-16T02:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236752#M119000</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="561052" data-lia-user-login="Fluffy_Skye" class="lia-mention lia-mention-user"&gt;Fluffy_Skye&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then try to add a calculated column like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Column = MIN(dates[Date]) + INT(DIVIDE ([date]-MIN(dates[Date]), 60))*60&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;it worked like:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 02:33:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236752#M119000</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2023-05-16T02:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236966#M119011</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="460868" data-lia-user-login="FreemanZ" class="lia-mention lia-mention-user"&gt;FreemanZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the response but that's not what I'm looking for. In your example, my desired result is as follows.&lt;/P&gt;&lt;PRE&gt;1/1/2023    1/1/2023&lt;BR /&gt;1/2/2023    1/1/2023&lt;BR /&gt;5/1/2023    5/1/2023&lt;BR /&gt;5/30/2023   5/1/2023&lt;BR /&gt;7/1/2023    7/1/2023&lt;BR /&gt;7/30/2023   7/1/2023&lt;BR /&gt;9/1/2023    9/1/2023&lt;BR /&gt;9/30/2023   9/1/2023&lt;/PRE&gt;&lt;P&gt;This is because 7/1/2023 is more than 60 days apart from 5/1/2023, so it becomes the first day in the third batch. Similarly, 9/1/2023 is more than 60 days apart from 7/1/2023, so it becomes the first day in the forth batch.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In particular, the result date should always come from the original date column, as it represents the first day in the batch.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 05:43:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3236966#M119011</guid>
      <dc:creator>Fluffy_Skye</dc:creator>
      <dc:date>2023-05-16T05:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3237241#M119027</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="561052" data-lia-user-login="Fluffy_Skye" class="lia-mention lia-mention-user"&gt;Fluffy_Skye&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The logic is the same.&lt;/P&gt;&lt;P&gt;then try to add a calculated column:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Column = 
VAR BatchStart= MIN(ranktable[Date1]) + INT(DIVIDE (ranktable[date1]-MIN(ranktable[Date1]), 60))*60
VAR result =
MINX(
    FILTER(
        ranktable, 
        ranktable[date1] &amp;gt;= BatchStart
    ),
    ranktable[date1]
)
RETURN result&lt;/LI-CODE&gt;&lt;P&gt;it worked like:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 08:14:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3237241#M119027</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2023-05-16T08:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can I turn this into a loop?</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3238396#M119089</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="460868" data-lia-user-login="FreemanZ" class="lia-mention lia-mention-user"&gt;FreemanZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the update. This method is my first attempt as well, but it doesn't provide the correct result. Specifically, it can only guarantee the correct identification of the second batch start date, but the third batch start date won't be correct if the dates are not structured perfectly.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Please see the example below, which is part of the actual data I'm working with. I've put the correct result in the third column and the result of your method in the second column. Now I think you could see the problem I'm running into, which is why it seems to me I have to iteratively find the first day in each batch.&lt;/P&gt;&lt;PRE&gt;6/1/2022       6/1/2022       6/1/2022&lt;BR /&gt;6/28/2022      6/1/2022       6/1/2022&lt;BR /&gt;7/30/2022      6/1/2022       6/1/2022&lt;BR /&gt;8/11/2022      8/11/2022      8/11/2022&lt;BR /&gt;9/20/2022      8/11/2022      8/11/2022&lt;BR /&gt;10/6/2022      10/6/2022      8/11/2022&lt;BR /&gt;10/12/2022     10/6/2022      10/12/2022&lt;BR /&gt;11/25/2022     10/6/2022      10/12/2022&lt;BR /&gt;11/29/2022     11/29/2022     10/12/2022&lt;BR /&gt;1/2/2023       11/29/2022     1/2/2023&lt;BR /&gt;2/7/2023       2/7/2023       1/2/2023&lt;BR /&gt;3/6/2023&amp;nbsp;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/7/2023 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/6/2023&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, in your calculation, when it comes to the date 10/6/2022, you will get&lt;/P&gt;&lt;PRE&gt;var BatchStart = 6/1/2022 + 2 * 60&lt;BR /&gt;var result = 10/6/2022&lt;/PRE&gt;&lt;P&gt;That is, your method thinks that since 10/6/2022 is more than 120 days apart from 6/1/2022, so it must be in the third or more batch. However, since 10/6/2022 is within 60 days from 8/11/2022, it should belong to the second batch.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 16:27:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-I-turn-this-into-a-loop/m-p/3238396#M119089</guid>
      <dc:creator>Fluffy_Skye</dc:creator>
      <dc:date>2023-05-16T16:27:52Z</dc:date>
    </item>
  </channel>
</rss>

