Forum Discussion
power bi time difference
Hi,
I am creating a table that has the following columns (ID, Time, Action) and want to create an additional column for time difference (in mins) between a specific ID and there Action and output the time difference on the 'Out' row. The data is titled "Data" and the table looks like the following:
ID Time Action
1 12:23:18 05/05/2015 In
2 12:26:26 05/05/2015 In
1 14:23:18 05/05/2015 Out
1 15:23:18 05/05/2015 In
1 16:23:18 05/05/2015 Out
2 16:26:26 05/05/2015 Out
. . .
. . .
And I would like the table to look like:
ID Time Action Time Diff
1 12:23:18 05/05/2015 In
2 12:26:26 05/05/2015 In
1 14:23:18 05/05/2015 Out 120
1 15:23:18 05/05/2015 In
1 16:23:18 05/05/2015 Out 60
2 16:26:26 05/05/2015 Out 240
. . .
. . .
Note that there maybe multiple time In and Out for a ID within one day. Any advice would be appriciated.
Thanks
4 Replies
- v-juanli-msftCommunity Support
Hi drad2211
az38's measure works.
Time Diff = VAR LastTimeIn = CALCULATE ( MAX ( Table1[Time] ), FILTER ( ALL ( Table1 ), Table1[Action] = "In" && Table1[ID] = SELECTEDVALUE ( Table1[ID] ) && Table1[Time] < SELECTEDVALUE ( 'Table1'[Time] ) ) ) RETURN IF ( SELECTEDVALUE ( Table1[Action] ) = "Out", DATEDIFF ( LastTimeIn, SELECTEDVALUE ( Table1[Time] ), MINUTE ), "" )Since Measure doesn't stored in the data model and it can't be used as a item in a slicer,
if you want to create a calculated column, you could create a column like this:
Column = VAR last_ = CALCULATE ( MAX ( Table1[Time] ), FILTER ( Table1, Table1[ID] = EARLIER ( Table1[ID] ) && Table1[Action] = "In" && Table1[Time] < EARLIER ( Table1[Time] ) ) ) RETURN IF ( [Action] = "Out", DATEDIFF ( last_, [Time], MINUTE ) )Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.- drad2211Frequent Visitor
Thank you, that worked great! One last question, is there a way to state that if the 'Out' isn't preceeded by a 'In' to skip to the next one for flag it with a X or something? Kind of similar to this:
ID Time Action Time Diff
1 12:23:18 05/05/2015 In
2 12:26:26 05/05/2015 In
1 14:23:18 05/05/2015 Out 120
1 15:15:18 05/05/2015 Out (Either leave blank or show as "X")
1 15:23:18 05/05/2015 In
1 16:23:18 05/05/2015 Out 60
2 16:26:26 05/05/2015 Out 240
. . .
. . .
- az38Community Champion
Hi drad2211
try a measure
Time Diff = VAR LastTimeIn = calculate(max(Table1[Time]);FILTER(ALL('Table1');Table1[Action]="In" && Table1[ID]=SELECTEDVALUE(Table1[ID]) && Table1[Time]<SELECTEDVALUE('Table1'[Time]))) return if(SELECTEDVALUE(Table1[Action]) = "Out";datediff(LastTimeIn;SELECTEDVALUE(Table1[Time]);MINUTE);"")do not hesitate to give a kudo to useful posts and mark solutions as solution
- Ashish_MathurSuper User
Hi,
Try this calculated column formula
=IF(Data[Action]="Out",Data[Time]-CALCULATE(MAX(Data[Time]),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[Time]<EARLIER(Data[Time]))),BLANK())
Hope this helps.