Forum Discussion

tonmcg's avatar
tonmcg
Icon for Resolver II rankResolver II
6 years ago
Solved

Find n Number of Events Between Two Events

I have the following table, FactEvent. It details all the events an employee logs every day. I need to categorize each event as a work event, either TRUE or FALSE. A work event is any event that occurs between CLOCK IN and CLOCK OUT events, inlcusive of a CLOCK OUT event, except if these events occur between CLOCK IN and CLOCK OUT events for special TimeOff periods such as an Unpaid Lunch.

 

In the example below, I've incorrectly categorized the two events between a CLOCK IN and CLOCK OUT period as TRUE, the Badge to Breakroom event at 10:24 AM and the Badge to Arcade event at 10:34 AM, defined in the isWorkEvent column. I need these two events to be FALSE, as shown in the isWorkEvent2 column.

 

IndexDateIDDaily Clock In IndexDaily Clock Out IndexEmployeeDateTimeEventTimeOffisWorkEventisWorkEvent2
1120200110111John Smith1/10/2020 15:45CLOCK OUT TRUETRUE
1020200110111John Smith1/10/2020 15:30Last Task End TRUETRUE
920200110111John Smith1/10/2020 10:37CLOCK IN FALSEFALSE
820200110111John Smith1/10/2020 10:37CLOCK OUTUnpaid LunchFALSEFALSE
720200110111John Smith1/10/2020 10:34Badge to Arcade TRUEFALSE
620200110111John Smith1/10/2020 10:24Badge to Breakroom TRUEFALSE
520200110111John Smith1/10/2020 10:13CLOCK INUnpaid LunchFALSEFALSE
420200110111John Smith1/10/2020 10:13CLOCK OUT TRUETRUE
320200110111John Smith1/10/2020 10:04Badge to Breakroom TRUETRUE
220200110111John Smith1/10/2020 10:02First Task End TRUETRUE
120200110111John Smith1/10/2020 6:00CLOCK IN FALSEFALSE

 

Here are how I currently calculate isWorkEvent:

isWorkEvent = FactEvent[Index] >= FactEvent[Daily Clock In Index] && FactEvent[Index] <= FactEvent[Daily Clock Out Index] && ISBLANK( FactEvent[TimeOffID] ) && FactEvent[Event] <> "CLOCK IN"

 

How do I adjust isWorkEvent so that the two events between the 10:13 AM CLOCK IN and 10:37 AM CLOCK OUT events are FALSE?

  • Hi tonmcg ,

     

    You could refer to the following DAX:

    isWorkEvent =
    VAR a =
        CALCULATE (
            MAX ( FactEvent[Index] ),
            FILTER ( FactEvent, FactEvent[TimeOff] = "Unpaid Lunch" )
        )
    VAR b =
        CALCULATE (
            MIN ( FactEvent[Index] ),
            FILTER ( FactEvent, FactEvent[TimeOff] = "Unpaid Lunch" )
        )
    RETURN
        ( FactEvent[Index] >= FactEvent[Daily Clock In Index] )
        && ( FactEvent[Index] <= FactEvent[Daily Clock Out Index] )
        && ( FactEvent[TimeOff] = BLANK () )
        && ( FactEvent[Event] <> "CLOCK IN" )
        && OR ( FactEvent[Index] > a, FactEvent[Index] < b )

    Here is the result.

     

2 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Seems like you would just check the TimeOff value of the MAX CLOCK OUT event < current date/time and make sure the date/time is less than the next CLOCK IN even. If the TimeOff of the MAX CLOCK OUT event < current data/time of row is Unpaid lunch and there is no CLOCK IN event between that time and the current date/time. FALSE. 

     

    See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586

  • v-eachen-msft's avatar
    v-eachen-msft
    Icon for Community Support rankCommunity Support

    Hi tonmcg ,

     

    You could refer to the following DAX:

    isWorkEvent =
    VAR a =
        CALCULATE (
            MAX ( FactEvent[Index] ),
            FILTER ( FactEvent, FactEvent[TimeOff] = "Unpaid Lunch" )
        )
    VAR b =
        CALCULATE (
            MIN ( FactEvent[Index] ),
            FILTER ( FactEvent, FactEvent[TimeOff] = "Unpaid Lunch" )
        )
    RETURN
        ( FactEvent[Index] >= FactEvent[Daily Clock In Index] )
        && ( FactEvent[Index] <= FactEvent[Daily Clock Out Index] )
        && ( FactEvent[TimeOff] = BLANK () )
        && ( FactEvent[Event] <> "CLOCK IN" )
        && OR ( FactEvent[Index] > a, FactEvent[Index] < b )

    Here is the result.