简体   繁体   English

连续缺勤/工作日 Power BI 度量

[英]Consecutive Absent/Worked Days Power BI Measure

I am current trying to Dynamically Calculate Consecutive worked and absent days for a group of employees, however this as proven to be quite a difficult task.我目前正在尝试动态计算一组员工的连续工作和缺勤天数,但事实证明这是一项非常艰巨的任务。 The RAW table looks something like this: RAW 表如下所示:

Date日期 ID ID Absent缺席的
6/1/2021 2021 年 6 月 1 日 1234 1234 1 1
6/2/2021 6/2/2021 1234 1234 1 1
6/32021 6/32021 1234 1234 1 1
6/4/2021 6/4/2021 1234 1234 0 0
6/1/2021 2021 年 6 月 1 日 6789 6789 1 1
6/2/2021 6/2/2021 6789 6789 0 0

I would like to use Dax (not Power Query) to calculate the consecutive days they were absent, the closest I have come to a solution is the following, Shout out SQLBI :我想使用 Dax(而不是 Power Query)来计算他们缺席的连续天数,我得出的最接近的解决方案如下,大声喊出 SQLBI

DaysWith0 = 
VAR CurrentDate = MAX('Calendar'[Date])
VAR FirstDateEver =  CALCULATE(MIN( 'Calendar'[Date]), REMOVEFILTERS())
VAR PrevWorked =
    CALCULATE(
        MAX('RawDataTable'[Date]),'Calendar'[Date] <= CurrentDate) 
VAR PrevDate = COALESCE(PrevWorked,FirstDateEver)
VAR Result = INT(CurrentDate - PrevDate)  
RETURN
    Result

But this only counts the consecutive days that have no data (no schedule).但这仅计算没有数据(无计划)的连续天数。 I need 2 measures that shows the consecutive worked days, and the consecutive absents.我需要 2 个显示连续工作日和连续缺勤的措施。

This looks like a classic Gaps-and-Islands这看起来像一个经典的间隙和岛屿

Example例子

Select DateR1 = min(Date)
      ,DateR2 = max(Date)
      ,ID
      ,Absent = sum( case when Absent=1 then 1 else 0 end )
      ,Worked = sum( case when Absent=0 then 1 else 0 end )
 From  (
        Select *
              ,Grp = datediff(day,'1900-01-01',Date) - row_number() over (partition by ID,Absent order by Date) 
         From YourTable
       ) A
 Group By ID,Grp
 Order By ID,min(Date)

Results结果

DateR1      DateR2      ID      Absent  Worked
2021-06-01  2021-06-03  1234    3       0
2021-06-04  2021-06-04  1234    0       1
2021-06-01  2021-06-01  6789    1       0
2021-06-02  2021-06-02  6789    0       1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM