如何将Power BI中的时间序列转换为时间段

时间:2017-01-11 03:49:34

标签: database powerbi

我有一张包含以下数据的表格:

Time                 Value
2016-01-01 00:01:02  'a'
2016-01-02 10:00:01  'b'
2016-01-02 23:45:00  'c'

但我希望能够生成一个包含常规时间和相应值的新表格,例如:

Data for 2016-01-02 by hour:

Time                 Value
2016-02-02 00:00:00  'a'
2016-02-02 01:00:00  'a'
2016-02-02 02:00:00  'a'
...
2016-02-02 10:00:00  'a'
2016-02-02 11:00:00  'b'
2016-02-02 12:00:00  'b'
...
2016-02-02 22:00:00  'b'
2016-02-02 23:00:00  'b'

任何人都可以帮助解决方法吗?

1 个答案:

答案 0 :(得分:1)

这个怎么样?

let
    src = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwNNM1MAQiBQMDKwNDKwMjJR2lRKVYHSQ5IwVDoBxIGiiXhC5nZGxlYgqUBsolK8XGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Time = _t, Value = _t]),
    ch_type = Table.TransformColumnTypes(src,{{"Time", type datetime}}),
    StartOfHour = Table.AddColumn(ch_type, "StartOfHour", each Time.StartOfHour([Time])),
    i = Table.AddIndexColumn(StartOfHour, "i", 1, 1),
    j = Table.AddIndexColumn(i, "j", 0, 1),
    join = Table.NestedJoin(j,{"i"},j,{"j"},"NewColumn",JoinKind.LeftOuter),
    t_expand = Table.ExpandTableColumn(join, "NewColumn", {"StartOfHour"}, {"NewColumn.StartOfHour"}),
    dur = Table.AddColumn(t_expand, "StartOfHour_diff", each if [NewColumn.StartOfHour] is null then {[StartOfHour]} else List.Transform({0..Duration.Hours([NewColumn.StartOfHour]-[StartOfHour])-1}, (l)=>[StartOfHour]+#duration(0,l,0,0))),
    del = Table.RemoveColumns(dur,{"Time", "StartOfHour", "i", "j", "NewColumn.StartOfHour"}),
    l_expand = Table.ExpandListColumn(del, "StartOfHour_diff"),
    rename = Table.RenameColumns(l_expand,{{"StartOfHour_diff", "Time"}}),
    reorder = Table.ReorderColumns(rename,{"Time", "Value"})
in
    reorder

enter image description here