如何使Power BI显示时间数据类型的总和?
场景:包含列Phase(文本)和Duration(SQL Server时间数据类型,也在Power BI中像时间一样被识别)的表 . 当我添加条形图,其中Phase为轴,持续时间为值时,我只能选择显示Count和Count Distinct for Duration,无SUM .
添加DurationMeasure后的其他说明:
-
PowerBI中的表TBL1具有列持续时间(数据类型时间) .
-
测量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 ) ) )
)
提前致谢
1 回答
如果将持续时间转换为十进制数,则可以显示总和 . 然后是值
1 = 1 day = 24 hour = 1440 minutes = 86400 seconds
. 所以0.5的值意味着12小时 . 在图表中将其显示为小时和分钟是一件麻烦事,但是来自Power BI论坛的this显示了它是如何完成的 .