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?

Power Queryで西暦から和暦を計算する(カスタム関数)

Posted at

Power Query:西暦から和暦を計算する - なんでもエクセル(Nandemo Excel)を参考にカスタム関数で和暦変換。

令和元年と出るようにしています。

(inputDate as date) as text =>
let
    // 元号の開始日と名称のリスト
    eras = {
        [Name = "令和", StartDate = #date(2019, 5, 1)],
        [Name = "平成", StartDate = #date(1989, 1, 8)],
        [Name = "昭和", StartDate = #date(1926, 12, 25)],
        [Name = "大正", StartDate = #date(1912, 7, 30)],
        [Name = "明治", StartDate = #date(1868, 1, 25)]
    },
    // 入力日付に対応する元号を特定
    currentEra = List.First(List.Select(eras, each inputDate >= _[StartDate])),
    // 和暦年の計算
    eraYear = Date.Year(inputDate) - Date.Year(currentEra[StartDate]) + 1,
    // "元年"の処理
    eraYearText = if eraYear = 1 then "元年" else Text.From(eraYear) & "年",
    // 和暦日付の組み立て
    warekiDate = currentEra[Name] & eraYearText & Date.ToText(inputDate, "M月d日")
in
    warekiDate

使い方

  1. 空のクエリ→詳細エディタにして上記コードを入力
  2. 列の追加→カスタム関数の呼び出し
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?