You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power BI中计算Step4与Step2间日期平均时长的问题

解决Power BI中Step4与对应Step2的平均时间差计算问题

一、解决总时长显示异常日期的问题

你看到的30.12.1899 0:09:22是因为Power BI默认将时间差解析为日期时间类型(基于Excel的1899-12-30起始日期),只需格式化输出即可去除异常日期:

TotalTimeDiff = 
FORMAT(
    CALCULATE(SUM(Logging[Datetime]), Logging[Step] = 4) - CALCULATE(SUM(Logging[Datetime]), Logging[Step] = 2),
    "hh:mm:ss"
)

此度量值会直接输出00:09:22,不再附带异常日期。

二、正确计算每组Step4与Step2的平均时间差

由于你的数据是Step2和Step4按顺序成对出现,需要先给每组配对添加标识,再计算差值的平均值:

步骤1:添加分组标识计算列

在Logging表中创建计算列,为每对Step2和对应的Step4分配相同的组ID:

GroupID = 
VAR CurrentDateTime = Logging[Datetime]
RETURN
IF(
    Logging[Step] = 2,
    COUNTROWS(FILTER(Logging, Logging[Step] = 2 && Logging[Datetime] <= CurrentDateTime)),
    COUNTROWS(FILTER(Logging, Logging[Step] = 2 && Logging[Datetime] < CurrentDateTime))
)

该列会为第一对Step2/Step4生成ID 1,第二对生成ID 2,以此类推,确保每组配对共享同一ID。

步骤2:计算平均时间差度量值

创建度量值,基于分组计算每组时间差后取平均,并格式化输出:

AverageTimeDiff = 
VAR GroupTimeDiffs = 
    ADDCOLUMNS(
        VALUES(Logging[GroupID]),
        "@Diff", CALCULATE(MAX(Logging[Datetime]), Logging[Step] = 4) - CALCULATE(MAX(Logging[Datetime]), Logging[Step] = 2)
    )
RETURN
FORMAT(AVERAGEX(GroupTimeDiffs, [@Diff]), "hh:mm:ss")

此度量值会输出你需要的00:02:20结果。

替代方案(无需计算列)

如果不想添加计算列,可直接通过度量值匹配每个Step2之后的第一个Step4:

AverageTimeDiff_NoColumn = 
VAR Step2Times = CALCULATETABLE(VALUES(Logging[Datetime]), Logging[Step] = 2)
VAR Step4Times = CALCULATETABLE(VALUES(Logging[Datetime]), Logging[Step] = 4)
VAR PairedDiffs = 
    GENERATE(
        Step2Times,
        VAR CurrentStep2 = [Datetime]
        VAR NextStep4 = MAXX(FILTER(Step4Times, [Datetime] > CurrentStep2), [Datetime])
        RETURN ROW("@Diff", NextStep4 - CurrentStep2)
    )
RETURN
FORMAT(AVERAGEX(PairedDiffs, [@Diff]), "hh:mm:ss")

内容的提问来源于stack exchange,提问作者bombom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:50:23