Elapsed Time calculation

itzashish

New Member
Joined
Aug 22, 2017
Messages
3
Hi There,

Any quick fix, please?
I need the total open time of the SRs excluding any overlapping hours. In the below example, the total time is 11:42 hrs. Bt in the actual data dump, there would be even more number of rows for the same SR with no sorting.

[TABLE="width: 600"]
<colgroup><col span="3"></colgroup><tbody>[TR]
[TD]SR #[/TD]
[TD]Start Date Time[/TD]
[TD]End Date Time[/TD]
[/TR]
[TR]
[TD]TSHJKL4356[/TD]
[TD="align: right"]6/1/2017 7:10[/TD]
[TD="align: right"]6/1/2017 11:03[/TD]
[/TR]
[TR]
[TD]TSHJKL4357[/TD]
[TD="align: right"]6/1/2017 13:15[/TD]
[TD="align: right"]6/1/2017 17:03[/TD]
[/TR]
[TR]
[TD]TSHJKL4358[/TD]
[TD="align: right"]6/1/2017 15:08[/TD]
[TD="align: right"]6/1/2017 20:22[/TD]
[/TR]
[TR]
[TD]TSHJKL4359[/TD]
[TD="align: right"]6/1/2017 16:08[/TD]
[TD="align: right"]6/1/2017 17:10[/TD]
[/TR]
[TR]
[TD]TSHJKL4360[/TD]
[TD="align: right"]6/1/2017 6:35[/TD]
[TD="align: right"]6/1/2017 9:24[/TD]
[/TR]
</tbody>[/TABLE]


Please help with a formula or code.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Please explain exactly how you arrived at the answer of 11:42 hours.

I think I can see how, but I don't want to guess wrong.

This might be achievable, if you can resort your data, and use helper columns.
Are these options for you ?
 
Upvote 0
Did you mean 11:35, rather than 11:42? If so, this will do it:

Code:
=SMALL($C$2:$C$6,1)-SMALL($B$2:$B$6,1)+SUMPRODUCT(SMALL($C$2:$C$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))-((SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))>SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1))))*SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1))))-((SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))<=SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1))))*SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1)))))
 
Last edited:
Upvote 0
Okay, first think of the two time columns sorted (seperately).

The calc starts with subtracting the earliest starting time from the earliest finishing time. After that it's a case of working through the lists with the following logic: If starting time 2 is greater than finishing time 1, then add the time between starting time 2 and finishing time 2, otherwise add the time between finishing time 1 and finishing time 2.

I hope that makes sense.

By the way, this is a very slight improvement:


Code:
=MIN($C$2:$C$6)-MIN($B$2:$B$6)+SUMPRODUCT(SMALL($C$2:$C$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))-((SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))>SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1))))*SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1))))-((SMALL($B$2:$B$6,ROW(INDIRECT("2:"&COUNT($C$2:$C$6),1)))<=SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1))))*SMALL($C$2:$C$6,ROW(INDIRECT("1:"&COUNT($C$2:$C$6)-1,1)))))
 
Upvote 0
Maybe...


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][td="bgcolor: #DCE6F1"]
F
[/td][td="bgcolor: #DCE6F1"]
G
[/td][td="bgcolor: #DCE6F1"]
H
[/td][td="bgcolor: #DCE6F1"]
I
[/td][td="bgcolor: #DCE6F1"]
J
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
SR #​
[/td][td]
Start Date Time​
[/td][td]
End Date Time​
[/td][td][/td][td]
Minutes​
[/td][td]
Minutes​
[/td][td][/td][td]
Result (Minutes)​
[/td][td]
Hours​
[/td][td]
Minutes​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
TSHJKL4356​
[/td][td]
06/01/2017 07:10​
[/td][td]
06/01/2017 11:03​
[/td][td][/td][td]
430​
[/td][td]
663​
[/td][td][/td][td]
695​
[/td][td]
11​
[/td][td]
35​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
TSHJKL4357​
[/td][td]
06/01/2017 13:15​
[/td][td]
06/01/2017 17:03​
[/td][td][/td][td]
795​
[/td][td]
1023​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
TSHJKL4358​
[/td][td]
06/01/2017 15:08​
[/td][td]
06/01/2017 20:22​
[/td][td][/td][td]
908​
[/td][td]
1222​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
TSHJKL4359​
[/td][td]
06/01/2017 16:08​
[/td][td]
06/01/2017 19:10​
[/td][td][/td][td]
968​
[/td][td]
1150​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
TSHJKL4360​
[/td][td]
06/01/2017 06:35​
[/td][td]
06/01/2017 09:24​
[/td][td][/td][td]
395​
[/td][td]
564​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


Formula in E2 copied across to F2 and down
=60*HOUR(B2)+MINUTE(B2)

