在DAX中将度量值结果转移至另一维度的技术实现问题
解决DAX度量值:将Product A销售额映射到同客户的Product B行
正确度量值代码
Moved Sales := VAR CustomerASales = CALCULATE([Sales], ALL('Product'), 'Product'[Product] = "Product A") RETURN IF(SELECTEDVALUE('Product'[Product]) = "Product B", CustomerASales, BLANK())
逻辑说明
- 计算客户维度下的Product A销售额:
使用ALL('Product')移除当前行的产品筛选上下文,确保CALCULATE仅保留客户维度的筛选,计算对应客户的Product A总销售额,不受当前行显示的产品影响。 - 控制显示范围:
通过IF判断当前行的产品是否为Product B,仅在Product B的行返回计算出的Product A销售额,其他产品行返回空值(BLANK())。
原代码失效原因
你之前尝试的Moved Sales:=CALCULATE([Sales of A], KEEPFILTERS('Product'[Product] = "Product B"))之所以无效,是因为[Sales of A]内部已经筛选了Product'[Product] = "Product A",外部又通过KEEPFILTERS强制筛选Product B,两个筛选条件互斥,导致没有符合条件的数据,最终返回空值。
效果验证
对应你提供的表格,这个度量值会输出如下结果:
| Product | Customer | Sales | Sales of A | Moved Sales |
|---|---|---|---|---|
| Product A | ABC | 10 | 10 | |
| Product A | DEF | 20 | 20 | |
| Product B | ABC | 30 | 10 | 10 |
| Product B | DEF | 40 | 20 | 20 |
| Product C | ABC | 50 | 10 |
内容的提问来源于stack exchange,提问作者Kim
相关产品推荐
相关产品推荐

