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

Power BI中同物料不同生产批次的成本差异计算问题

解决Power BI中同物料生产批次成本差异计算问题

步骤1:为每个物料的生产批次生成排名

首先在**生产表(ProductionTable)**中添加计算列,按物料分组、生产日期降序给批次排名(最新批次为1,上一批为2):

BatchRank = 
RANKX(
    FILTER(ProductionTable, ProductionTable[ItemID] = EARLIER(ProductionTable[ItemID])),
    ProductionTable[prodDate],
    ,
    DESC,
    DENSE
)
  • 逻辑说明:FILTER限定只计算当前物料的批次,EARLIER引用当前行的ItemID,DENSE确保排名连续(避免日期相同导致的跳号)。

步骤2:编写度量值获取上一批次的对应成本

创建度量值,根据当前批次的排名,匹配同物料的上一批次ProdID,进而提取对应成本类型的金额:

PreviousBatchCost = 
VAR CurrentItem = SELECTEDVALUE(ProductionTable[ItemID])
VAR CurrentRank = SELECTEDVALUE(ProductionTable[BatchRank])
-- 获取上一批次的ProdID
VAR PreviousProdID = CALCULATE(
    MAX(ProductionTable[ProdID]),
    FILTER(
        ALL(ProductionTable),
        ProductionTable[ItemID] = CurrentItem && ProductionTable[BatchRank] = CurrentRank + 1
    )
)
-- 返回对应成本类型的上批成本
RETURN 
IF(
    NOT ISBLANK(PreviousProdID),
    CALCULATE(
        SUM(ProductionDetailTable[Costs]),
        ProductionDetailTable[ProdID] = PreviousProdID && 
        ProductionDetailTable[TypeofCost] = SELECTEDVALUE(ProductionDetailTable[TypeofCost])
    ),
    BLANK()
)

步骤3:计算批次间成本差异

创建差异度量值,对比当前批次与上一批次的成本:

CostDifference = 
SUM(ProductionDetailTable[Costs]) - [PreviousBatchCost]

如果需要差异率(用于效率分析更直观):

CostDifferenceRate = 
DIVIDE(
    [CostDifference],
    [PreviousBatchCost],
    0 -- 无数据时返回0
)

可视化配置调整

在你的矩阵可视化中:

  • 行字段:添加ItemID + ProdID(可展开查看物料下的各批次)
  • 列字段:保留TypeofCost
  • 值字段:添加SUM(ProductionDetailTable[Costs])(当前批次成本)、PreviousBatchCost、CostDifference(或CostDifferenceRate)

这样就能在物料筛选器生效的前提下,查看每个物料各批次的成本对比与差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 14:37:37