Forum Discussion
finn881988
4 years agoFrequent Visitor
[Solved] How to calculate time difference from one column based on various conditions
Goal:
Use dax to calculate average employee online time duration (Logout Timestamp - Login Timestamp)
Data:
| Employee No | Status | Timestamp |
| 1 | Login | 2020-1-1 12:00:00 |
| 1 | Inactive | 2020-1-1 13:00:00 |
| 1 | Active | 2020-1-1 14:00:00 |
| 1 | Logout | 2020-1-1 15:03:00 |
| 2 | .. | .. |
| 2 | .. | .. |
Hi,
Please check the below picture and the attached pbix file.
It is for creating a measure, and it shows by minute.
Online time duration avg: = VAR _duration = ADDCOLUMNS ( VALUES ( Data[Employee No] ), "@duration", DATEDIFF ( CALCULATE ( MAXX ( FILTER ( Data, Data[Status] = "Login" ), Data[Timestamp] ) ), CALCULATE ( MAXX ( FILTER ( Data, Data[Status] = "Logout" ), Data[Timestamp] ) ), MINUTE ) ) RETURN AVERAGEX ( _duration, [@duration] )
2 Replies
- Jihwan_Kim
Super User
Hi,
Please check the below picture and the attached pbix file.
It is for creating a measure, and it shows by minute.
Online time duration avg: = VAR _duration = ADDCOLUMNS ( VALUES ( Data[Employee No] ), "@duration", DATEDIFF ( CALCULATE ( MAXX ( FILTER ( Data, Data[Status] = "Login" ), Data[Timestamp] ) ), CALCULATE ( MAXX ( FILTER ( Data, Data[Status] = "Logout" ), Data[Timestamp] ) ), MINUTE ) ) RETURN AVERAGEX ( _duration, [@duration] )- finn881988Frequent Visitor
Thanks Jihwan, it works perfect!