Calculate time difference by group?

cpaching

New Member
Joined
Sep 19, 2018
Messages
6
I have an Excel question, in the example below I have a very long list of dates times where I added a column to calculate gaps, and tagged rows where the gap were less than 15 minutes to try to determine a possible consecutive activity. Do you have any idea how to get the groups of tagged rows to calculate a time like latest minus earliest for each tagged group?

[TABLE="width: 734"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]VisitCount[/TD]
[TD]LastAccessed[/TD]
[TD]Last Access Date[/TD]
[TD]Last Access Time[/TD]
[TD]Gap (hms)[/TD]
[TD]Tag (+) if gap less than 15 min[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]5/29/18 8:26 AM[/TD]
[TD]05/29/18[/TD]
[TD]8:26:58 AM[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:07 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:07:12 PM[/TD]
[TD]3:40:14[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:10 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:10:35 PM[/TD]
[TD]0:03:23[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:14 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:14:46 PM[/TD]
[TD]0:04:11[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:15 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:15:11 PM[/TD]
[TD]0:00:25[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:20 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:20:28 PM[/TD]
[TD]0:05:17[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:22 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:22:51 PM[/TD]
[TD]0:02:23[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:23 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:23:10 PM[/TD]
[TD]0:00:19[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:25 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:25:33 PM[/TD]
[TD]0:02:23[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:26 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:26:13 PM[/TD]
[TD]0:00:40[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 12:31 PM[/TD]
[TD]05/29/18[/TD]
[TD]12:31:48 PM[/TD]
[TD]0:05:35[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 4:02 PM[/TD]
[TD]05/29/18[/TD]
[TD]4:02:35 PM[/TD]
[TD]3:30:47[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 4:02 PM[/TD]
[TD]05/29/18[/TD]
[TD]4:02:35 PM[/TD]
[TD]0:00:00[/TD]
[TD]+[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/18 4:02 PM[/TD]
[TD]05/29/18[/TD]
[TD]4:02:35 PM[/TD]
[TD]0:00:00[/TD]
[TD]+[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Welcome to the forum.

Try:


ABCDEFGH
VisitCountLastAccessedLast Access DateLast Access TimeGap (hms)Tag (+) if gap less than 15 minGrouped total
+
+
0:00:25+
+
0:02:23+
+
+
+
+
5/29/2018 16:02
+
+

<tbody>
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]5/29/2018 8:26[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]8:26:58 AM[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:07[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:07:12 PM[/TD]
[TD="align: right"]3:40:14[/TD]
[TD="align: right"][/TD]

[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:10[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:10:35 PM[/TD]
[TD="align: right"]0:03:23[/TD]

[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:14[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:14:46 PM[/TD]
[TD="align: right"]0:04:11[/TD]

[TD="align: right"][/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:15[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:15:11 PM[/TD]

[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:20[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:20:28 PM[/TD]
[TD="align: right"]0:05:17[/TD]

[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:22[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:22:51 PM[/TD]

[TD="align: right"][/TD]

[TD="align: center"]9[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:23[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:23:10 PM[/TD]
[TD="align: right"]0:00:19[/TD]

[TD="align: right"][/TD]

[TD="align: center"]10[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:25[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:25:33 PM[/TD]
[TD="align: right"]0:02:23[/TD]

[TD="align: right"][/TD]

[TD="align: center"]11[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:26[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:26:13 PM[/TD]
[TD="align: right"]0:00:40[/TD]

[TD="align: right"][/TD]

[TD="align: center"]12[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 12:31[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]12:31:48 PM[/TD]
[TD="align: right"]0:05:35[/TD]

[TD="align: right"]0:24:36[/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]
[TD="align: right"]1[/TD]

[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]4:02:35 PM[/TD]
[TD="align: right"]3:30:47[/TD]
[TD="align: right"][/TD]

[TD="align: right"][/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 16:02[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]4:02:35 PM[/TD]
[TD="align: right"]0:00:00[/TD]

[TD="align: right"][/TD]

[TD="align: center"]15[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5/29/2018 16:02[/TD]
[TD="align: right"]5/29/2018[/TD]
[TD="align: right"]4:02:35 PM[/TD]
[TD="align: right"]0:00:00[/TD]

[TD="align: right"][/TD]

</tbody>
Sheet9

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH]G3[/TH]
[TD="align: left"]=IF(E3> TIMEVALUE("00:15:00"),"",IF(E4< TIMEVALUE("00:15:00"),"",SUM(INDEX($E$2:$E2,LOOKUP(2,1/($E$2:$E2> TIMEVALUE("00:15:00")),ROW($E$2:$E2)-ROW($E$2)+2)):E3)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



I ended up not used the F column, I used the TIMEVALUE function to look at the E column directly, but that's easy to change if you want.
 
Last edited:
Upvote 0
Ok Eric, that is magic! I don't understand it yet but it worked :) Thank you very much for your expertise and the quick response.
 
Upvote 0
Sure.

=IF(E3> TIMEVALUE("00:15:00"),"",IF(E4< TIMEVALUE("00:15:00"),"",SUM(INDEX($E$2:$E2,LOOKUP(2,1/($E$2:$E2> TIMEVALUE("00:15:00")),ROW($E$2:$E2)-ROW($E$2)+2)):E3)))

The part in red just says if the value on the current row is over 15 minutes, then just show an empty cell. Otherwise, it must be less than 15 minutes, so we want to include it in a group range. But we only want to put the group sum on the last row of the range, so if the next row is less than 15, it must also be part of the group, so we show an empty cell too (the green part).

So if we get to the last part of the formula, we know we're on the last row of a group. We want to sum up the whole group. In the example, this happens on row 12. What we want to do is sum up E4:E12, like this SUM(E4:E12). The E12 cell is easy, it's the current row (in blue). To find the E4, we have to find the start of the group, which would be the row right after a value > 15. And if you have a long range, it has to be the one just before the current row, not one from farther up in the range.

This construct:

LOOKUP(2,1/($E$2:$E2> TIMEVALUE("00:15:00")),ROW($E$2:$E2)-ROW($E$2)+2)

is used to find the last value > 15 before the current row. Notice how the range is defined as $E$2:$E2. So as you drag the formula down the column the top row stays the same, but the bottom row will always be the row just above the current row. On row 12 it's $E$2:$E11. The part in red checks each cell in that range, and returns an array of TRUE/FALSE values, like this {FALSE,TRUE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE}. Since TRUE = 1 and FALSE = 0, taking the reciprocal (blue) changes that array to {#DIV/0!,1,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!}. Now LOOKUP looks for a 2, which it will never find, since the only possible values are 1 and #DIV/0!. So a quirk of LOOKUP is that it will return the position of the last 1 in that array, or 2. Now if you take ROW($E$2:$E11)-ROW($E$2)+1, you get {1,2,3,4,5,6,7,8,9,10}, which is the offsets into the range. Note that I actually add 2 instead of 1, so this gives us the offset after the row with the value > 15, or 3. Finally, we give the 3 to INDEX, or INDEX($E$2:$E11,3), which resolves to E4, which is what we were looking for.

Whew! There are some tricky concepts there that you won't find in a basic formula primer, but they're not too hard once you look at them. Walk through the formula with the Evaluate Formula tool on the Formulas tab, that'll help too.

Good luck!
 
Upvote 0
Hi Eric, I hate to bother you again but I have one more question - how can I customize this formula to subtract the last time in the tagged group from the first time in the tagged group to get elapsed time for the group instead of a sum of the the gap minutes?
 
Upvote 0
In G3:

=IF(E3>TIMEVALUE("00:15:00"),"",IF(E4< TIMEVALUE("00:15:00"),"",D3-INDEX($D$2:$D2,LOOKUP(2,1/($E$2:$E2>TIMEVALUE("00:15:00")),ROW($E$2:$E2)-ROW($E$2)+2))))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top