<?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 to get the resulting EndDate after adding N week days to a given StartDate in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3337734#M125196</link>
    <description>&lt;P&gt;Hello again folks,&amp;nbsp;&lt;BR /&gt;Well, after a second look, it seems that Alberto Ferrari had the answer to my question in this video :&lt;BR /&gt;&lt;A href="https://www.youtube.com/watch?v=2HkBbqxBzF0" target="_self"&gt;https://www.youtube.com/watch?v=2HkBbqxBzF0&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here is his solution adapted to my contexte :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Test Found EndDate 2 = 
// Take the Cycle StartDate, add N working days and return the resulting EndDate

// Get the number of selected ElapsedDays
VAR ElapsedDays = SELECTEDVALUE(Slicer_Days[ElapsedDays])

// Define the Cycle StartDate as StartDate
VAR StartDate = SELECTEDVALUE(SurveyCycle_Dim[Cycle StartDate])

// Get the list of all working days after the StartDate (this var is a subtable of Calendar table)
VAR NextWorkingDates = FILTER(
    ALL(Calendar_Dim),
    Calendar_Dim[Date] &amp;gt; StartDate &amp;amp;&amp;amp; 
    Calendar_Dim[Is WeekDay] = 1
    )

// Get the next N working days
VAR Next_N_WorkingDays = 
    TOPN(ElapsedDays, NextWorkingDates, Calendar_Dim[Date], ASC)

// Get the last of the topN days of the list above
VAR Last_WorkingDays = MAXX(Next_N_WorkingDays, Calendar_Dim[Date])

// I'm substracting 1 day to the final result because it's require in my contexte.
RETURN
    Last_WorkingDays - 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Thanks Alberto,&amp;nbsp;&lt;BR /&gt;I'll still let this thread there as it may still help other people.&lt;BR /&gt;Cheers,&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2023 05:48:39 GMT</pubDate>
    <dc:creator>Nyansapo</dc:creator>
    <dc:date>2023-07-19T05:48:39Z</dc:date>
    <item>
      <title>DAX measure to get the resulting EndDate after adding N week days to a given StartDate</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3337697#M125194</link>
      <description>&lt;P&gt;Hello Power BI community,&amp;nbsp;&lt;BR /&gt;I'm writting to you for the first time because for the first time, I haven't been able to find my answer elswhere on the Internet. I found some similar threads for calculated columns (among which a video from Alberto Ferrari) and for Power Query, but nothing that I was able to use for this problem.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Here is what I try to code in DAX.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a measure that will take as input a StartDate (in the contextual row) as well as a given integer number (called N) (normally between 0 and 30) and will return the EndDate resulting after adding N week days (or working days) to the StartDate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The value "N" comes from a slicer that contain value between 0 and 30, and can be selected by the user to decide how many working days he/she wants to add to the StartDate of each row contexte. Then, I expect this measure to return the proper EndDate for each of these row contexts.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code of what I tried, but for some reasons I'm not sure to understand, this measure to not work in every contexte. I would say, that it returns the proper value about 80% of the time, but as the selected value "N" becomes larger, the measure tend to return the wrong EndDate.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Test Found EndDate = 
// Find the EndDate resulting of the Cycle StartDate + ElapsedDays (ElapsedDays being only weekdays)

//1- Value selected in the "ElapsedDays" slicer (ElapsedDays is an integer between 0 and 30)
VAR ElapsedDays = SELECTEDVALUE(Slicer_Days[ElapsedDays])

//2- Cycle StartDate of the cycle in the row context
VAR Cycle_StartDate = SELECTEDVALUE(SurveyCycle_Dim[Cycle StartDate])

//3- Calendar EndDate derived by simply adding ElapsedDays to the StartDate
VAR Calendar_EndDate = Cycle_StartDate + ElapsedDays

//4- Return number of week days between "Cycle_StartDate" and "Calendar_EndDate"
VAR Num_Week_days = NETWORKDAYS(Cycle_StartDate, Calendar_EndDate, 1)

//5- Derive the number of Weekend days between "Cycle_StartDate" and "Calendar_EndDate"
// Number of weekend days = Total number of Days - Weekdays
// Total number of days = ElapsedDays + 1     (adding 1 to account for the startdate)
VAR Num_Weekend_Days = (ElapsedDays+1) - Num_Week_days

//6- The first temporary EndDate of the calculation
VAR First_EndDate = Cycle_StartDate + ElapsedDays + Num_Weekend_Days

//7- Add number of weekend days skipped while adding weekend at the preceding step
// Number of weekend days = "Total Number of days" - "Number of weekdays"
VAR WeekendSkipped = (First_EndDate - Calendar_EndDate) - NETWORKDAYS(Calendar_EndDate, First_EndDate, 1)

