Forum Discussion
Working days date count
Hello
you can use my function I've developed some time ago. This is the complete function
// fnNetWorkDays2
(DateStart as date, DateEnd as date, optional Holidays as list) =>
let
Switch = DateStart > DateEnd,
StartDate = if Switch then DateEnd else DateStart,
EndDate = if Switch then DateStart else DateEnd,
HolidayIntern = if Holidays = null then List.Buffer({}) else List.Buffer(Holidays),
CreateListDates =List.Buffer( List.Dates
(
StartDate,
Duration.TotalDays
(
EndDate - StartDate
)
+1,
#duration(1,0,0,0)
)),
ExcludeWeekend = List.Select
(
CreateListDates,
each
Date.DayOfWeek
(
_,
Day.Monday
)
<5
),
ExcludeHoliday =
if Holidays = null then ExcludeWeekend else List.Difference
(
ExcludeWeekend,
HolidayIntern
),
Result = List.Count
(
ExcludeHoliday
),
FinalResult = try (if Switch then -1 else 1) * (Result) otherwise null
in
FinalResult
Here a small practical example how to invoke the function "fnNetWorkDays2"
let
Source = #table
(
{"Task","Start","End"},
{
{"1","43831","43905"}, {"2","43525","44122"}
}
),
ToDate = Table.TransformColumns
(
Source,
{
{
"Start",
each Date.From(Number.From(_)),
type date
},
{
"End",
each Date.From(Number.From(_)),
type date
}
}
),
AddWorkingDays = Table.AddColumn
(
ToDate,
"WorkingDays",
each fnNetWorkDays2([Start],Date.From(DateTime.FixedLocalNow())),
type number
)
in
AddWorkingDays
Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hi Jimmy801 ,
Thanks for coming back to me. I'm so new at this and I don't really understand your response. Sorry.
I know from other posts that I the syntax that I need for the today M query is (DateTime.LocalNow()
But for me that is still erroring.
This is what I have changed my code to:
= Table.AddColumn(#"Changed Type1", "Working days count", each #"Working days query"([#"Changed On (Task)"], DateTime.FixedLocalNow()))
But I can't understand why this isn't working.
- Jimmy8016 years agoCommunity Champion
Hello EmilyM2019
first of all please copy my first code to a new blank query and name the query fnNetWorkDays2
then change your code as follows
= Table.AddColumn(#"Changed Type1", "Working days count", each fnNetWorkDays2([#"Changed On (Task)"], DateTime.FixedLocalNow()))
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy- EmilyM20196 years agoHelper II
Hi Jimmy801
When I copy and paste your code into a new blank query it doesn't load in as an fx query, it shows as ABC and all the text is in red.
What am I doing wrong?
Thanks, Emily.
- Jimmy8016 years agoCommunity Champion
Hello EmilyM2019
most probably you are copying into the formula bar.
Open the advanced editor, delete everything and paste my first code
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy