Excel 2016 Add Difference Based on Binary "ON" "OFF" (Logic Text) in a Single Formula with Multiple Occurrences of said Logic

ultma

New Member
Joined
Dec 12, 2013
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
I just cant seem to get this to work. Basically I need to create an if array that has logic but also multiple occurrences. the easy way is in using the formula in cells C2:C6 and the SUM in E2, but this will be a very dynamic sheet with varying amounts of data and the data in A and B is from a plugin that has weird formulas in the cells and doesn't really work well with auto fill. An index match would work (maybe :unsure:) if only I could get it to produce an array (the failed attempt in D2), FILTER or JOINTEXT would work but this is for excel 2016.

In short I'm adding up the time the system is in the on state over a period of time, the period of time is controlled by the plugin so I don't need to worry about that. However I need it to be a single formula in one cell.

Example.xlsx
ABCDE
144608ON
244608.01OFF0.0049190.0147560.014594
344608.01ON0
444608.02OFF0.005
544608.02ON0
644608.02OFF0.004676
Sheet1
Cell Formulas
RangeFormula
D2D2=SUM(IF(B1:B6="off",INDEX(A1:A6,MATCH("OFF",B1:B6,0),1)-INDEX(A1:A6,MATCH("OFF",B1:B6,0)-1,1),0))
E2E2=SUM(C2:C6)
C2:C6C2=IF(B2="off",A2-A1,0)
Press CTRL+SHIFT+ENTER to enter array formulas.
 
this is what it looks like without the indexed cell ranges and references to other cells rather than nesting formula.

The indexed cell ranges do a lot of the heavy lifting thanks to C2 and C3

=IF(C5>C3,SUMIF(B1:B118,"Off",A1:A118)-SUMIF(B1:B118,"On",A1:A118)+(D1-C4),SUMIF(B1:B118,"Off",A1:A118)-SUMIF(B1:B118,"On",A1:A118))
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
not quite there missing logic for the bit at the start if it starts at off
 
Upvote 0
No need for arrays,
The time duration that a switch is in the ON postion is the clock time it was turned Off minus the clock time it was turned On.

=SUMIF(B:B,"OFF",A:A) - SUMIF(B:B."On",A:A) is the total duration that the switch in the OP was in the On position.
 
Upvote 0
No need for arrays,
The time duration that a switch is in the ON postion is the clock time it was turned Off minus the clock time it was turned On.

=SUMIF(B:B,"OFF",A:A) - SUMIF(B:B."On",A:A) is the total duration that the switch in the OP was in the On position.
Correct I was making it difficult for myself yesterday.

Got it sorted now just need one last IF and a tiny tweak to formula to fully make it hands free.

The probelm is at either end of the data, as the extract only records state change between two dates. The first record wont trigger until after the start time unless the start time is the same as the state change event.

I could of used helper cells to create the end and start events as well, which to be honest would be better. However I want to finish the challenge of doing it in one formula now.
 
Upvote 0
this is the final formula
=IF(MATCH("OFF",B:B,0)<MATCH("ON",B:B,0),IF(MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)>MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(E1-LOOKUP(2,1/(B:B="ON"),A:A))+(INDEX(A:A,MATCH("OFF",B:B,0))-D1),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(INDEX(A:A,MATCH("OFF",B:B,0))-D1)),IF(MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)>MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(E1-LOOKUP(2,1/(B:B="ON"),A:A)),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))))

this is the simple version referring to other cells

=IF(D6<D2,IF(D5>D3,SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(E1-D4)+(D7-D1),SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(D7-D1)),IF(D5>D3,SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(E1-D4),SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))))