//8- Second temporary EndDate
VAR Second_EndDate = First_EndDate + WeekendSkipped

//9- This last step is to move to the closest next weekdays if the result fall on a Saturday or a Sunday.
// Check if "Second_EndDate" is a Saturday (then add 2 to it) or a Sunaday (then add 1 to it)
VAR adjust = 
IF(WEEKDAY(Second_EndDate,2) = 6, 2,
    IF(WEEKDAY(Second_EndDate,2) = 7, 1, 0)
)

//10- Return the date corresponding to the Cycle StartDate + nomber of working days equal to "ElapsedDays"
RETURN
Second_EndDate + adjust&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;I'm pretty sure my error is related to the fact that I don't account properly for the weekend days I skipp and add back at the step 6 and 7, but I can't specifically find the specific error I'm making.&amp;nbsp;&lt;BR /&gt;So far, I haven't used any Calendar table for this measure, simply because I couldn't figure how it would be usefull, but I'm open to any suggestions.&amp;nbsp;&lt;BR /&gt;Hope to read you soon!&lt;BR /&gt;Thanks a lot folks,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 04:38:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3337697#M125194</guid>
      <dc:creator>Nyansapo</dc:creator>
      <dc:date>2023-07-19T04:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure to get the resulting EndDate after adding N week days to a given StartDate</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3337734#M125196</link>
      <description>&lt;P&gt;Hello again folks,&amp;nbsp;&lt;BR /&gt;Well, after a second look, it seems that Alberto Ferrari had the answer to my question in this video :&lt;BR /&gt;&lt;A href="https://www.youtube.com/watch?v=2HkBbqxBzF0" target="_self"&gt;https://www.youtube.com/watch?v=2HkBbqxBzF0&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here is his solution adapted to my contexte :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Test Found EndDate 2 = 
// Take the Cycle StartDate, add N working days and return the resulting EndDate

// Get the number of selected ElapsedDays
VAR ElapsedDays = SELECTEDVALUE(Slicer_Days[ElapsedDays])

// Define the Cycle StartDate as StartDate
VAR StartDate = SELECTEDVALUE(SurveyCycle_Dim[Cycle StartDate])

// Get the list of all working days after the StartDate (this var is a subtable of Calendar table)
VAR NextWorkingDates = FILTER(
    ALL(Calendar_Dim),
    Calendar_Dim[Date] &amp;gt; StartDate &amp;amp;&amp;amp; 
    Calendar_Dim[Is WeekDay] = 1
    )

// Get the next N working days
VAR Next_N_WorkingDays = 
    TOPN(ElapsedDays, NextWorkingDates, Calendar_Dim[Date], ASC)

// Get the last of the topN days of the list above
VAR Last_WorkingDays = MAXX(Next_N_WorkingDays, Calendar_Dim[Date])

// I'm substracting 1 day to the final result because it's require in my contexte.
RETURN
    Last_WorkingDays - 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Thanks Alberto,&amp;nbsp;&lt;BR /&gt;I'll still let this thread there as it may still help other people.&lt;BR /&gt;Cheers,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 05:48:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3337734#M125196</guid>
      <dc:creator>Nyansapo</dc:creator>
      <dc:date>2023-07-19T05:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure to get the resulting EndDate after adding N week days to a given StartDate</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3339013#M125292</link>
      <description>&lt;P&gt;Ok, I was so happy and I thought that this solution was perfect, but it looks like for some row contexte, it's not returning the right date...&amp;nbsp;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;I'm starting losing faith...&lt;/P&gt;&lt;P&gt;For example, when the measure gets the Start Date 2022-10-04, and the value N = 19, then it returns the date 2022-10-30, while it should return the date 2022-10-28. Indeed, when including the first and the last date, there are 19 working days between&amp;nbsp;2022-10-04 and&amp;nbsp;2022-10-28.&amp;nbsp;&lt;BR /&gt;I have the same problem with StartDate = 2021-05-04&amp;nbsp; and N=19. The measure returns 2021-05-30, while it should return 2021-05-28.&amp;nbsp;&lt;BR /&gt;Strangely, for these two situations, the month/year change but the days are the same...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I double checked my calendar table, and it only have weekend days for Saturday and Sunday, I haven't included any holidays.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be very pleased to know if anyone else get the same problem implementing the same code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope to read you.&amp;nbsp;&lt;BR /&gt;Thanks,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 15:57:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-get-the-resulting-EndDate-after-adding-N-week/m-p/3339013#M125292</guid>
      <dc:creator>Nyansapo</dc:creator>
      <dc:date>2023-07-19T15:57:22Z</dc:date>
    </item>
  </channel>
</rss>

