如何使 Power BI 显示时间数据类型列的总和?
场景:包含 Phase (text) 和 Duration 列的表(SQL Server 时间数据类型,也可以在 Power BI 中识别,如时间)。当我添加以相位为轴并以持续时间为值的条形图时,我只能选择显示持续时间的计数和计数不同,没有总和。
添加 DurationMeasure 后的附加说明:
- PowerBI 中的表 TBL1 具有 Duration 列(数据类型时间)。
- Measure DurationMeasure 添加到 TBL1 中,公式如下所示。
- PowerBI 不允许我将此度量添加到条形图中的值属性(见下图)。无法在 Values 属性上拖放。如果我只是检查这个度量,它会像所有其他度量一样转到 Visual 的 Tooltip 属性而不是 Values 属性。也许是因为它是字符串数据,但我不知道如何格式化它。
这是 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 ) ) )
)
提前致谢