DAX Measure按项目筛选时无法正确过滤全月分配员工的问题
员工月度分配筛选DAX度量修复问题
数据集结构
| Date | Emp Code | Project Code | Allocation |
|---|---|---|---|
| 7/1/2022 0:00 | 1 | A | 0 |
| 8/1/2022 0:00 | 1 | A | 0 |
| 9/1/2022 0:00 | 1 | A | 1 |
| 10/1/2022 0:00 | 1 | A | 1 |
| 11/1/2022 0:00 | 1 | A | 0.2 |
| 12/1/2022 0:00 | 1 | A | 0 |
| 7/1/2022 0:00 | 2 | B | 1 |
| 8/1/2022 0:00 | 2 | B | 1 |
| 9/1/2022 0:00 | 2 | B | 1 |
| 10/1/2022 0:00 | 2 | B | 1 |
| 11/1/2022 0:00 | 2 | B | 0.2 |
| 12/1/2022 0:00 | 2 | B | 1 |
需求说明
报表配置了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。
修正后的度量:
- 先计算筛选范围内的总月份数(不受员工或Project筛选影响);
- 再统计该员工在筛选范围内Allocation>0的月份数;
- 比较两者是否相等,相等则返回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
相关产品推荐
相关产品推荐

