Skip to content

Conversation

Copy link

Copilot AI commented Nov 4, 2025

Implements Excel-compatible WORKDAY function that calculates a date n working days before/after a start date, excluding weekends (Saturday/Sunday) and optional holidays.

Changes

Function Definition

  • Added WorkdayFunction class in DateTime.cs with signature: Workday(start_date, days, [holiday1, holiday2, ...])
  • Registered in BuiltinFunctionsCore.cs
  • Supports variadic arguments (up to int.MaxValue) for individual holiday dates

Runtime Implementation

  • Iterative day-by-day calculation in LibraryDate.cs
  • HashSet-based O(1) holiday lookup
  • Math.Truncate() for fractional days (Excel parity)
  • Early return optimization for zero days
  • Consistent error handling with ReplaceBlankWith pattern matching EDate/EOMonth
  • ErrorValue checking - returns immediately if any argument is an ErrorValue
  • Supports both individual Date/DateTime arguments and Table-based holidays for backward compatibility

Localization

  • Added function description and parameter strings in Strings.cs and PowerFxResources.en-US.resx
  • Updated documentation to reflect variadic holiday arguments

Tests

  • Comprehensive test suite in Workday.txt covering: bidirectional calculations, weekend handling, year boundaries, holidays (individual and table-based), blank values, fractional truncation
  • All expectations verified against Excel behavior

Example

// 30 working days forward from Saturday
Workday(Date(2024,8,24), 30)  
// Returns: Date(2024,10,4)

// With individual holiday dates (new)
Workday(Date(2024,8,24), 5, Date(2024,8,28))
// Returns: Date(2024,9,2) - skips the holiday

// With multiple holiday dates
Workday(Date(2024,12,20), 10, Date(2024,12,25), Date(2025,1,1))
// Returns: Date(2025,1,7)

// With holiday table (backward compatible)
Workday(Date(2024,8,24), 5, Table({Date:Date(2024,8,28)}))
// Returns: Date(2024,9,2)

// Backward calculation
Workday(Date(2024,8,24), -10)
// Returns: Date(2024,8,12)

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>WORKDAY function is not available in Power-Fx</issue_title>
<issue_description>In Excel, we have WORKDAY function which returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). Working days exclude weekends and any dates identified as holidays. Use WORKDAY to exclude weekends or holidays when you calculate invoice due dates, expected delivery times, or the number of days of work performed.

</issue_description>

<agent_instructions>Create a new WORKDAY function that returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). Working days exclude weekends and any dates identified as holidays. Use WORKDAY to exclude weekends or holidays when you calculate. The function will take 2 or more arguments: Start_date (Required. A date that represents the start date.), days (Required. The number of nonweekend and nonholiday days before or after start_date. A positive value for days yields a future date; a negative value yields a past date.), holidays (Optional. An optional list of one or more dates to exclude from the working calendar, such as state and federal holidays and floating holidays. The list can be either a range of cells that contain the dates or an array constant of the serial numbers that represent the dates.) </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@anderson-joyle This function is not currently on our roadmap, but it is nice to have. Feel free to contribute to the project. @anderson-joyle > Working on this feature. I'll keep this thread updated.

Please note that @m365solutioninsights already send us a PR on this: #2638
I'll find time this week to review it.</comment_new>
<comment_new>@anderson-joyle
@m365solutioninsights I apologize for the delay in reviewing this. Can you please merge/rebase your branch so I can run a review of it this week?</comment_new>

Original prompt

This section details on the original issue you should resolve

<issue_title>WORKDAY function is not available in Power-Fx</issue_title>
<issue_description>In Excel, we have WORKDAY function which returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). Working days exclude weekends and any dates identified as holidays. Use WORKDAY to exclude weekends or holidays when you calculate invoice due dates, expected delivery times, or the number of days of work performed.
image
</issue_description>

<agent_instructions>Create a new WORKDAY function that returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). Working days exclude weekends and any dates identified as holidays. Use WORKDAY to exclude weekends or holidays when you calculate. The function will take 2 or more arguments: Start_date (Required. A date that represents the start date.), days (Required. The number of nonweekend and nonholiday days before or after start_date. A positive value for days yields a future date; a negative value yields a past date.), holidays (Optional. An optional list of one or more dates to exclude from the working calendar, such as state and federal holidays and floating holidays. The list can be either a range of cells that contain the dates or an array constant of the serial numbers that represent the dates.) </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@anderson-joyle This function is not currently on our roadmap, but it is nice to have. Feel free to contribute to the project. @anderson-joyle > Working on this feature. I'll keep this thread updated.

Please note that @m365solutioninsights already send us a PR on this: #2638
I'll find time this week to review it.</comment_new>
<comment_new>@anderson-joyle
@m365solutioninsights I apologize for the delay in reviewing this. Can you please merge/rebase your branch so I can run a review of it this week?</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add WORKDAY function to Power-Fx Add WORKDAY function for calculating working days excluding weekends and holidays Nov 4, 2025
Copilot AI requested a review from anderson-joyle November 4, 2025 22:55
public override bool IsSelfContained => true;

public WorkdayFunction()
: base("Workday", TexlStrings.AboutWorkday, FunctionCategories.DateTime, DType.Date, 0, 2, 3, DType.DateTime, DType.Number, DType.EmptyTable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3

The function has 2 required args, but have have multiple optional. This should be int.MaxValue instead of a fixed 3 value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Excel only supports 2 or 3 arguments (and the optional third argument is a list), so that's correct: https://support.microsoft.com/en-us/office/workday-function-f764a5b7-05fc-4494-9486-60d494efbf33

public override bool IsSelfContained => true;

public WorkdayFunction()
: base("Workday", TexlStrings.AboutWorkday, FunctionCategories.DateTime, DType.Date, 0, 2, 3, DType.DateTime, DType.Number, DType.EmptyTable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DType.EmptyTable

Starting from arg2 (0-based), all args should be DType.DateTime.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use DType.DateTime for all optional holiday arguments. Updated in commit 8707c3c.

{
}

public override IEnumerable<TexlStrings.StringGetter[]> GetSignatures()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetSignatures

This should handle a N number of arguments.

Copy link
Author

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 to int.MaxValue holiday dates. Updated in commit 8707c3c.

}
}

public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
Copy link
Contributor

@anderson-joyle anderson-joyle Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args

@copilot If any arg is an ErrorValue, return it and thats it.

Copy link
Contributor

@anderson-joyle anderson-joyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function has 2 required args, but have have multiple optional. This should be int.MaxValue instead of a fixed 3 value.

Starting from arg2 (0-based), all args types should be DType.DateTime.

The function signature should handle a N number of arguments.

When executing the function, If any of the args is an ErrorValue, return it and thats it.

@anderson-joyle
Copy link
Contributor

@copilot If any arg is an ErrorValue, return it and thats it.

Copy link
Author

Copilot AI commented Nov 5, 2025

@copilot If any arg is an ErrorValue, return it and thats it.

Added ErrorValue checking at the beginning of the Workday function. If any argument is an ErrorValue, it returns immediately. Updated in commit 8707c3c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WORKDAY function is not available in Power-Fx

3 participants