Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Couldn't find any posts about functions that can add or subtract workdays from a date, but I'm sure someone will point out a simpler method after I post this. 🙃
Works mostly like WORKDAY() does in Excel:
fnWorkday(
#date(2021,08,20), //a Friday
#duration(0,0,0,1) //one *second* later
)
…returns Monday 2021-08-22. Conversely,
fnWorkday(
#date(2021,08,22), //a Monday
#duration(0,0,0,1) //one *second* before
)
…returns Friday 2021-08-20.
fnWorkday=(
this_date as date,
this_move as duration
)=> let
fnDurationSign=(_)=> Number.Sign(
Number.From(_)
),
ACTION = if this_move>=#duration(0,0,0,0)
then [
DAYS = #duration(1,0,0,0),
MOVE = #duration(-1,0,0,0)
]
else [
DAYS = #duration(-1,0,0,0),
MOVE = #duration(1,0,0,0)
],
next_date = this_date+ACTION[DAYS],
next_move = this_move+ACTION[MOVE],
next_date_is_holiday = List.Contains(
List.Buffer(
HOLIDAYS //**IMPORTANT** A list of your own holidays to skip
), next_date
) or Date.DayOfWeek(next_date) = Day.Saturday
or Date.DayOfWeek(next_date) = Day.Sunday,
sign_changed = fnDurationSign(this_move)<>fnDurationSign(next_move)
in if this_move = #duration(0,0,0,0)
then this_date
else if sign_changed and not next_date_is_holiday
then next_date
else if next_date_is_holiday
then @fnWorkday(
next_date,
this_move
)
else @fnWorkday(
next_date,
next_move
),
Solved! Go to Solution.
@lbendlin And you're making an assumption that I was trying to emulate Excel's WORKDAYS.INTL().
Me: adds #ShowAndTell tag
Super User III: "It doesn't work for cases that are out-of-scope."
Me: Never posts here again
@lbendlin And you're making an assumption that I was trying to emulate Excel's WORKDAYS.INTL().
Me: adds #ShowAndTell tag
Super User III: "It doesn't work for cases that are out-of-scope."
Me: Never posts here again
You're making an assumption that Saturday and Sunday are weekend days. That assumption is not valid for a large number of countries.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.