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

DAX问题:Power Pivot中各科目按Sales计算占比的度量值实现

问题:Power Pivot中实现各科目以Sales为基数计算占比

数据结构

Table 1

YearMonthBranch_IDStore_IDArticleValue
20221011Sales100
20221012Sales200
20221012Operating expenses50
20221011Operating expenses80
20221012Cost of Sales20
20221011Cost of Sales30

Table 2

YearMonthBranch_IDStore_IDArticleValue
20221011Sales_Ecomm20
20221012Sales_Ecomm15

Table 3(维度表)

Article
Sales
Operating expenses
Cost of Sales
Sales_Ecomm

需求

创建数据透视表,将所有科目值转换为相对于Sales的百分比,目标结构如下:

Store IDSalesOperating expensesCost of SalesSales_Ecomm
Value% of salesValue% of salesValue% of salesValue% of sales
1100100.00%8080.00%3030.00%2020.00%
2200100.00%5025.00%2010.00%157.50%

现有度量值及问题

已实现科目绝对值计算:

Val. := SUM(Table1[Value]) + SUM(Table2[Value])

尝试的百分比度量值仅能正确计算Sales自身占比,其他科目返回#NUM!错误:

%_of_Sales := [Val.] / CALCULATE([Val.], FILTER(Table3; Table3[Article]="Sales"))

解决方案

问题出在FILTER(Table3; Table3[Article]="Sales")会保留当前透视表中Article字段的筛选上下文,导致计算其他科目时,筛选条件同时要求当前科目和Sales,结果为空,因此出现#NUM!错误。

需要清除Article字段的筛选,同时保留Year、Month、Branch_ID、Store_ID等其他维度的上下文,使用REMOVEFILTERS(或旧版本的ALLSELECTED)来实现:

正确的DAX度量值

%_of_Sales := 
DIVIDE(
    [Val.],
    CALCULATE(
        [Val.],
        REMOVEFILTERS(Table3[Article]),
        Table3[Article] = "Sales"
    ),
    0
)

说明

  1. REMOVEFILTERS(Table3[Article]):清除当前Article字段的筛选,确保能获取到当前上下文下(Year、Month、Branch_ID、Store_ID)的Sales值。
  2. Table3[Article] = "Sales":重新筛选出Sales科目。
  3. DIVIDE函数:替代直接除法,避免除数为0时出现错误,第三个参数指定除数为0时返回0。

如果使用的是旧版本Power Pivot(不支持REMOVEFILTERS),可以用ALLSELECTED(Table3[Article])替代:

%_of_Sales := 
DIVIDE(
    [Val.],
    CALCULATE(
        [Val.],
        ALLSELECTED(Table3[Article]),
        Table3[Article] = "Sales"
    ),
    0
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:40:47