LoginSignup
0
0

月末日および銀行最終営業日のリストを求める

Posted at

借入金のスケジュール表を作るついでに、久々に書いてみました。
コード中のtestは本当にただのテストなので、コメントアウトしてます。

出来上がりイメージ

image.png

コード

let
    startDate = #date(2023,6,30),
    endDate = #date(2026,6,30),
    // 除外したい日のリスト。手打ちの必要はない。
    holidayLst = List.Transform({2023,2024,2025}, each #date(_,12,31)),
    
    //日付を受け取って、直前の平日を返す関数。再帰的に実行する。
    getLastWeekday = (d as date)as date=>
        if List.Contains(holidayLst, d) then @getLastWeekday(d - #duration(1,0,0,0))
        //0を月曜として、曜日が0~6で返る。
        else if Date.DayOfWeek(d, Day.Monday) > 4 then @getLastWeekday(d - #duration(Date.DayOfWeek(d, Day.Monday)-4,0,0,0))
        else d,
 
    // test = let x = {#date(2023,6,10),#date(2023,6,11),#date(2023,6,12)} in List.Transform(x, getLastWeekday),

    // 日付を受け取って、その翌月の返済日を返す。ただし、引数が月末日でない場合はその日付の月の月末日を返す。
    fx = (d as date) as date=>
        let
            endOfThisMonth = Date.EndOfMonth(d),
            // Date.AddMonthsは必要ない。
            nextEndOfMonth = Date.EndOfMonth(endOfThisMonth + #duration(1,0,0,0))
        in
            if d = endOfThisMonth then getLastWeekday( nextEndOfMonth )
            else endOfThisMonth,
    // test3 = let x = {#date(2023,9,30),#date(2023,10,11),#date(2023,11,30)} in List.Transform(x, fx),
    lst = List.Generate(()=>startDate, each _ <= endDate, fx),
    result = Table.FromColumns({lst}),
    #"Added Custom" = Table.AddColumn(result, "検証用曜日", each Date.ToText([Column1],"ddd"), type text)
in
    #"Added Custom"

バージョン情報

Microsoft365のExcel(バージョン2305)
Power Query:2.117.362.0(64 ビット)

0
0
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
0
0