Skills Advisor | Data Transformation

Business Day Calculation in Power Query

This guide outlines a method to add business days to a date in Power Query while excluding weekends and specified holidays, featuring M language code and best practices for modular functions and error handling.


Empty image or helper icon

Prompt

adding a business days and  no holidays in  power query

Answer

Data Transformation: Adding Business Days Excluding Holidays in Power Query

Problem Analysis

Transforming date-related data to account for business days while excluding holidays is a common issue in data analysis. This task involves:

  1. Adding a specified number of business days to a given date.
  2. Ensuring that weekends and holidays are excluded from the calculation.

Solution Outline

To address this problem in Power Query, we will:

  1. Create a list of dates accounting for business days.
  2. Exclude weekends from this list.
  3. Exclude specified holidays from the final date list.
  4. Create a custom function in M language to add business days.

Steps and Explanation

1. Create a List of Dates

Generate a list of dates starting from the given date and extending for the required number of days.

let
    StartDate = #date(2023, 10, 9),
    NumberOfDays = 30,
    DateList = List.Dates(StartDate, NumberOfDays, #duration(1, 0, 0, 0))
in
    DateList

2. Exclude Weekends

Filter out weekends from the DateList.

let
    StartDate = #date(2023, 10, 9),
    NumberOfDays = 30,
    DateList = List.Dates(StartDate, NumberOfDays, #duration(1, 0, 0, 0)),
    BusinessDays = List.Select(DateList, each Date.DayOfWeek(_, Day.Monday) < 5)
in
    BusinessDays

3. Exclude Holidays

Assume we have a list of holidays. We will exclude these from our BusinessDays list.

let
    StartDate = #date(2023, 10, 9),
    NumberOfDays = 30,
    DateList = List.Dates(StartDate, NumberOfDays, #duration(1, 0, 0, 0)),
    BusinessDays = List.Select(DateList, each Date.DayOfWeek(_, Day.Monday) < 5),
    Holidays = {
        #date(2023, 10, 10), 
        #date(2023, 10, 20)
    },
    BusinessDaysExcludingHolidays = List.Difference(BusinessDays, Holidays)
in
    BusinessDaysExcludingHolidays

4. Custom Function to Add Business Days

Create a custom M function to add a specified number of business days to a start date, excluding weekends and holidays.

// Function Definition
let
    AddBusinessDays = (startDate as date, daysToAdd as number, holidays as list) as date =>
    let
        DateList = List.Dates(startDate, daysToAdd + 60, #duration(1, 0, 0, 0)), // Adding buffer
        BusinessDays = List.Select(DateList, each Date.DayOfWeek(_, Day.Monday) < 5),
        BusinessDaysExcludingHolidays = List.Difference(BusinessDays, holidays),
        ResultDate = List.FirstN(BusinessDaysExcludingHolidays, daysToAdd){daysToAdd - 1}
    in
        ResultDate
in
    AddBusinessDays

// Usage Example
let
    StartDate = #date(2023, 10, 9),
    DaysToAdd = 15,
    Holidays = {
        #date(2023, 10, 10), 
        #date(2023, 10, 20)
    },
    Result = AddBusinessDays(StartDate, DaysToAdd, Holidays)
in
    Result

Promoting Best Practices

  1. Modular Functions: Break down complex operations into modular functions for better readability and maintenance.
  2. Error Handling: Implement error handling to deal with unexpected values or situations.
  3. Reusable Code: Create reusable functions to save time on recurring tasks.

Further Learning

For a deeper understanding of Data Transformation and more advanced techniques, consider the courses offered on the Enterprise DNA Platform.

By following this structured approach, you can effectively add business days to a date in Power Query, excluding weekends and holidays. This method ensures accuracy and efficiency in handling date-related transformations.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This guide outlines a method to add business days to a date in Power Query while excluding weekends and specified holidays, featuring M language code and best practices for modular functions and error handling.