Forum Discussion
Time difference between date time values in same column based on a value in a different column
Hello,
I am trying to aggregate the time a user has spent in the Receipt function scanning items in the warehouse. How would I get the output in the following table (elapsed seconds):
| Assignment 1 | User | Date Time Start | Function | Elapsed Seconds |
| 1 | JF | 9/30/19 8:16 AM | RECEIPT | 0 |
| 1 | JF | 9/30/19 8:17 AM | RECEIPT | 60 |
| 2 | MK | 10/1/19 4:04 PM | RECEIPT | 0 |
| 2 | MK | 10/1/19 4:10 PM | RECEIPT | 0 |
| 2 | MK | 10/1/19 4:11 PM | RECEIPT | 420 |
Essentially, I need to aggregate the elapsed seconds by User for the Receipt function. I know I need to use the DATEDIFF between the minimal date/time and max date time for the user that is completing the receipt function, but I am not sure how to have 0 for all values except for the last one (the aggregated value). I guess I could have the number of seconds between each date since power bi will aggregate it anyway. So, to have:
| Assignment 1 | User | Date Time Start | Function | Elapsed Seconds |
| 1 | JF | 9/30/19 8:16 AM | RECEIPT | 0 |
| 1 | JF | 9/30/19 8:17 AM | RECEIPT | 60 |
| 2 | MK | 10/1/19 4:04 PM | RECEIPT | 0 |
| 2 | MK | 10/1/19 4:10 PM | RECEIPT | 360 |
| 2 | MK | 10/1/19 4:11 PM | RECEIPT | 60 |
The receipt function is not the only function in the dataset, so I would need to filter to that function and aggregate the time by user.
I appreciate the help!
Hi Anonymous ,
Here is the pbix PBIX
I put both columns in so that you have your choice, the second column only shows in the last row of the user. The difference between the measure and the column at the high level, is that with a measure, the engine does not have row context, whereas in the table it does. (It knows which row it is operating on.) Therefore it was just a matter of dropping MAX().
Let me know if you have any questions.
If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel
11 Replies
- Nathaniel_CCommunity Champion
Hi Anonymous
What is the difference between the two tables?
Nathaniel- Nathaniel_CCommunity Champion
Hi Anonymous ,
If I understand correctly this is what you are looking as an output?
Let me know if you have any questions.
If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniellapse Secs Calc = VAR _curTime = MAX ( TimeElapse[Date Time Start] ) VAR _pasttime = CALCULATE ( MAX ( TimeElapse[Date Time Start] ), TimeElapse[Date Time Start] < _curTime, ALLEXCEPT ( TimeElapse, TimeElapse[User], TimeElapse[Function] ) ) VAR _dif = DATEDIFF ( _pasttime, _curTime, SECOND ) RETURN _dif
- Nathaniel_CCommunity Champion
Hi Anonymous
Or with zeros.
Let me know if you have any questions.
If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
NathanielElapse Secs Calc = VAR _curTime = MAX ( TimeElapse[Date Time Start] ) VAR _pasttime = CALCULATE ( MAX ( TimeElapse[Date Time Start] ), TimeElapse[Date Time Start] < _curTime, ALLEXCEPT ( TimeElapse, TimeElapse[User], TimeElapse[Function] ) ) VAR _dif = IF(ISBLANK(_pasttime), 0 ,DATEDIFF ( _pasttime, _curTime, SECOND )) RETURN _dif