DAX-使用多个过滤器在线路级别(SUMX)进行计算

时间:2018-09-24 17:24:56

标签: powerbi dax powerpivot powerbi-desktop

我要让DAX做的是:

  1. 浏览HR数据表中的每一行。
  2. 确定员工(“员工a”)的开始日期
  3. 使用以下过滤器对表中的其他员工总数求和:
    一种。成功完成任务
    b。在开始日期+ 31之前结束了他们的作业
    C。在开始日期-31(也就是说,在雇员a的开始日期的一个月内)之后结束了他们的工作
    d。在雇员a之前开始(不计入雇员a或同类中的任何人)
    e。与员工具有相同的职位

这本质上是用普通的英语提问:“对于我的每位员工,有多少个具有相同职称的员工在该员工上任后一个月内成功完成了他们的任务?”在DAX中基本上只是“如何将多个过滤条件应用于SUMX或COUNTAX度量/计算列?”

我已经尝试过的措施是:

Contractors Available = COUNTAX(
'BAT VwRptMspAssignment',
    CALCULATE(
        DISTINCTCOUNT('BAT VwRptMspAssignment'[assignmentgk]),
        FILTER(
            FILTER(
                FILTER(
                    FILTER(
                        FILTER(ALL('BAT VwRptMspAssignment'),
                        'BAT VwRptMspAssignment'[End.Date]<EARLIER('BAT VwRptMspAssignment'[Start.Date])+31),
                    'BAT VwRptMspAssignment'[End.Date]>EARLIER('BAT VwRptMspAssignment'[Start.Date])-31),
                'BAT VwRptMspAssignment'[Start.Date]<EARLIER('BAT VwRptMspAssignment'[Start.Date])),
            'BAT VwRptMspAssignment'[EoaReason]="Successful Completion"),
        'BAT VwRptMspAssignment'[JobPostingTitle.1]=EARLIER('BAT VwRptMspAssignment'[JobPostingTitle.1]))
        )
    )

我尝试的计算列是:

Contractors Available.1 = SUMX(
FILTER(
        FILTER(
            FILTER(
                FILTER(
                    FILTER(
                        FILTER(ALL('BAT VwRptMspAssignment'),
                            'BAT VwRptMspAssignment'[customergk]=EARLIER('BAT VwRptMspAssignment'[customergk])),
                        'BAT VwRptMspAssignment'[JobPostingTitle.1]=EARLIER('BAT VwRptMspAssignment'[JobPostingTitle.1])),
                        'BAT VwRptMspAssignment'[End.Date]<EARLIER('BAT VwRptMspAssignment'[Start.Date])+31),
                    'BAT VwRptMspAssignment'[End.Date]>EARLIER('BAT VwRptMspAssignment'[Start.Date])-31),
                'BAT VwRptMspAssignment'[Start.Date]<EARLIER('BAT VwRptMspAssignment'[Start.Date])),
            'BAT VwRptMspAssignment'[EoaReason]="Successful Completion"),

'BAT VwRptMspAssignment'[FinishFlag])

但这些解决方案均无效。
有谁知道为什么或还有什么我可以尝试实现这一目标?导出到Excel的数据格式示例:

scrubbed data

“可用的承包商2”是计算所得的列。 请注意第一行中的521。如果我在Excel中应用所有这些过滤器,则应为零,此职位在数据集中是唯一的。它说107个“技术作家-专家”行应该在2017年9月26日的一个月内结束,但是这些是数据集中仅有的3位技术作家,其他两个中的零个在9 / 30/2016: enter image description here

1 个答案:

答案 0 :(得分:1)

尝试对计算所得的列执行以下操作:

Contractors Available.1 =
VAR StartDate = 'BAT VwRptMspAssignment'[Start.Date]
VAR JobTitle = 'BAT VwRptMspAssignment'[JobPostingTitle.1]
RETURN
    COUNTROWS (
        FILTER (
            'BAT VwRptMspAssignment',
            'BAT VwRptMspAssignment'[End.Date] < StartDate + 31
                && 'BAT VwRptMspAssignment'[End.Date] > StartDate - 31
                && 'BAT VwRptMspAssignment'[Start.Date] < StartDate
                && 'BAT VwRptMspAssignment'[EoaReason] = "Successful Completion"
                && 'BAT VwRptMspAssignment'[JobPostingTitle.1] = JobTitle
        )
    )

不需要EARLIER函数,因为变量将保留上下文的定义位置,即rowcontext。

编辑: 我使用您提供的数据测试了我的公式,它似乎起作用了。我在第二行中更改了[End.Date],以便在第一行中获得结果。

enter image description here