Formula in H2
=SUMPRODUCT(--(COUNTIFS(E2:E6,"<="&ROW(INDIRECT(MIN(E2:F6)&":"&MAX(E2:F6))),F2:F6,">"&ROW(INDIRECT(MIN(E2:F6)&":"&MAX(E2:F6))))>0))

Formula in I2
=INT(H2/60)

Formula in J2
=MOD(H2,60)

Hope this helps

M.
 
Upvote 0
Hello Marcelo,

That works for me - I looked at doing this a similar way, a method that examines every minute to ascertain whether it falls in any of the date ranges.

This version does that too, but doesn't require any helper cells and will work over multiple days

=SUMPRODUCT(0+(COUNTIFS(B2:B6,"<"&ROW(INDIRECT("1:"&ROUND(1440*(MAX(C2:C6)-MIN(B2:B6)),0)))/1440+MIN(B2:B6)-1/2880,C2:C6,">"&ROW(INDIRECT("1:"&ROUND(1440*(MAX(C2:C6)-MIN(B2:B6)),0)))/1440+MIN(B2:B6)-1/2880)>0))/1440

It has several disadvantages :sad:

1) It's limited by the number of rows in the worksheet, so on current worksheet length that limits the data period to (2^20)/1440 days which is approximately 2 years
2) If you use a period that long, or close to it, then the formula is very slow
3) Like your version it only handles whole minutes

