以hh:mm:ss显示时差

问题描述 投票:0回答:1

美好的一天 在Power BI中,我想计算时间差并以hh:mm:ss显示。我有一个名为“New”的字段和一个名为“At_Incident”的字段,我希望将其差异显示为 hh:mm:ss。截图如下。 sample image

我已经分别计算了H、M、S中的列。 TimeTaskDiff_H 的公式为:

TimeTaskDiff_H = DATEDIFF(unit_task_timestamps[NEW], unit_task_timestamps[AT_INCIDENT], HOUR)

M 和 S 相同。

要将列显示为 hh:mm:ss,我使用了以下公式:

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 = [TimeTaskDiff_S]
// 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 ) ) )
    )

但是我收到以下错误: 无法将文本类型的值“::”转换为日期类型。

如有任何帮助,我们将不胜感激。我应该提到我是 Power BI 和 DAX 的新手。

我尝试过以下代码:

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 = [TimeTaskDiff_S]
// 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 ) ) )
    )
dax
1个回答
0
投票

在我看来,你把一个相当简单的问题过于复杂化了,像这样的事情怎么样:

DurationFormatted = 
    VAR Duration = [Seconds]
    VAR Hours = INT(Duration / 3600)
    VAR Minutes = INT(MOD(Duration, 3600) / 60)
    VAR Seconds = MOD(Duration, 60)
    VAR H = IF(LEN(Hours) = 1, "0" & Hours, Hours)
    VAR M = IF(LEN(Minutes) = 1, "0" & Minutes, Minutes)
    VAR S = IF(LEN(Seconds) = 1, "0" & Seconds, Seconds)
    RETURN H & ":" & M & ":" & S
© www.soinside.com 2019 - 2024. All rights reserved.