1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerQuery: 休日テーブルを利用した、○○営業日前/後の取得の仕方

Posted at

背景

会社で質問が上がっていたので、たまには触ろうと思った記録

結論

休日テーブルがある前提なので、それを利用して、営業日かどうかで減算していく関数を作った
あとは、カスタム関数の適用をすれば、以下な感じになる

image.png

関数

GetPreviousBusinessDayWithHolidayTable

休日テーブルで、土日の定義をやめたほうが楽だと思う。
その場合は、isWeekendOrHoliday をコメントアウトしてあるやつにする

休日スキップ用
// GetPreviousBusinessDayWithHolidayTable
(currentDate as date, businessDays as number, holidays as list) as date =>
    let
        // 初期値の設定
        dayCount = 0,
        dateCheck = currentDate,

        // 営業日を数える関数
        countBusinessDays = (dateToCheck as date, daysToCount as number) as date =>
        let
            // 日付を逆に進める
            previousDay = Date.AddDays(dateToCheck, -1),
            // 土日と休日をスキップする
            // isWeekendOrHoliday = Date.DayOfWeek(previousDay, Day.Monday) > 4 or List.Contains(holidays, previousDay),
            // 休日のみスキップする
            isWeekendOrHoliday = List.Contains(holidays, previousDay),
            // カウントする日数を調整
            newDaysToCount = if isWeekendOrHoliday then daysToCount else daysToCount - 1,
            // 営業日を減らす
            newDayCount = if isWeekendOrHoliday then daysToCount else daysToCount - 1,
            // 終了条件
            result = if newDaysToCount = 0 then previousDay else @countBusinessDays(previousDay, newDayCount)
        in
            result,

        // 初期関数呼び出し
        resultDate = countBusinessDays(dateCheck, businessDays)
    in
        resultDate

GetPreviousBusinessDay

比較用に、土日だけスキップするやつ
休日テーブルなんて面倒だぜ!って場合はこれで。

土日のみスキップ
// GetPreviousBusinessDay
// 3営業日前を計算する関数
 (currentDate as date, businessDays as number) as date =>
let
    // 初期値の設定
    dayCount = 0,
    dateCheck = currentDate,

    // 営業日を数える関数
    countBusinessDays = (dateToCheck as date, daysToCount as number) as date =>
    let
        // 日付を逆に進める
        previousDay = Date.AddDays(dateToCheck, -1),
        // 土日をスキップする
        isWeekend = Date.DayOfWeek(previousDay, Day.Monday) > 4,
        // カウントする日数を調整
        newDaysToCount = if isWeekend then daysToCount else daysToCount - 1,
        // 営業日を減らす
        newDayCount = if isWeekend then daysToCount else daysToCount - 1,
        // 終了条件
        result = if newDaysToCount = 0 then previousDay else @countBusinessDays(previousDay, newDayCount)
    in
        result,

    // 初期関数呼び出し
    resultDate = countBusinessDays(dateCheck, businessDays)
in
    resultDate

参考データ

休日定義

image.png

あとがき

Rest API か、Web Contents で、どっかに公開されてる祝日情報さえ取れれば定義も不要なんだけどね
とはいえ、社内用で提供していることは難しいかな。
あるとしたら、Excel定義が多そう。まぁ、それも Excel.Contents で、か

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?