Search code examples
timepowerbiformatmeasureweighted

Power BI Avg weighted time format measures


I have created two measures to calculate my weighted ACW avg but the time format is not showing correctly and I am stuck. I have an imported table from Postgres into Powerbi the column I am using for my acw time is in seconds in the Inboundlog table.

To get my average I first created a measure to sum the total ACW time.

SUM ACW = SUM(inboundlog[time_acwork])

I then created a second measure to divide this result by my total handled calls

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

When added to my table it shows the correct results but not in time format. When I added the Format to the AVG ACW meausre for either "HH:MM:SS" or "MM:SS" it throws the results way off. Is there a way I can manipulate to show in time format but with the correct resutls.

Here are the correct results not in time format.

Power BI Results

This is a screenshot of what I am tying out to.

End Result

Screenshot of the data type in the inboundlog table.

Time_acwork


Solution

  • Use code from here: 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 ) ) )
        )