PowerBI中的时间表问题-(用于堆积的条形图)

时间:2018-12-05 10:02:48

标签: time powerbi stacked-chart timetable

提前感谢您的时间!

我正在使用DirectQuery for SQL。由于ddbb太大,因此无法导入数据。 在该主/事实表中,我有一列“日期” -dd / MM / yyyy和一列“时间” -HH:MM:SS。我已经计算出日期的日历表,并且尝试使用在Internet上使用M或DAX找到的不同解决方案来计算时间表。但是没有时间表能正常工作。例如:按“小时8”过滤,则必须突出显示8:00:00到8:59:59之间的所有时间,但是并不能显示该时间范围内的所有数据条目。

我需要制作一个堆积的条形图/堆积的条形图:X轴:HOUR Y轴:受语言限制的操作次数。但是由于某种原因,并不是所有的操作都在图表中显示。我认为计算出的时间表无法正确过滤我的事实表中的时间列。

PowerBI printscreen

注意:我确实拥有11月的PowerBI最新版本。

*注意:在时间表中,我使用了以下方法:

  1. 在M:
let

Source = List.Times(#time(0,0,0) , 1440, #duration(0,0,1,0)),
convertToTable = Table.FromList(Source, Splitter.SplitByNothing(), {"DayTime"}, null, ExtraValues.Error),
createTimeKey = Table.AddColumn(convertToTable, "TimeKey", each Time.ToText([DayTime], "HHmmss")),
hourIndex = Table.AddColumn(createTimeKey, "HourIndex", each Time.Hour([DayTime])),
minuteIndex = Table.AddColumn(hourIndex, "MinuteIndex", each Time.Minute([DayTime])),
setDataType = Table.TransformColumnTypes(minuteIndex,{{"DayTime", type time}, {"TimeKey", type text}, {"HourIndex", Int64.Type},
{"MinuteIndex", Int64.Type}})

in
setDataType
  1. 我发现了更复杂的M代码。我不需要这种细节,但我没有修改它,因为我发现它没有用。
let CreateTimeTable = () as table =>
let
 // Similar to our CreateDateTable script, we start with the smallest unit of the dimension, minute
 // There are a fixed number of minutes in a day, so no need for parameters here
 // 525,600 minutes divided by 365 days in a year = 1440 minutes in a day. 
 // Who says we never learn from Broadway musicals?
 MinuteCount = 1440,
 // Now create a Time type list for a total of 1440 minutes, incrementing one minute at a time
 Source = List.Times(#time(0, 0, 0),MinuteCount, #duration(0,0,1,0)),
 // Turn that list into a one column table
 TableFromList = Table.FromList(Source, Splitter.SplitByNothing()), 
 // Change that table's one column to type Time
 ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type time}}),
 // Rename column to Time
 RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Time"}}),
 // Start inserting columns for each unit of time to represent in the dimension
 InsertHour = Table.AddColumn(RenamedColumns, "Hour", each Time.StartOfHour([Time])),
 InsertMinute = Table.AddColumn(InsertHour, "Minute", each Time.Minute([Time])),
 ChangedTypeHour = Table.TransformColumnTypes(InsertMinute,{{"Hour", type time}}),
 // Creating levels in the hierarchy that might be useful for reporting. Omit if not useful to yours
 InsertQuarterHour = Table.AddColumn(ChangedTypeHour, "Quarter Hour", each if [Minute]<15 then [Hour] else if [Minute] < 30 then Value.Add([Hour],#duration(0,0,15, 0)) else if [Minute] < 45 then Value.Add([Hour],#duration(0,0,30, 0)) else Value.Add([Hour],#duration(0,0,45, 0))),
 ChangedTypeQtrHr = Table.TransformColumnTypes(InsertQuarterHour,{{"Quarter Hour", type time}}),
 ReorderedColumns = Table.ReorderColumns(ChangedTypeQtrHr,{"Time", "Hour", "Quarter Hour", "Minute"}),
 InsertHourNumber = Table.AddColumn(ReorderedColumns, "Hour Number", each Time.Hour([Time])),
 NextHour = Table.AddColumn(InsertHourNumber, "Next Hour", each Value.Add([Hour],#duration(0,1,0, 0))),
 NextQuarterHour = Table.AddColumn(NextHour, "Next Quarter Hour", each Value.Add([Quarter Hour],#duration(0,0,15, 0))),
 InsertPeriod = Table.AddColumn(NextQuarterHour, "Period of Day", 
 each if [Hour Number] >= 0 and [Hour Number] < 4 then "After Midnight" else 
 if [Hour Number] >= 4 and [Hour Number] < 8 then "Early Morning" else
 if [Hour Number] >= 8 and [Hour Number] < 12 then "Late Morning" else
 if [Hour Number] >= 12 and [Hour Number] < 16 then "Afternoon" else
 if [Hour Number] >= 16 and [Hour Number] < 20 then "Evening" else "Late Night"),
 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] < 16 then 3 else
 if [Hour Number] >= 16 and [Hour Number] < 20 then 4 else 5),
 InsertTimeKey = Table.AddColumn(InsertPeriodSort, "TimeKey", each Time.ToText([Time], "HHmm"), type text)
 in
 InsertTimeKey
in
 CreateTimeTable
  1. 最后,我在DAX中创建的一个简单的DAX列就这么简单:
Hora = VALUES(FactTable[I_Time])
hour = FORMAT(Hora[I_Time]; "HH")
hora2 = time(Hora[hour];0;0) 

这就是我要实现的目标! Purpose!但由于某些原因,数据无法正确显示。我正在用SQL查询验证它。我认为这是因为时间表与事实表在许多方面都有很多联系。时间表没有唯一的值,但我认为它应该与日历表一样。现在,我将时间表与DAX代码(3)一起使用。

谢谢!

1 个答案:

答案 0 :(得分:0)

您只需使用DAX将一个“小时”计算列添加到事实表中即可。

Hour = HOUR ( FactTable[I_Time] )

或者您可以使用此M代码(问题中的#2的简化版本)生成TIME表,并创建与事实表的Time列的关系:

let
    Source = List.Times(#time(0, 0, 0), 1440, #duration(0,0,1,0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Time"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Time", type time}}),
    #"Inserted Hour" = Table.AddColumn(#"Changed Type", "Hour", each Time.Hour([Time]), Int64.Type)
in
    #"Inserted Hour"