Forum Discussion

Bryanna's avatar
Bryanna
Icon for Helper II rankHelper II
2 years ago
Solved

Adding working days to a date excluding weekends and holidays

Hi!

 

In another forum i found the below custom function to remove weekends when adding days to a date. I also need to exclude holidays. Any idea on how to exclude holidays from the below as well? Thanks!

 

let WorkDayAdd = (Start as date, WorkDays as number) as date =>
    let
        Source = List.Generate( () => Date.AddDays(Start, WorkDays * 4), each _ >= Date.AddDays(Start,1), each Date.AddDays(_, -1 )),
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Added Custom" = Table.AddColumn(#"Converted to Table", "WeekDay", each Date.DayOfWeek([Column1],Day.Monday)),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [WeekDay] < 5),
        #"Sort Table" = Table.Sort(#"Filtered Rows", {"Column1"}),
        #"Added Index" = Table.AddIndexColumn(#"Sort Table", "Index", 1, 1, Int64.Type),
        #"Filtered Rows 1" = Table.SelectRows(#"Added Index", each [Index] = WorkDays),
        #"Select Column" = Table.SelectColumns(#"Filtered Rows 1", "Column1")
    in
        Record.Field(Table.Max(#"Select Column", "Column1"), "Column1")
in
    WorkDayAdd

 

  • Bryanna,

     

    try this, but edit step Holidays in Advanced Editor and replace US_Holidays[Date] with your holidays Table[Column] reference, i.e. if you have table called Hol and in this table column date so you should replace it with Hol[date]

    let WorkDayAdd = (Start as date, WorkDays as number) as date =>
        let
            Holidays = List.Buffer(US_Holidays[Date]),
            Source = List.Generate( () => Date.AddDays(Start, WorkDays * 4), each _ >= Date.AddDays(Start,1), each Date.AddDays(_, -1 )),
            #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            #"Added Custom" = Table.AddColumn(#"Converted to Table", "WeekDay", each Date.DayOfWeek([Column1],Day.Monday)),
            #"Added Custom1 Holiday" = Table.AddColumn(#"Added Custom", "Holiday", each List.Contains(Holidays, [Column1]), type logical),
            #"Filtered Rows" = Table.SelectRows(#"Added Custom1 Holiday", each [WeekDay] < 5 and [Holiday] = false),
            #"Sort Table" = Table.Sort(#"Filtered Rows", {"Column1"}),
            #"Added Index" = Table.AddIndexColumn(#"Sort Table", "Index", 1, 1, Int64.Type),
            #"Filtered Rows 1" = Table.SelectRows(#"Added Index", each [Index] = WorkDays),
            #"Select Column" = Table.SelectColumns(#"Filtered Rows 1", "Column1")
        in
            Record.Field(Table.Max(#"Select Column", "Column1"), "Column1")
    in
        WorkDayAdd

     

9 Replies

  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

    Hi Bryanna, do you have a list of your country holiday days? Or could you send me a link to a webpage with your contry holidays?

    • Bryanna's avatar
      Bryanna
      Icon for Helper II rankHelper II

      Hi,

      US Holidays.

      New Years , Memorial Day, Independance Day, Labor Day, Thanksgiving, Christmas Day

       

      • dufoq3's avatar
        dufoq3
        Icon for Community Champion rankCommunity Champion

        Bryanna,

         

        try this, but edit step Holidays in Advanced Editor and replace US_Holidays[Date] with your holidays Table[Column] reference, i.e. if you have table called Hol and in this table column date so you should replace it with Hol[date]

        let WorkDayAdd = (Start as date, WorkDays as number) as date =>
            let
                Holidays = List.Buffer(US_Holidays[Date]),
                Source = List.Generate( () => Date.AddDays(Start, WorkDays * 4), each _ >= Date.AddDays(Start,1), each Date.AddDays(_, -1 )),
                #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
                #"Added Custom" = Table.AddColumn(#"Converted to Table", "WeekDay", each Date.DayOfWeek([Column1],Day.Monday)),
                #"Added Custom1 Holiday" = Table.AddColumn(#"Added Custom", "Holiday", each List.Contains(Holidays, [Column1]), type logical),
                #"Filtered Rows" = Table.SelectRows(#"Added Custom1 Holiday", each [WeekDay] < 5 and [Holiday] = false),
                #"Sort Table" = Table.Sort(#"Filtered Rows", {"Column1"}),
                #"Added Index" = Table.AddIndexColumn(#"Sort Table", "Index", 1, 1, Int64.Type),
                #"Filtered Rows 1" = Table.SelectRows(#"Added Index", each [Index] = WorkDays),
                #"Select Column" = Table.SelectColumns(#"Filtered Rows 1", "Column1")
            in
                Record.Field(Table.Max(#"Select Column", "Column1"), "Column1")
        in
            WorkDayAdd