Example.xlsx
ABCDE
116-Feb-22 00:10:28OFF16/02/2022 12:10 AM16/02/2022 12:41 AM
216-Feb-22 00:16:19ON02Row of first ON
316-Feb-22 00:23:31OFF0.0055Row of last OFF
416-Feb-22 00:28:50ON016-Feb-22 00:41:39Last ON
516-Feb-22 00:35:34OFF0.0046766Row of last ON
616-Feb-22 00:41:39ON01Row of first OFF
7 016-Feb-22 00:10:28First OFF
8 0
9 0
10 0
11 00.00967559formula
12 00.00967559
Sheet1
Cell Formulas
RangeFormula
D2D2=MATCH("ON",B:B,0)
D3D3=MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)
D4D4=LOOKUP(2,1/(B:B="ON"),A:A)
D5D5=MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)
D6D6=MATCH("OFF",B:B,0)
D7D7=INDEX(A:A,MATCH("OFF",B:B,0))
C2:C12C2=IF(B2="OFF",A2-A1,0)
D11D11=IF(MATCH("OFF",B:B,0)<MATCH("ON",B:B,0),IF(MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)>MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(E1-LOOKUP(2,1/(B:B="ON"),A:A))+(INDEX(A:A,MATCH("OFF",B:B,0))-D1),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(INDEX(A:A,MATCH("OFF",B:B,0))-D1)),IF(MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)>MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))+(E1-LOOKUP(2,1/(B:B="ON"),A:A)),SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"Off",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))-SUMIF(INDEX(B:B,MATCH("ON",B:B,0)):INDEX(B:B,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)),"On",INDEX(A:A,MATCH("ON",B:B,0)):INDEX(A:A,MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)))))
D12D12=IF(D6<D2,IF(D5>D3,SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(E1-D4)+(D7-D1),SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(D7-D1)),IF(D5>D3,SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))+(E1-D4),SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"Off",INDEX(A:A,D2):INDEX(A:A,D3))-SUMIF(INDEX(B:B,D2):INDEX(B:B,D3),"On",INDEX(A:A,D2):INDEX(A:A,D3))))
 
Upvote 0
I have run into a problem

I get Consecutive ON's and OFF's sometimes
and I also get BAD sometimes which can create consecutive ON's and OFF's

I cant seem to think of a way to deal with this without manual intervention, i need a way to find these subtract the time stamps from the sums unless there is another way
 
Upvote 0
A bit more detail and sort of an idea to deal with it.

I get Consecutive ON's and OFF's sometimes (almost entirely OFF's that I have seen in a quick looks evenly spaced I would rather deal with this issue altogether than try and take advantage of this incase its not just scenario).

and I also get BAD sometimes which can create consecutive ON's and OFF's i.e. ON, BAD, ON.

I cant seem to think of a way to deal with this without manual intervention, I need a way to find these and subtract the time stamps from the On and OFF sums unless there is another way.

I know I wont get this in a single formula, I have a technique that uses SUMPRODUCT as a helper cell and a nested array formula with IF, INDEX, ROW and SMALL to create a list from a larger data source but I cant think of a criteria to find the these problematic situations.

examples of the issue's.

14-Dec-21 04:53:20​
ON
14-Dec-21 05:12:17​
OFF
14-Dec-21 06:27:45​
Bad
14-Dec-21 06:34:09​
OFF
14-Dec-21 06:50:37​
ON

04-Dec-21 04:18:10​
ON
04-Dec-21 04:51:44​
OFF
04-Dec-21 12:51:44​
OFF
04-Dec-21 20:51:44​
OFF
05-Dec-21 04:51:44​
OFF
05-Dec-21 12:51:44​
OFF
05-Dec-21 20:51:44​
OFF
06-Dec-21 04:51:44​
OFF
06-Dec-21 10:27:16​
ON
 
Upvote 0
What is your expected result?
I suggest 4 scenarios as per below sheet:
Value in column A
ON is negative
OFF is positive
Book1
ABCDEFGHIJKLM
1VALUEONOFFONOFFONOFFONOFF
21ON-1-1
32ON
43ON-3-3
54OFF44
65OFF55
76ON-6-6-6-6
87OFF77
98OFF
109BAD
1110OFF1010
1211ON-11-11-11-11
1312BAD
1413BAD
1514OFF14141414
16
1775119
18
19scenarior 1:scenarior 2:scenarior 3:scenarior 4:
201st ON vs 1st OFFLast ON vs 1st OFF1st ON vs last OFFlast ON vs last OFF
Sheet5
Cell Formulas
RangeFormula
I2,I12,I7I2=-A2
F4,F12,F7F4=-A4
J6,J15,J11J6=A6
A3:A15A3=A2+1
D17,M17,J17,G17D17=SUM(C2:D15)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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