Forum Discussion
DATEDIFF between DateTime Values by ID
I have a Table_A, which tracks the DateTime of status changes.
| DateTime | ID | Entry | Exit |
| 29/10/2020 14:25 | 1001 | 1 | 0 |
| 01/12/2020 15:30 | 1002 | 1 | 0 |
| 05/02/2021 12:00 | 1001 | 0 | 1 |
| 15/04/2021 21:45 | 1002 | 0 | 1 |
I want to calculate the amount of time between Entry and Exit for each ID (TimeSpent).
| DateTime | ID | Entry | Exit | TimeSpent |
| 29/10/2020 14:25 | 1001 | 1 | 0 | null |
| 01/12/2020 15:30 | 1002 | 1 | 0 | null |
| 05/02/2021 12:00 | 1001 | 0 | 1 | 99 |
| 15/04/2021 21:45 | 1002 | 0 | 1 | 135 |
Something like this
TimeSpent =
DATEDIFF(
FILTER('TableA'[DateTime], Entry = 1),
FILTER('TableA'[DateTime], Exit = 1),
Day
)
I get the following error message "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value."
I doubt very much that FILTER is the right way to identify cases for the calculation... I've tried CALCULATE but that didn't work either.
Any ideas?
Similar issues:
https://community.powerbi.com/t5/Desktop/DateDiff-with-Filter-based-on-specific-value/m-p/1366481#M584849
https://community.powerbi.com/t5/Desktop/using-datediff-with-filters/m-p/528938#M247760
https://community.powerbi.com/t5/DAX-Commands-and-Tips/DATEDIFF-with-filter/m-p/769728#M3623
Anonymous , Try a measure like
averageX(values(Table[ID]), calculate(datediff(minx(filter(Table, Table[Entry] =1), [DateTime]),maxx(filter(Table, Table[exit] =1), [DateTime]),minute)))
6 Replies
- amitchandakSuper User
Anonymous , Try a measure like
averageX(values(Table[ID]), calculate(datediff(minx(filter(Table, Table[Entry] =1), [DateTime]),maxx(filter(Table, Table[exit] =1), [DateTime]),minute)))
- AnonymousNot applicable
Hi Amitchandak,
Is it possible to filter this measure [TimeSpent] by a Date table 'Date'?
You would need to specify in the Measure that the filter applies to the Exit DateTime. Or possibly ignore the lesser date with something like ...FILTER(ALL('Date'[DateTime]),'Date'[DateTime]<=MAX('Date'[DateTime]))
- Jihwan_KimSuper User
Hi, Anonymous
Please check the below picture and the sample pbix file's link down below.
Time Spent Measure =VAR currentid =MAX ( 'Table'[ID] )VAR entrydatetime =CALCULATE (MAX ( 'Table'[DateTime] ),FILTER ( ALL ( 'Table' ), 'Table'[ID] = currentid && 'Table'[Entry] = 1 ))RETURNIF (ISFILTERED ( 'Table'[ID] ),IF (SELECTEDVALUE ( 'Table'[Exit] ) = 1,DATEDIFF ( entrydatetime, SELECTEDVALUE ( 'Table'[DateTime] ), DAY ),"null"))Hi, My name is Jihwan Kim.
If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.
Linkedin: linkedin.com/in/jihwankim1975/
Twitter: twitter.com/Jihwan_JHKIM
- AnonymousNot applicable
Thanks!
downloaded the pbix. and your solution works for the sample data. However, it returned null values for my actual dataset.
must be the if()? could be that my IDs are text not numerical?- Jihwan_KimSuper User
Hi, Anonymous
Thank you for your feedback.
in my sample, ID is a number format.
If your ID column is a text format, please try writing something like below.
"1" "2" ...
Thank you.
Hi, My name is Jihwan Kim.
If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.
Linkedin: linkedin.com/in/jihwankim1975/
Twitter: twitter.com/Jihwan_JHKIM
- AnonymousNot applicable
Thank you both for getting back to me. I'll try out the solutions and get back to you 🙂