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

Time table issue in PowerBI - (for stacked bar chart)

提前感谢您的宝贵时间!

我正在为 SQL 使用 DirectQuery。无法导入数据,因为 ddbb 太大。 在 main/fact table 中,我有一列 "date"-dd/MM/yyyy 和一列 "time"-HH:MM:SS。我已经为日期计算了一个日历 table,并且我尝试使用我在互联网上使用 M 或 DAX 找到的不同解决方案来计算时间 table。但是 none 时间 table 工作正常。例如:按 "the hour 8" 过滤它必须突出显示 8:00:00 和 8:59:59 之间的所有时间,但它没有向我显示该时间范围内的所有数据条目。

我需要制作一个堆积柱状图 chart/stacked 柱状图:X 轴:HOUR Y 轴:按语言分组的操作数。但由于某种原因,并非所有操作都显示在图表中。我相信计算的时间 table 没有从我的事实 table 中正确过滤时间列。

PowerBI printscreen

注意:我有最新版本的 PowerBI-november。

*注:暂时table我用过这些方式:

  1. 中号:
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 查询验证它。我相信这是因为时间 table 在双方都与事实 table 相关联。时间 table 没有唯一值,但我认为它应该与日历 table 一样。现在我将时间 table 与 DAX 代码 (3) 一起使用。

谢谢!

您可以使用 DAX 简单地将 "Hour" 计算列添加到您的事实 table:

Hour = HOUR ( FactTable[I_Time] )

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

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"