Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
azywoniki
Frequent Visitor

Calculating Durations between Sign In/Out events in a user log

Hi all,

 

I am working with a user log of sign in and sign out events indicating the date/time the user either signed in or out with a status of 1 for "SignIn" and 0 for "SignOut".

 

From this table I would like to calculate a duration for the amount of time each user was signed in.  Essentially, this would be the time between each sequential "SignIn" event and "SignOut" event.  If the final user event is a "SignIn" event, I would calculate the duration up to the current date/time.

 

I would like to accomplish this in Power BI but struggling on how to shape the data appropriately or to be able to create a measure to capture this.

 

Any help would be appreciated!

 

Below is a sample data table:

 

EventDateTimeUserNameUserStatus
8/1/2019 3:09Betty1
8/1/2019 7:40Gary1
8/1/2019 10:39Sue1
8/1/2019 11:12Gary0
8/1/2019 13:00Gary1
8/1/2019 13:09Betty0
8/1/2019 13:34Betty1
8/1/2019 15:05Betty0
8/1/2019 16:48Betty1
8/1/2019 16:57John0
8/1/2019 18:55John1
8/1/2019 19:12John0
8/1/2019 19:59Betty0
8/1/2019 20:38Betty1
8/1/2019 20:45John1
8/1/2019 21:41Gary0
8/1/2019 22:14Sue1
8/1/2019 22:56Gary1
8/1/2019 22:59Gary0
8/1/2019 23:00Betty0
8/1/2019 23:33Sue0
8/2/2019 1:10John0
8/2/2019 1:24John1
8/2/2019 1:48Betty1
8/2/2019 3:03Gary1
8/2/2019 10:36Sue1
8/2/2019 11:29Sue0
8/2/2019 12:13Betty0
8/2/2019 13:09John0
1 ACCEPTED SOLUTION
v-eachen-msft
Community Support
Community Support

Hi  @azywoniki ,

 

At first, you need to create an index to sort data by name by group.

index =
RANKX (
    FILTER ( Table1, EARLIER ( Table1[UserName] ) = Table1[UserName] ),
    Table1[EventDateTime],
    ,
    ASC
)

Then put sign-in time and sign-out time on the same row.

OutTime =
CALCULATE (
    IF (
        ISBLANK ( FIRSTNONBLANK ( 'Table1'[EventDateTime], 1 ) ),
        NOW (),
        FIRSTNONBLANK ( 'Table1'[EventDateTime], 1 )
    ),
    FILTER (
        'Table1',
        Table1[index]
            = EARLIER ( Table1[index] ) + 1
            && Table1[UserName] = EARLIER ( Table1[UserName] )
    )
)

Create a new table to remove meaningless data.

Table2 =
CALCULATETABLE ( Table1, FILTER ( Table1, Table1[UserStatus] = 1 ) )

Create a measure to calculate duration by minute.

Duration(Minute) =
CALCULATE (
    DATEDIFF (
        SELECTEDVALUE ( 'Table2'[EventDateTime] ),
        SELECTEDVALUE ( 'Table2'[OutTime] ),
        MINUTE
    )
)

Here is the document about DATEDIFF() function. You can change interval according to your own needs.

https://docs.microsoft.com/en-us/dax/datediff-function-dax

At last, you can get your visual.2-1.PNG

 

Best Regards,

Eads

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

2 REPLIES 2
v-eachen-msft
Community Support
Community Support

Hi  @azywoniki ,

 

At first, you need to create an index to sort data by name by group.

index =
RANKX (
    FILTER ( Table1, EARLIER ( Table1[UserName] ) = Table1[UserName] ),
    Table1[EventDateTime],
    ,
    ASC
)

Then put sign-in time and sign-out time on the same row.

OutTime =
CALCULATE (
    IF (
        ISBLANK ( FIRSTNONBLANK ( 'Table1'[EventDateTime], 1 ) ),
        NOW (),
        FIRSTNONBLANK ( 'Table1'[EventDateTime], 1 )
    ),
    FILTER (
        'Table1',
        Table1[index]
            = EARLIER ( Table1[index] ) + 1
            && Table1[UserName] = EARLIER ( Table1[UserName] )
    )
)

Create a new table to remove meaningless data.

Table2 =
CALCULATETABLE ( Table1, FILTER ( Table1, Table1[UserStatus] = 1 ) )

Create a measure to calculate duration by minute.

Duration(Minute) =
CALCULATE (
    DATEDIFF (
        SELECTEDVALUE ( 'Table2'[EventDateTime] ),
        SELECTEDVALUE ( 'Table2'[OutTime] ),
        MINUTE
    )
)

Here is the document about DATEDIFF() function. You can change interval according to your own needs.

https://docs.microsoft.com/en-us/dax/datediff-function-dax

At last, you can get your visual.2-1.PNG

 

Best Regards,

Eads

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

Eads,

 

Thank you very much for the thorough explanation of a workable solution.  I was able to use your suggestion to do exactly what I needed.  Apologies for the delayed response, it to me some time to break free and fully understand your solution.

 

Once again thanks for the assistance.

 

Cheers

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.