daxgroupingpowerpivot

grouping consecutive dates of absence for each employee


I'm working in Excel PowerPivot and I have an absence data set where there is a row for each day of absence per employee.

I want to assign the first day of absence to each row where the previous employee ID is the same AND the previous absence date is consecutive (i.e. is the previous day).

My data set looks like this:

current dataset with column "Absence Start Date" being what i'm hoping to achieve (https://i.sstatic.net/gTJ9w.png)

Any help would be hugely appreciated! :)

I have played around with Earlier etc but i'm just going round in circles!

I want to extract the first day of the absence occurrence (so the first date in the date range for that employee's specific absence) and add it to each row in that range.

Employee ID Time Off Date First Date Occurrence Absence Start Date Time Off Reason
123 01/02/2023 23/01/2023 Hernia
123 02/02/2023 23/01/2023 Hernia
123 01/03/2023 23/01/2023 Hernia
123 02/03/2023 23/01/2023 Hernia

Solution

  • Here is an approach but it involves two calculated columns.

    1st Calculated Column called FirstDateOccurrence:

    =
      var thisId = [Employee ID]
      var thisReason = [Time Off Reason]
      var thisDate = [Time Off Date]
      var prevRow = TOPN(1, FILTER('Table1', [Employee ID] = thisId && [Time Off Reason] = thisReason && [Time Off Date] < thisDate), [Time Off Date], DESC)
      var prevDate = CALCULATE(MAX([Time Off Date]), prevRow)
      // use this one if NETWORKDAYS is available
      // return IF(NETWORKDAYS(prevDate, thisDate) > 2, thisDate)
      return SWITCH(TRUE(),
        ISBLANK(prevDate), thisDate,
        WEEKDAY(prevDate, 2) = 5 && DATEDIFF(prevDate, thisDate, DAY) < 4, BLANK(),
        DATEDIFF(prevDate, thisDate, DAY) <> 1, thisDate
      )
    

    2nd Calculated Column called Occurrence ID:

    =
      var thisId = [Employee ID]
      var thisReason = [Time Off Reason]
      var thisDate = [Time Off Date]
      var prevRow = TOPN(1, FILTER('Table1', [Employee ID] = thisId && [Time Off Reason] = thisReason && [FirstDateOccurrence] <= thisDate), [FirstDateOccurrence], DESC)
      return CALCULATE(MAX([Time Off Date]), prevRow)
    

    As an added bonus, the above uses NETWORKDAYS and so if a date is Friday, and the next is Monday, then it will consider them as siblings.

    Result

    enter image description here