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

DAX Measure按项目筛选时无法正确过滤全月分配员工的问题

员工月度分配筛选DAX度量修复问题

数据集结构

DateEmp CodeProject CodeAllocation
7/1/2022 0:001A0
8/1/2022 0:001A0
9/1/2022 0:001A1
10/1/2022 0:001A1
11/1/2022 0:001A0.2
12/1/2022 0:001A0
7/1/2022 0:002B1
8/1/2022 0:002B1
9/1/2022 0:002B1
10/1/2022 0:002B1
11/1/2022 0:002B0.2
12/1/2022 0:002B1

需求说明

报表配置了Date Range Slicer用于选择日期范围,通过Matrix Visual展示员工月度分配情况,需添加过滤规则:仅显示在筛选的所有月份中Allocation>0的员工。

  • 示例1:筛选7-12月时,仅显示员工2(全月分配>0);
  • 示例2:筛选9-12月时,员工1和2均满足条件,全部显示。

当前使用的DAX度量

Employee Allocated All Months Boolean = 
Var _count = 
        CALCULATE(
            DISTINCTCOUNT('Calendar'[Month]), 
            FILTER(
                ALLSELECTED(Competency),
                Competency[Emp Code] = MAX(Competency[Emp Code])
            )
        )
    Var _total = 
        CALCULATE(
            DISTINCTCOUNT('Calendar'[Month]), 
            ALLSELECTED(Competency)
        )
RETURN 
    IF(_count = _total, 1)

问题现象

按Project筛选时出现异常:
当筛选Project A(仅包含员工1的记录),且选择日期范围7-12月时,员工1存在多个月份Allocation=0,按规则应无结果显示,但实际却显示了员工1及其中3个分配>0的月份,不符合预期。

修复方案

修正后的DAX度量

Employee Allocated All Months Boolean = 
VAR SelectedTotalMonths = CALCULATE(DISTINCTCOUNT('Calendar'[Month]), ALLSELECTED('Calendar'))
VAR EmployeeValidMonths = 
    CALCULATE(
        DISTINCTCOUNT('Calendar'[Month]),
        FILTER(
            ALLSELECTED('Competency'),
            'Competency'[Emp Code] = MAX('Competency'[Emp Code])
                && 'Competency'[Allocation] > 0
        )
    )
RETURN
    IF(EmployeeValidMonths = SelectedTotalMonths, 1, 0)

修复说明

原度量的问题在于:仅统计了员工在筛选数据集中存在记录的月份数,未排除Allocation=0的情况。当筛选Project A时,员工1的6个月份均有记录(即使Allocation为0),导致_count等于_total,错误返回1。

修正后的度量:

  1. 先计算筛选范围内的总月份数(不受员工或Project筛选影响);
  2. 再统计该员工在筛选范围内Allocation>0的月份数;
  3. 比较两者是否相等,相等则返回1(符合条件),否则返回0(不符合条件)。

也可以用另一种逻辑(检查是否存在未达标月份)实现:

Employee Allocated All Months Boolean = 
VAR SelectedMonthList = VALUES('Calendar'[Month])
VAR EmployeeMissingValidMonths = 
    COUNTROWS(
        EXCEPT(
            SelectedMonthList,
            CALCULATETABLE(
                VALUES('Calendar'[Month]),
                FILTER(
                    ALLSELECTED('Competency'),
                    'Competency'[Emp Code] = MAX('Competency'[Emp Code])
                        && 'Competency'[Allocation] > 0
                )
            )
        )
    )
RETURN
    IF(EmployeeMissingValidMonths = 0, 1, 0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:41:05