Forum Discussion

JohnH-Crown's avatar
JohnH-Crown
Frequent Visitor
4 years ago
Solved

Splitting up events by shift start times in Power Query Editor

I have a report that displays event duration data for a manufacturing facility that runs three shifts around the clock and events can span multiple shifts. The current request I have from my users i...
  • JohnH-Crown's avatar
    JohnH-Crown
    4 years ago

    Thanks for the reply AlexisOlson! I was not able to get your approach to work, but after examining it, it inspired me to my solution!

     

    TLDR; The approach that I found to work was to add a collection column for each 'shift period' and then derive a Start and End column based on the shift period, the Event Start/End, and the Shift start time columns.

    The Details

    First I added the ShiftPeriod column:

     

    ShiftPeriod = {"PreFirstShift", "FirstShift", "SecondShift", "ThirdShift"}

     

    Then I expanded that column so every record is multiplexed with these periods.

    After that, I added a Start custom column with this logic:

     

    Start = if [ShiftPeriod] = "PreFirstShift" then
      if [RecordStart] < [FirstShiftStart] then
        [RecordStart]
      else
        ""
    else if [ShiftPeriod] = "FirstShift" then
      if [RecordStart] < [FirstShiftStart] then
        [FirstShiftStart]
      else if [RecordStart] < [SecondShiftStart] then
        [RecordStart]
      else
        ""
    else if [ShiftPeriod] = "SecondShift" then  
      if [RecordStart] < [SecondShiftStart] then
        [SecondShiftStart]
      else if [RecordStart] < [ThirdShiftStart] then
        [RecordStart]
      else
        ""
    else
      if [RecordStart] < [ThirdShiftStart] then
        [ThirdShiftStart]
      else
        [RecordStart]

     

    Which determines the appropriate start date/time for that record, depending on the shift period that the record is intended for.

    Then I did something similar for an End custom column:

     

    End = if [ShiftPeriod] = "PreFirstShift" then
      if [RecordEnd] < [FirstShiftStart] then
        [RecordEnd]
      else
        [FirstShiftStart]
    else if [ShiftPeriod] = "FirstShift" then
      if [RecordEnd] < [FirstShiftStart] then
        ""
      else if [RecordEnd] < [SecondShiftStart] then
        [RecordEnd]
      else
        [SecondShiftStart]
    else if [ShiftPeriod] = "SecondShift" then
      if [RecordEnd] < [SecondShiftStart] then
        ""
      else if [RecordEnd] < [ThirdShiftStart] then
        [RecordEnd]
      else
        [ThirdShiftStart]
    else
      if [RecordEnd] < [ThirdShiftStart] then
        ""
      else 
        [RecordEnd]

     

    This will leave records that the Event Start/End don't overlap with a null or empty string in either the Start or End columns, which I filtered out, and changed the column types to Date/Times. Then cleaned up the no longer needed columns.