在Power BI中汇总时间数据类型

时间:2018-04-18 09:15:03

标签: powerbi powerbi-desktop

如何使Power BI显示时间数据类型列的总和?

场景:包含列Phase(文本)和Duration(SQL Server时间数据类型,也在Power BI中识别,如时间)的表。当我添加条形图,其中相位为轴,持续时间为值时,我只有选项显示持续时间的计数和计数不同,没有SUM。

添加DurationMeasure后的其他说明:

  • PowerBI中的表TBL1具有列持续时间(数据类型时间)。
  • 测量DurationMeasure在TBL1中添加,公式如下所示。
  • PowerBI不允许我将此度量添加到条形图到Values属性(请参见下图)。无法拖放“值”属性。如果我只是检查这个度量,它会像所有其他度量一样转到visual的Tooltip属性而不是Values属性。也许是因为它是字符串数据,但我不知道如何格式化它。

bar chart visual

这是DurationMeasure定义(如Karl Anka给出的链接):

    DurationMeasure = 
// Duration formatting 
// * @konstatinos 1/25/2016
// * Given a number of seconds, returns a format of "hh:mm:ss"
//
// We start with a duration in number of seconds
VAR Duration = SUM(TBL1[DurationSeconds])
// There are 3,600 seconds in an hour
VAR Hours =
    INT ( Duration / 3600)
// There are 60 seconds in a minute
VAR Minutes =
    INT ( MOD( Duration - ( Hours * 3600 );3600 ) / 60)
// Remaining seconds are the remainder of the seconds divided by 60 after subtracting out the hours 
VAR Seconds =
    ROUNDUP(MOD ( MOD( Duration - ( Hours * 3600 );3600 ); 60 );0) // We round up here to get a whole number
// These intermediate variables ensure that we have leading zero's concatenated onto single digits
// Hours with leading zeros
VAR H =
    IF ( LEN ( Hours ) = 1; 
        CONCATENATE ( "0"; Hours );
        CONCATENATE ( ""; Hours )
      )
// Minutes with leading zeros
VAR M =
    IF (
        LEN ( Minutes ) = 1;
        CONCATENATE ( "0"; Minutes );
        CONCATENATE ( ""; Minutes )
    )
// Seconds with leading zeros
VAR S =
    IF (
        LEN ( Seconds ) = 1;
        CONCATENATE ( "0"; Seconds );
        CONCATENATE ( ""; Seconds )
    )
// Now return hours, minutes and seconds with leading zeros in the proper format "hh:mm:ss"
RETURN
    CONCATENATE (
        H;
        CONCATENATE ( ":"; CONCATENATE ( M; CONCATENATE ( ":"; S ) ) )
    )

提前致谢

1 个答案:

答案 0 :(得分:1)

如果将持续时间转换为十进制数,则可以显示总和。然后是值1 = 1 day = 24 hour = 1440 minutes = 86400 seconds。所以0.5的值意味着12小时。在图表中将其显示为小时和分钟是一件麻烦事,但是来自Power BI论坛的this显示了它是如何完成的。