<?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: WorkDays Function in Power Query M in Power Query</title>
    <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1010466#M34412</link>
    <description>&lt;P&gt;Thank you for solving it, I tested it and noticed it doesn't work with negative values,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so I went away and updated with the following,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;its a bit messy but it works&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully someone who needs it can quickly use it.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;//fnWorkDays
let func = (StartDate as date, WorkDays as number) =&amp;gt;
let

WorkDays2 = if WorkDays&amp;lt;0 then 

    (WorkDays*2)-7 else (WorkDays*2)+7,


StartDate2 = 
if WorkDays&amp;lt;0  then 
                                if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else
                                if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate
                            else 
                                if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,-1) else
                                if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,-2) else StartDate,


ListofDates = if WorkDays&amp;lt;0 then 
                                List.Dates(Date.AddDays(StartDate2,WorkDays2), -1*WorkDays2+1,#duration(1,0,0,0)) 
                            else
                                List.Dates(StartDate2, WorkDays2,#duration(1,0,0,0)),

DeleteWeekends = List.Select(ListofDates, each Date.DayOfWeek(_) &amp;lt; 5 ),

StartDateRange = if WorkDays&amp;lt;0 then List.PositionOf(DeleteWeekends,StartDate2) else 0,

WorkDateRange = if WorkDays&amp;lt;0 then StartDateRange+WorkDays else WorkDays,

WorkDate = List.Range(DeleteWeekends,WorkDateRange,1),

Result = if WorkDays =0 then StartDate else WorkDate{0}

in
Result
in
func&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 06 Apr 2020 16:02:15 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-04-06T16:02:15Z</dc:date>
    <item>
      <title>WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007028#M34312</link>
      <description>&lt;P&gt;I am trying the create a function that replicates the Workdays function typically found in Excel.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have gotten so far;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//fnWorkDays&lt;BR /&gt;let func = (StartDate as date, WorkDays as number) =&amp;gt;&lt;BR /&gt;let&lt;/P&gt;&lt;P&gt;WorkDays2 = (WorkDays*2)+7,&lt;/P&gt;&lt;P&gt;StartDate = if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else&lt;BR /&gt;if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate,&lt;/P&gt;&lt;P&gt;ListOfDates = List.Dates(StartDate, WorkDays2,#duration(1,0,0,0)),&lt;/P&gt;&lt;P&gt;DeleteWeekends = List.Select(ListOfDates, each Date.DayOfWeek(_,1) &amp;lt; 5 ),&lt;/P&gt;&lt;P&gt;WorkDate = List.Range(DeleteWeekends,WorkDays,1),&lt;BR /&gt;&lt;BR /&gt;Result = WorkDate{0}&lt;BR /&gt;&lt;BR /&gt;in&lt;/P&gt;&lt;P&gt;Result&lt;/P&gt;&lt;P&gt;in&lt;/P&gt;&lt;P&gt;func&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I invoke the function it works but when I apply it to Columns StartDate and WorkDays in a table it isn't working.&lt;/P&gt;&lt;P&gt;It then throws an extremely long error message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anybody know the solution?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 13:48:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007028#M34312</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-03T13:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007207#M34313</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp;might be able to help with the Power Query.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want a solution, I have a DAX version:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/td-p/367362" target="_blank"&gt;https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/td-p/367362&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 15:03:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007207#M34313</guid>
      <dc:creator>Greg_Deckler</dc:creator>
      <dc:date>2020-04-03T15:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007249#M34316</link>
      <description>&lt;P&gt;If you can make your start date a Monday every time correctly as you're trying to do, then rather than using lists, abuse modular arithmetic&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- derive the number of days between your start and end dates regardless of whether it's a weekend or not&lt;/P&gt;&lt;P&gt;- do (Number.RoundDown (thatnumberofdays/7))*5 to get a count of five for every completed week&lt;/P&gt;&lt;P&gt;- do Number.Mod(thatnumberofdays,7) to get the number of days left, using an if statement to change a 6 to 5&lt;/P&gt;&lt;P&gt;- add the results of step 2&amp;amp;3 together&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 15:17:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007249#M34316</guid>
      <dc:creator>jthomson</dc:creator>
      <dc:date>2020-04-03T15:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007641#M34330</link>
      <description>&lt;P&gt;Hi Anonymous&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;that looks a bit buggy, indeed.&lt;/P&gt;
&lt;P&gt;However, you formula works if you avoid using the same name for a step than for a variable like so:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;let func = (StartDate as date, WorkDays as number) =&amp;gt;
let
WorkDays2 = (WorkDays*2)+7,
startDate = if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else
if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate,
ListOfDates = List.Dates(startDate, WorkDays2,#duration(1,0,0,0)),
DeleteWeekends = List.Select(ListOfDates, each Date.DayOfWeek(_,1) &amp;lt; 5 ),
WorkDate = List.Range(DeleteWeekends,WorkDays,1),

Result = WorkDate{0}

in
Result
in
func&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 21:46:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007641#M34330</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2020-04-03T21:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1010466#M34412</link>
      <description>&lt;P&gt;Thank you for solving it, I tested it and noticed it doesn't work with negative values,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so I went away and updated with the following,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;its a bit messy but it works&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully someone who needs it can quickly use it.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;//fnWorkDays
let func = (StartDate as date, WorkDays as number) =&amp;gt;
let

WorkDays2 = if WorkDays&amp;lt;0 then 

    (WorkDays*2)-7 else (WorkDays*2)+7,


StartDate2 = 
if WorkDays&amp;lt;0  then 
                                if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else
                                if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate
                            else 
                                if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,-1) else
                                if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,-2) else StartDate,


ListofDates = if WorkDays&amp;lt;0 then 
                                List.Dates(Date.AddDays(StartDate2,WorkDays2), -1*WorkDays2+1,#duration(1,0,0,0)) 
                            else
                                List.Dates(StartDate2, WorkDays2,#duration(1,0,0,0)),

DeleteWeekends = List.Select(ListofDates, each Date.DayOfWeek(_) &amp;lt; 5 ),

StartDateRange = if WorkDays&amp;lt;0 then List.PositionOf(DeleteWeekends,StartDate2) else 0,

WorkDateRange = if WorkDays&amp;lt;0 then StartDateRange+WorkDays else WorkDays,

WorkDate = List.Range(DeleteWeekends,WorkDateRange,1),

Result = if WorkDays =0 then StartDate else WorkDate{0}

in
Result
in
func&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 06 Apr 2020 16:02:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1010466#M34412</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-06T16:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1042669#M35202</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please can you help us to get this function working also when the values are negative.&lt;/P&gt;&lt;P&gt;I actually need the same that Richard but only substracting not adding.&lt;/P&gt;&lt;P&gt;Or is there other work around.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;m&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 00:22:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1042669#M35202</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-23T00:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1042786#M35205</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;something like this or is there an easier way? Thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;let func = (StartDate as date, WorkDays as number) =&amp;gt;
let

WorkDays2 = (Number.Abs(WorkDays)*2)+7,
negative = 
if Date.DayOfWeek(StartDate,Day.Monday)=5 then Date.AddDays(StartDate,-1) else
if Date.DayOfWeek(StartDate,Day.Monday)=6 then Date.AddDays(StartDate,-2) else StartDate,
positive = 
if Date.DayOfWeek(StartDate,Day.Monday)=5 then Date.AddDays(StartDate,2) else
if Date.DayOfWeek(StartDate,Day.Monday)=6 then Date.AddDays(StartDate,1) else
StartDate,
startDate = if WorkDays &amp;lt;0 then negative else
positive,
ListOfDates = if WorkDays &amp;lt;0 then List.Dates(startDate, WorkDays2,#duration(-1,0,0,0))
else List.Dates(startDate, WorkDays2,#duration(1,0,0,0)),
DeleteWeekends = List.Select(ListOfDates, each Date.DayOfWeek(_,1) &amp;lt; 5 ),
WorkDate = List.Range(DeleteWeekends,Number.Abs(WorkDays),1),

Result = WorkDate{0}

in
Result
in
func&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 01:42:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1042786#M35205</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-23T01:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1043325#M35219</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;if that works for you, I would rather not spend my time on it and like to leave it as it is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 06:26:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1043325#M35219</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2020-04-23T06:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1876216#M55634</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp; - i have a requriement to extract data from DWH only on weekdays , so i was thinking of using this function to pass the date , for e.g. for Monday run date , my run_date should be last Friday date . The above function works fine , as long as you pass the start date .&amp;nbsp;&lt;/P&gt;&lt;P&gt;but in my case , my start date would always be today to get the last business work date ,&amp;nbsp; could you please help here . Thanks in advance&lt;/P&gt;</description>
      <pubDate>Tue, 01 Jun 2021 23:15:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1876216#M55634</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-06-01T23:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1877181#M55659</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;I don't understand the requirment, unfortunately.&lt;BR /&gt;Could you please give an example with sample values (before and desired after)?&lt;BR /&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 08:40:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1877181#M55659</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2021-06-02T08:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1878769#M55705</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp; ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apologies if i was not clear on my requirement . Request :&amp;nbsp; i just want to retrive the last working day and pass this date as a parameter in another script to fetch data .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So from this function , all i want is to retreive the last working day . E.g. If today is Monday (31 May 2021) , then my last working day is Last friday ( 28 May 2021) .&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For clarity - all days Mon-Fridays are working day in our firm , which includes all public holidays as well, so data is made available from Monday to Friday in our DWH .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above function you had shared is great but i would need to pass a date there , in my case the start date would always be today . i tried to pass the start date as&amp;nbsp;(DateTime.Date(DateTime.LocalNow())) but was unsuccessful .&lt;/P&gt;&lt;P&gt;Hope you can help me here .&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 23:45:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1878769#M55705</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-06-02T23:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: WorkDays Function in Power Query M</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1881261#M55789</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp; please ignore my requirement , i got the solution working now . Again thanks for your time .&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 04:54:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1881261#M55789</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-06-04T04:54:21Z</dc:date>
    </item>
  </channel>
</rss>

