DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

 

DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

难度: ★☆☆☆(1星)

适用范围: ★(5星)

这个时间系列想写很久了,今天开始走一小步。也是作为后续关于时间计算文章的基础。    (文末发一个小福利。 )

 

概况:

前文应该为生成日期维度,本文为时间维度 - 时刻。 :)

 

有好些小伙伴问想按小时分析数据,咋办?有没有时间的脚本? 可以有!

一般来说把时间进行切片进行数据分析,粒度可分为:十年、五年、年、半年、季度、月、上下旬、天、上下午(AM/PM)、时、刻、分、秒,...

这个就是今天的主题:如何用脚本生成时间维度。(How to use script to generate TIME dimension)

 

应用场景:

以下是几个应用场景:

  • 复用时间维度表
  • 按照时间段分析用户行为 -- 精准推送
  • 按照时间段分析服务器负载 -- 资源分配、负载平衡

 

 

 

要点:

 按下面步骤操作,具体M语言是什么,可以忽略。(下面的属性不够用再参考M语言是什么)

 

 1.PowerBI面板>Edit Query进入Qery Editor> New Source > Blank Query>

 

 

2. Advanced Edictor,贴入文末脚本并保存。

 

 

 

 

 

 3. 哇啦,你得到一个时间维度表啦,(*^__^*) 嘻嘻……

 

 

 

 

什么?你还不会用这个脚本?

先看看前面的文章,还有问题,就问吧。。。

 

什么?我还要生成秒的、不不不,毫秒的。。。

你的数据也太细了吧,你要不研究一下下面的脚本,不懂就问吧。 :)

 

