首页 > 解决方案 > Power BI Avg 加权时间格式度量

问题描述

我创建了两个度量来计算我的加权 ACW 平均值,但时间格式显示不正确,我被卡住了。我有一个从 Postgres 导入 Powerbi 的表,我用于我的 acw 时间的列在 Inboundlog 表中以秒为单位。

为了得到我的平均值,我首先创建了一个度量来计算总 ACW 时间。

SUM ACW = SUM(inboundlog[time_acwork])

然后我创建了第二个度量来将此结果除以我处理的呼叫总数

 AVG ACW = DIVIDE([SUM ACW], [Calls Handled])

当添加到我的表中时,它会显示正确的结果,但不是时间格式。当我将格式添加到“HH:MM:SS”或“MM:SS”的AVG ACW测量时,它会抛出结果。有没有一种方法可以操纵以时间格式显示但结果正确。

以下是非时间格式的正确结果。

Power BI 结果

这是我要绑定的屏幕截图。

最终结果

inboundlog 表中数据类型的屏幕截图。

Time_acwork

标签: timepowerbiformatmeasureweighted

解决方案


使用此处的代码: https ://community.powerbi.com/t5/Community-Blog/Aggregating-Duration-Time/ba-p/22486

Duration = 
// 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 = [Duration in Seconds]
// 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 ) ) )
    )

推荐阅读