-
Notifications
You must be signed in to change notification settings - Fork 354
Add WORKDAY function for calculating working days excluding weekends and holidays #2994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/add-workday-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c5397a
Initial plan
Copilot cdee6a7
Implement WORKDAY function with runtime logic and tests
Copilot e3fd58a
Fix test case expected values for WORKDAY function
Copilot 0cbce62
Address code review feedback - improve error handling and performance
Copilot 8707c3c
Address PR feedback: support variadic holiday dates and ErrorValue ha…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Licensed under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.Contracts; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
|
|
@@ -926,6 +927,111 @@ public static FormulaValue EOMonth(EvalVisitor runner, EvalVisitorContext contex | |
| } | ||
| } | ||
|
|
||
| public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot If any arg is an |
||
| { | ||
| // Check for ErrorValue in any argument and return it immediately | ||
| foreach (var arg in args) | ||
| { | ||
| if (arg is ErrorValue) | ||
| { | ||
| return arg; | ||
| } | ||
| } | ||
|
|
||
| var timeZoneInfo = runner.TimeZoneInfo; | ||
|
|
||
| DateTime startDate = runner.GetNormalizedDateTime(args[0]); | ||
|
|
||
| if (args[1] is not NumberValue daysValue) | ||
| { | ||
| throw CommonExceptions.RuntimeMisMatch; | ||
| } | ||
|
|
||
| // Truncate toward zero for Excel compatibility | ||
| int days = (int)Math.Truncate(daysValue.Value); | ||
|
|
||
| // Collect holidays from individual date arguments (args[2] onwards) | ||
| var holidays = new HashSet<DateTime>(); | ||
| for (int i = 2; i < args.Length; i++) | ||
| { | ||
| if (args[i] is not BlankValue) | ||
| { | ||
| if (args[i] is DateValue dateValue) | ||
| { | ||
| holidays.Add(dateValue.GetConvertedValue(timeZoneInfo).Date); | ||
| } | ||
| else if (args[i] is DateTimeValue dateTimeValue) | ||
| { | ||
| holidays.Add(dateTimeValue.GetConvertedValue(timeZoneInfo).Date); | ||
| } | ||
| else if (args[i] is TableValue holidayTable) | ||
| { | ||
| // Also support table for backward compatibility | ||
| foreach (var row in holidayTable.Rows) | ||
| { | ||
| if (row.IsValue) | ||
| { | ||
| var fields = row.Value.Fields.ToArray(); | ||
| if (fields.Length > 0) | ||
| { | ||
| var field = fields[0]; | ||
| if (field.Value is DateValue dateVal) | ||
| { | ||
| holidays.Add(dateVal.GetConvertedValue(timeZoneInfo).Date); | ||
| } | ||
| else if (field.Value is DateTimeValue dateTimeVal) | ||
| { | ||
| holidays.Add(dateTimeVal.GetConvertedValue(timeZoneInfo).Date); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| DateTime currentDate = startDate.Date; | ||
|
|
||
| // Early return for zero days | ||
| if (days == 0) | ||
| { | ||
| DateTime resultDate = MakeValidDateTime(runner, currentDate, timeZoneInfo); | ||
| return new DateValue(irContext, resultDate); | ||
| } | ||
|
|
||
| int direction = days > 0 ? 1 : -1; | ||
| int remainingDays = Math.Abs(days); | ||
|
|
||
| while (remainingDays > 0) | ||
| { | ||
| currentDate = currentDate.AddDays(direction); | ||
|
|
||
| // Check if it's a weekend (Saturday or Sunday) | ||
| if (currentDate.DayOfWeek == DayOfWeek.Saturday || currentDate.DayOfWeek == DayOfWeek.Sunday) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Check if it's a holiday | ||
| if (holidays.Contains(currentDate)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| remainingDays--; | ||
| } | ||
|
|
||
| DateTime newDate = MakeValidDateTime(runner, currentDate, timeZoneInfo); | ||
| return new DateValue(irContext, newDate); | ||
| } | ||
| catch | ||
| { | ||
| return CommonErrors.ArgumentOutOfRange(irContext); | ||
| } | ||
| } | ||
|
|
||
| private static double WeekStartDay(double startOfWeek) | ||
| { | ||
| if (startOfWeek == 1 || startOfWeek == 2) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Workday.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
|
|
||
| // WORKDAY basic tests | ||
|
|
||
| // Basic forward workday calculations | ||
| >> Workday(Date(2024,8,24),30) | ||
| Date(2024,10,4) | ||
|
|
||
| // Basic backward workday calculations | ||
| >> Workday(Date(2024,8,24),-10) | ||
| Date(2024,8,12) | ||
|
|
||
| // Zero days should return the start date | ||
| >> Workday(Date(2024,8,24),0) | ||
| Date(2024,8,24) | ||
|
|
||
| // Starting on a Saturday (weekend) | ||
| >> Workday(Date(2024,8,24),1) | ||
| Date(2024,8,26) | ||
|
|
||
| // Starting on a Sunday (weekend) | ||
| >> Workday(Date(2024,8,25),1) | ||
| Date(2024,8,26) | ||
|
|
||
| // Starting on a Friday | ||
| >> Workday(Date(2024,8,23),1) | ||
| Date(2024,8,26) | ||
|
|
||
| // Starting on a Monday | ||
| >> Workday(Date(2024,8,26),1) | ||
| Date(2024,8,27) | ||
|
|
||
| // Multiple weeks forward | ||
| >> Workday(Date(2024,1,15),20) | ||
| Date(2024,2,12) | ||
|
|
||
| // Multiple weeks backward | ||
| >> Workday(Date(2024,2,15),-20) | ||
| Date(2024,1,18) | ||
|
|
||
| // Crossing year boundary forward | ||
| >> Workday(Date(2024,12,20),10) | ||
| Date(2025,1,3) | ||
|
|
||
| // Crossing year boundary backward | ||
| >> Workday(Date(2025,1,10),-10) | ||
| Date(2024,12,27) | ||
|
|
||
| // Fractional days should be truncated | ||
| >> Workday(Date(2024,8,24),5.9) | ||
| Date(2024,8,30) | ||
|
|
||
| // Negative fractional days should be truncated | ||
| >> Workday(Date(2024,8,24),-5.9) | ||
| Date(2024,8,19) | ||
|
|
||
| // WORKDAY with holidays | ||
|
|
||
| // Single holiday | ||
| >> Workday(Date(2024,8,24),5,Date(2024,8,28)) | ||
| Date(2024,9,2) | ||
|
|
||
| // Single holiday as table (backward compatibility) | ||
| >> Workday(Date(2024,8,24),5,Table({Date:Date(2024,8,28)})) | ||
| Date(2024,9,2) | ||
|
|
||
| // Multiple holidays | ||
| >> Workday(Date(2024,8,24),10,Date(2024,8,28),Date(2024,9,2)) | ||
| Date(2024,9,6) | ||
|
|
||
| // Multiple holidays as table (backward compatibility) | ||
| >> Workday(Date(2024,8,24),10,Table({Date:Date(2024,8,28)},{Date:Date(2024,9,2)})) | ||
| Date(2024,9,6) | ||
|
|
||
| // Holiday on weekend should not affect result | ||
| >> Workday(Date(2024,8,24),5,Date(2024,8,25)) | ||
| Date(2024,8,30) | ||
|
|
||
| // Backward with holidays | ||
| >> Workday(Date(2024,8,30),-5,Date(2024,8,26)) | ||
| Date(2024,8,21) | ||
|
|
||
| // Holiday that is the start date | ||
| >> Workday(Date(2024,8,26),5,Date(2024,8,26)) | ||
| Date(2024,9,2) | ||
|
|
||
| // Multiple holidays in range | ||
| >> Workday(Date(2024,12,20),10,Date(2024,12,25),Date(2025,1,1)) | ||
| Date(2025,1,7) | ||
|
|
||
| // WORKDAY with DateTime inputs | ||
|
|
||
| >> Workday(DateTime(2024,8,24,12,30,45),5) | ||
| Date(2024,8,30) | ||
|
|
||
| >> Workday(DateTime(2024,8,24,12,30,45),-5) | ||
| Date(2024,8,19) | ||
|
|
||
| // WORKDAY with Blank values | ||
|
|
||
| >> Workday(Blank(),10) | ||
| Date(1900,1,12) | ||
|
|
||
| >> Workday(Date(2024,8,24),Blank()) | ||
| Date(2024,8,24) | ||
|
|
||
| >> Workday(Blank(),Blank()) | ||
| Date(1899,12,30) | ||
|
|
||
| // Edge cases | ||
|
|
||
| // Large number of workdays forward | ||
| >> Workday(Date(2024,1,1),250) | ||
| Date(2024,12,16) | ||
|
|
||
| // Large number of workdays backward | ||
| >> Workday(Date(2024,12,31),-250) | ||
| Date(2024,1,16) | ||
|
|
||
| // Ensure date values are truncated and do not include time | ||
| >> Text(Workday(DateTime(2024,8,24,12,34,56),5),"yyyy-mm-dd hh:mm:ss") | ||
| "2024-08-30 00:00:00" | ||
|
|
||
| >> Value(Workday(Date(2024,8,24),5)) | ||
| 45535 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should handle a
Nnumber of arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented
GetSignatures(int arity)to handle N number of arguments. The function now accepts up toint.MaxValueholiday dates. Updated in commit 8707c3c.