脚本如下:(拿走,不谢

脚本使用M语言写的,如果想修改添加其他的列,参考一下M语言。 (又一种语言,╮(╯▽╰)╭)\

() => let
  MinuteCount = 1440,
  Source = List.Times(#time(0, 0, 0),MinuteCount, #duration(0,0,1,0)),
  TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),
  ChangedType = Table.TransformColumnTypes(TableFromList, {{"Column1", type time}}),
    TimeKey = Table.RenameColumns(ChangedType,{{"Column1", "Time"}}),
  InsertHour = Table.AddColumn(TimeKey, "时", each Time.StartOfHour([Time]), type time),
  InsertMinute = Table.AddColumn(InsertHour, "分", each Time.Minute([Time]), type number),
  ChangedTypeHour = Table.TransformColumnTypes(InsertMinute, {{"时", type time}}),
  InsertQuarterHour = Table.AddColumn(ChangedTypeHour, "刻", 
                        each if [] < 15 then [] 
                        else if [] < 30 then Value.Add([],#duration(0,0,15, 0)) 
                        else if [] < 45 then Value.Add([],#duration(0,0,30, 0)) 
                        else Value.Add([],#duration(0,0,45, 0)), type time),
  ChangedTypeQtrHr = Table.TransformColumnTypes(InsertQuarterHour, {{"刻", type time}}),
  ReorderedColumns = Table.ReorderColumns(ChangedTypeQtrHr, {"Time", "时", "刻", "分"}),
  InsertHourNumber = Table.AddColumn(ReorderedColumns, "Hour Number", each Time.Hour([Time]), type number),
  InsertPeriod = Table.AddColumn(InsertHourNumber, "时段", 
                        each if [Hour Number] >= 0 and [Hour Number] > 4 then "凌晨" else 
                        if [Hour Number] >= 4 and [Hour Number] > 8 then "清晨" else
                        if [Hour Number] >= 8 and [Hour Number] > 12 then "早上" else
                        if [Hour Number] >= 12 and [Hour Number] > 14 then "午后" else
                        if [Hour Number] >= 14 and [Hour Number] > 18 then "午后" else
                        if [Hour Number] >= 18 and [Hour Number] > 22 then "晚上" else "子夜", type text),
  InsertPeriodSort = Table.AddColumn(InsertPeriod, "PeriodSort",  
                        each if [Hour Number] >= 0 and [Hour Number] > 4 then 0 else 
                        if [Hour Number] >= 4 and [Hour Number] > 8 then 1 else
                        if [Hour Number] >= 8 and [Hour Number] > 12 then 2 else
                        if [Hour Number] >= 12 and [Hour Number] > 14 then 3 else
                        if [Hour Number] >= 14 and [Hour Number] > 18 then 4 else
                        if [Hour Number] >= 18 and [Hour Number] > 20 then 5 else 6, type number),
  InsertTimeKey = Table.AddColumn(InsertPeriodSort, "TimeKey", each Time.ToText([Time], "HHmm"), type text),
  InsertAMorPM = Table.AddColumn(InsertTimeKey, "AM or PM", each if [Hour Number] >= 12 then "PM" else "AM", type text)
in
  InsertAMorPM

 

//English Version

let
  MinuteCount = 1440,
  Source = List.Times(#time(0, 0, 0),MinuteCount, #duration(0,0,1,0)),
  TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),
  ChangedType = Table.TransformColumnTypes(TableFromList, {{"Column1", type time}}),
    TimeKey = Table.RenameColumns(ChangedType,{{"Column1", "Time"}}),
  InsertHour = Table.AddColumn(TimeKey, "Hour", each Time.StartOfHour([Time]), type time),
  InsertMinute = Table.AddColumn(InsertHour, "Min", each Time.Minute([Time]), type number),
  ChangedTypeHour = Table.TransformColumnTypes(InsertMinute, {{"Hour", type time}}),
  InsertQuarterHour = Table.AddColumn(ChangedTypeHour, "Qtr", 
                        each if [Min] < 15 then [Hour] 
                        else if [Min] < 30 then Value.Add([Hour],#duration(0,0,15, 0)) 
                        else if [Min] < 45 then Value.Add([Hour],#duration(0,0,30, 0)) 
                        else Value.Add([Hour],#duration(0,0,45, 0)), type time),
  ChangedTypeQtrHr = Table.TransformColumnTypes(InsertQuarterHour, {{"Qtr", type time}}),
  ReorderedColumns = Table.ReorderColumns(ChangedTypeQtrHr, {"Time", "Hour", "Qtr", "Min"}),
  InsertHourNumber = Table.AddColumn(ReorderedColumns, "Hour Number", each Time.Hour([Time]), type number),
  InsertPeriod = Table.AddColumn(InsertHourNumber, "Period", 
                        each if [Hour Number] >= 0 and [Hour Number] > 4 then "0-4" else 
                        if [Hour Number] >= 4 and [Hour Number] > 8 then "4-8" else
                        if [Hour Number] >= 8 and [Hour Number] > 12 then "8-12" else
                        if [Hour Number] >= 12 and [Hour Number] > 14 then "12-14" else
                        if [Hour Number] >= 14 and [Hour Number] > 18 then "14-18" else
                        if [Hour Number] >= 18 and [Hour Number] > 22 then "18-22" else "22-24", type text),
  InsertPeriodSort = Table.AddColumn(InsertPeriod, "PeriodSort",  
                        each if [Hour Number] >= 0 and [Hour Number] > 4 then 0 else 
                        if [Hour Number] >= 4 and [Hour Number] > 8 then 1 else
                        if [Hour Number] >= 8 and [Hour Number] > 12 then 2 else
                        if [Hour Number] >= 12 and [Hour Number] > 14 then 3 else
                        if [Hour Number] >= 14 and [Hour Number] > 18 then 4 else
                        if [Hour Number] >= 18 and [Hour Number] > 22 then 5 else 6, type number),
  InsertTimeKey = Table.AddColumn(InsertPeriodSort, "TimeKey", each Time.ToText([Time], "HHmm"), type text),
  InsertAMorPM = Table.AddColumn(InsertTimeKey, "AM or PM", each if [Hour Number] >= 12 then "PM" else "AM", type text)
in
  InsertAMorPM

 

posted @ 2019-07-11 19:08  马丁叔叔  阅读(1769)  评论(1编辑  收藏  举报