LoginSignup
3
3

More than 3 years have passed since last update.

小ネタ:日付と時刻の表を生成する

Last updated at Posted at 2021-01-01

以前、カレンダーを作る関数の記事を書きました。
今日たまたま時刻を足したものを作ったので、整理して投稿しておきます。

実行した様子

Excelシートから呼ぶ時はこんな具合です。当然ながら半角で入力しましょう。
image.png
出来上がり品はこんな感じ
image.png

コード

日にちは1日ごと、時刻は1時間間隔で固定してます。
変えたい場合は引数追加するか、コード書き換えてください。
(日にち間隔は#duration(1,0,0,0)のところ、時間間隔は#duration(0,1,0,0)のところ)

(開始日 as date,終了日 as date,時刻始 as time,時刻終 as time) as table=>
let
    日にちリスト   = List.Dates(開始日,Duration.TotalDays(終了日-開始日)+1,#duration(1,0,0,0) ),
    日にちだけ     = Table.FromColumns( {日にちリスト},type table[日にち=date] ),
    時刻リスト     = List.Times(時刻始,Duration.TotalHours(時刻終-時刻始)+1,#duration(0,1,0,0) ),
    時刻リスト入れ = Table.AddColumn(日にちだけ, "時刻", each 時刻リスト ,type {time} ),
    時刻列の展開   = Table.ExpandListColumn(時刻リスト入れ, "時刻")
in
    時刻列の展開

注意点など

テスト環境:Excel365(バージョン2011、64bit)
Power Query:2.87.846.0

リンク

durationの書き方
https://docs.microsoft.com/en-us/powerquery-m/sharpduration
List.Datesの仕様
https://docs.microsoft.com/en-us/powerquery-m/list-dates
List.Timesの仕様
https://docs.microsoft.com/en-us/powerquery-m/list-times

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