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

基于行条件筛选表格:求优先取Type=B值的DAX度量值

需求:创建DAX度量值实现月份内Type优先级取值

问题说明

需要创建一个DAX度量值,按「Type」字段筛选数据:当同一月份同时存在Type=A和Type=B时,优先取Type=B的QTY值;若仅存在其中一种Type,则取对应Type的QTY值。

原始数据

M   Type    QTY
Jan A   1
Feb A   2
Mar A   3
Apr A   4
Apr B   5
May A   6
May B   7
Jun A   8
Jun B   9

预期结果

Forecast(Jan)=1
Forecast(Feb)=2
Forecast(Mar)=3
Forecast(Apr)=5
Forecast(May)=7
Forecast(Jun)=9

DAX度量值方案

方案一:条件判断法

Forecast = 
VAR CurrentMonth = SELECTEDVALUE('Table'[M])
VAR HasTypeB = CALCULATE(COUNTROWS('Table'), 'Table'[M] = CurrentMonth, 'Table'[Type] = "B") > 0
RETURN
IF(
    HasTypeB,
    CALCULATE(SUM('Table'[QTY]), 'Table'[M] = CurrentMonth, 'Table'[Type] = "B"),
    CALCULATE(SUM('Table'[QTY]), 'Table'[M] = CurrentMonth, 'Table'[Type] = "A")
)
  • CurrentMonth:获取当前上下文对应的月份
  • HasTypeB:检查当前月份是否存在Type=B的记录
  • 通过IF逻辑实现优先级:存在B则取B的QTY,否则取A的QTY

方案二:优先级排序法(更简洁)

Forecast = 
CALCULATE(
    SUM('Table'[QTY]),
    TOPN(1, ALL('Table'[Type]), IF('Table'[Type] = "B", 1, 0), DESC),
    ALLEXCEPT('Table', 'Table'[M])
)
  • 通过TOPN(1,...)筛选出当前月份优先级最高的Type(给B标记优先级1,A标记0,降序取最高)
  • ALLEXCEPT('Table', 'Table'[M])保留当前月份的上下文,确保计算范围是当前月

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:35:04