For those reasons I recommend stunnrock's solution which works very well and doesn't have those disadvantages (although I'd love to make it shorter :) )
 
Upvote 0
Hi Barry,
I did not look with due care the formula suggested by stunrock, because I found it a bit confusing, but I'm going to look at it more closely and try to understand it.

I tried several different formulas and I was very close to getting something that worked - maybe you can help me to find out a simpler formula.

For example, this worked for me

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
SR #​
[/TD]
[TD]
Start Date Time​
[/TD]
[TD]
End Date Time​
[/TD]
[TD][/TD]
[TD]
Hours​
[/TD]
[TD]
Minutes​
[/TD]
[TD][/TD]
[TD]
Helper​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
TSHJKL4356​
[/TD]
[TD]
06/01/2017 07:10​
[/TD]
[TD]
06/01/2017 11:03​
[/TD]
[TD][/TD]
[TD]
11​
[/TD]
[TD]
35​
[/TD]
[TD][/TD]
[TD]
0,093056​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
TSHJKL4357​
[/TD]
[TD]
06/01/2017 13:15​
[/TD]
[TD]
06/01/2017 17:03​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0,079861​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
TSHJKL4358​
[/TD]
[TD]
06/01/2017 15:08​
[/TD]
[TD]
06/01/2017 20:22​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0,126389​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
TSHJKL4359​
[/TD]
[TD]
06/01/2017 16:08​
[/TD]
[TD]
06/01/2017 19:10​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
TSHJKL4360​
[/TD]
[TD]
06/01/2017 06:35​
[/TD]
[TD]
06/01/2017 09:24​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0​
[/TD]
[/TR]
</tbody>[/TABLE]


Helper
Array formula in H2
=IFERROR(INDEX(IF(ROW(A$2:A$6)>ROW(A2),IF(1-((B$2:B$6>C2)+(B2>C$2:C$6)),IF(C2>C$2:C$6,C$2:C$6,C2)-IF(B2>B$2:B$6,B2,B$2:B$6))),MATCH(TRUE,ISNUMBER(IF(ROW(A$2:A$6)>ROW(A2),IF(1-((B$2:B$6>C2)+(B2>C$2:C$6)),IF(C2>C$2:C$6,C$2:C$6,C2)-IF(B2>B$2:B$6,B2,B$2:B$6)))),0)),0)

Formula in E2
=HOUR(SUMPRODUCT(C2:C6-B2:B6)-SUM(H2:H6))

Formula in F2
=MINUTE(SUMPRODUCT(C2:C6-B2:B6)-SUM(H2:H6))

--> The idea is to get (discount) only the first overlap

But failed miserably in this scenario

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
SR #​
[/TD]
[TD]
Start Date Time​
[/TD]
[TD]
End Date Time​
[/TD]
[TD][/TD]
[TD]
Hours​
[/TD]
[TD]
Minutes​
[/TD]
[TD][/TD]
[TD]
Helper​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
TSHJKL4356​
[/TD]
[TD]
06/01/2017 07:10​
[/TD]
[TD]
06/01/2017 11:03​
[/TD]
[TD][/TD]
[TD]
13​
[/TD]
[TD]
35​
[/TD]
[TD][/TD]
[TD]
0,093056​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
TSHJKL4357​
[/TD]
[TD]
06/01/2017 13:15​
[/TD]
[TD]
06/01/2017 17:03​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0,079861​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
TSHJKL4358​
[/TD]
[TD]
06/01/2017 15:08​
[/TD]
[TD]
06/01/2017 20:22​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0,043056​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
TSHJKL4359​
[/TD]
[TD]
06/01/2017 16:08​
[/TD]
[TD]
06/01/2017 17:10​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
TSHJKL4360​
[/TD]
[TD]
06/01/2017 06:35​
[/TD]
[TD]
06/01/2017 09:24​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
TSHJKL4360​
[/TD]
[TD]
06/01/2017 18:00​
[/TD]
[TD]
06/01/2017 20:00​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
0​
[/TD]
[/TR]
</tbody>[/TABLE]


M.
 
Upvote 0
Hi Barry

I *think* i created a simpler formula. See, please, if it's ok

Scenario 1 (just one day)

[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
SR #​
[/td][td]
Start Date Time​
[/td][td]
End Date Time​
[/td][td][/td][td]
Result​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
TSHJKL4356​
[/td][td]
06/01/2017 07:10​
[/td][td]
06/01/2017 11:03​
[/td][td][/td][td]
11:35:00​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
TSHJKL4357​
[/td][td]
06/01/2017 13:15​
[/td][td]
06/01/2017 17:03​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
TSHJKL4358​
[/td][td]
06/01/2017 15:08​
[/td][td]
06/01/2017 20:22​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
TSHJKL4359​
[/td][td]
06/01/2017 16:08​
[/td][td]
06/01/2017 17:10​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
TSHJKL4360​
[/td][td]
06/01/2017 06:35​
[/td][td]
06/01/2017 09:24​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
TSHJKL4360​
[/td][td]
06/01/2017 18:00​
[/td][td]
06/01/2017 20:00​
[/td][td][/td][td][/td][/tr]
[/table]


Array formula in E2
=MIN(C2:C7)-MIN(B2:B7)+SUM(SMALL(C2:C7,ROW(C3:C7)-ROW(C2)+1)-IF(SMALL(C2:C7,ROW(C2:C6)-ROW(C2)+1)>SMALL(B2:B7,ROW(B3:B7)-ROW(B2)+1),SMALL(C2:C7,ROW(C2:C6)-ROW(C2)+1),SMALL(B2:B7,ROW(B3:B7)-ROW(B2)+1)))
Ctrl+Shift+Enter

Scenario 2 (two days with the same times)

[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
SR #​
[/td][td]
Start Date Time​
[/td][td]
End Date Time​
[/td][td][/td][td]
Result​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
TSHJKL4356​
[/td][td]
06/01/2017 07:10​
[/td][td]
06/01/2017 11:03​
[/td][td][/td][td]
23:10:00​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
TSHJKL4357​
[/td][td]
06/01/2017 13:15​
[/td][td]
06/01/2017 17:03​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
TSHJKL4358​
[/td][td]
06/01/2017 15:08​
[/td][td]
06/01/2017 20:22​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
TSHJKL4359​
[/td][td]
06/01/2017 16:08​
[/td][td]
06/01/2017 17:10​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
TSHJKL4360​
[/td][td]
06/01/2017 06:35​
[/td][td]
06/01/2017 09:24​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
TSHJKL4360​
[/td][td]
06/01/2017 18:00​
[/td][td]
06/01/2017 20:00​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
8
[/td][td]
TSHJKL4356​
[/td][td]
07/01/2017 07:10​
[/td][td]
07/01/2017 11:03​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
9
[/td][td]
TSHJKL4357​
[/td][td]
07/01/2017 13:15​
[/td][td]
07/01/2017 17:03​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
10
[/td][td]
TSHJKL4358​
[/td][td]
07/01/2017 15:08​
[/td][td]
07/01/2017 20:22​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
11
[/td][td]
TSHJKL4359​
[/td][td]
07/01/2017 16:08​
[/td][td]
07/01/2017 17:10​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
12
[/td][td]
TSHJKL4360​
[/td][td]
07/01/2017 06:35​
[/td][td]
07/01/2017 09:24​
[/td][td][/td][td][/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
13
[/td][td]
TSHJKL4360​
[/td][td]
07/01/2017 18:00​
[/td][td]
07/01/2017 20:00​
[/td][td][/td][td][/td][/tr]
[/table]


Array Formula in E2
=MIN(C2:C13)-MIN(B2:B13)+SUM(SMALL(C2:C13,ROW(C3:C13)-ROW(C2)+1)-IF(SMALL(C2:C13,ROW(C2:C12)-ROW(C2)+1)>SMALL(B2:B13,ROW(B3:B13)-ROW(B2)+1),SMALL(C2:C13,ROW(C2:C12)-ROW(C2)+1),SMALL(B2:B13,ROW(B3:B13)-ROW(B2)+1)))
Ctrl+Shift+Enter

E2 format --> [h]:mm:ss

Hope i did not miss something :pray:
;)

M.
 
Upvote 0
Looking at the stunrock formula now, i see that the logic of my formula is identical (or very similar) to that of his formula.

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
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