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.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Does D2 work?
If not, what is your expected result in D2 (and D2 downward, maybe?)
 
Upvote 0
Hi,

If I understand correctly, And if your data is Always ON/OFF respectively, may be something like one of these (C1 or C2) single cell formula might work, depending on how your actual data setup is:

Book3.xlsx
ABC
144608ON0.014594
244608.01OFF0.014594
344608.01ON
444608.02OFF
544608.02ON
644608.02OFF
Sheet1006
Cell Formulas
RangeFormula
C1C1=SUMPRODUCT((B1:B6="Off")*(A1:A6)-(B1:B6="On")*(A1:A6))
C2C2=SUMIF(B1:B6,"Off",A1:A6)-SUMIF(B1:B6,"On",A1:A6)
 
Upvote 0
Hi,

If I understand correctly, And if your data is Always ON/OFF respectively, may be something like one of these (C1 or C2) single cell formula might work, depending on how your actual data setup is:

Book3.xlsx
ABC
144608ON0.014594
244608.01OFF0.014594
344608.01ON
444608.02OFF
544608.02ON
644608.02OFF
Sheet1006
Cell Formulas
RangeFormula
C1C1=SUMPRODUCT((B1:B6="Off")*(A1:A6)-(B1:B6="On")*(A1:A6))
C2C2=SUMIF(B1:B6,"Off",A1:A6)-SUMIF(B1:B6,"On",A1:A6)
Thank you
I don't know why I didn't consider sumproduct I use it all the time, must of been a bad day.

One issue if I was being picky is if the initial value is "off" I need to ignore it hence my formula in D2 which didn't work subtracted the ones above "OFF" from the "OFF" times

I also thought sumproduct would still work like this =SUMPRODUCT((B:B="Off")*(A:A)-(B:B="On")*(A:A)) as long as the arrays are the same, I guess I was wrong.
 
Last edited:
Upvote 0
"I also thought sumproduct would still work like this =SUMPRODUCT((B:B="Off")*(A:A)-(B:B="On")*(A:A)) as long as the arrays are the same, I guess I was wrong."

ignore this there is some junk at the bottom of the data from the data link Iferror should fix that
 
Upvote 0
"I also thought sumproduct would still work like this =SUMPRODUCT((B:B="Off")*(A:A)-(B:B="On")*(A:A)) as long as the arrays are the same, I guess I was wrong."

ignore this there is some junk at the bottom of the data from the data link Iferror should fix that
the sumif formula works with the junk in the bottom rows, iferror's didn't work with sumproduct even as an array the A columns throw it off with the junk at the end
 
Upvote 0
the sumif formula works with the junk in the bottom rows, iferror's didn't work with sumproduct even as an array the A columns throw it off with the junk at the end
so I just have the issue if the first cell in B is "OFF"
 
Upvote 0
so I just have the issue if the first cell in B is "OFF"

May be you can just Offset the Range by 1 row as shown below.
You will Not be able to use Entire Column references using this method, but you can make the range very large, more than you think you'll ever need, like B2:B100000 or whatever you want:

Book3.xlsx
ABC
144607.94OFF
244608ON0.014594
344608.01OFF0.014594
444608.01ON
544608.02OFF
644608.02ON
744608.02OFF
Sheet1006
Cell Formulas
RangeFormula
C2C2=SUMPRODUCT((B2:B7="Off")*(A2:A7)-(B1:B6="On")*(A1:A6))
C3C3=SUMIF(B2:B7,"Off",A2:A7)-SUMIF(B1:B6,"On",A1:A6)
 
Upvote 0
Solution
May be you can just Offset the Range by 1 row as shown below.
You will Not be able to use Entire Column references using this method, but you can make the range very large, more than you think you'll ever need, like B2:B100000 or whatever you want:

Book3.xlsx
ABC
144607.94OFF
244608ON0.014594
344608.01OFF0.014594
444608.01ON
544608.02OFF
644608.02ON
744608.02OFF
Sheet1006
Cell Formulas
RangeFormula
C2C2=SUMPRODUCT((B2:B7="Off")*(A2:A7)-(B1:B6="On")*(A1:A6))
C3C3=SUMIF(B2:B7,"Off",A2:A7)-SUMIF(B1:B6,"On",A1:A6)

I almost had it

=IF(B1="off",SUMIF(B:B,"On",A:A)-SUMIF(B:B,"Off",A:A),SUMIF(B:B,"Off",A:A)-SUMIF(B:B,"On",A:A))

however the trick is that the data extract comes form two dates so I need to account for that, however the first field and last field in A does not finish on the exact start and end dates

so I needed to find the last numerical value in the date column A

={INDEX(A:A,MATCH(9.99999999999999E+307,A:A))}
this is now the helper cell J1

and the start date - cell A1

and the difference between the two extract dates K1 and L1

and the difference between the end date L1 and the helper cell J1 the last numerical output in column A

=IF(B1="off",(L1-K1)-(L1-J1)-(A1-K1)-(SUMIF(B:B,"On",A:A)-SUMIF(B:B,"Off",A:A)),SUMIF(B:B,"Off",A:A)-SUMIF(B:B,"On",A:A))

however the spanner in the works is if the last cell in B is also "OFF" as well as B1 and vice versa for B1 and last value also being "ON"

I think I now need a formula to find the last ON and the last OFF and return the row numbers

then use A1:INDEX(A:A,"last row number") to fix this

I would also need a formula to return the corresponding date/time of the last "On" and "OFF"

then I can construct some logic for the different scenarios with some careful thought as to not add error from the first and the final sampled data and the end date.
 
Upvote 0
I got it to work for all scenarios (on off, on on, off off, off on) it was not as complex as I thought it would need to be (just looks that way). The basis was the simple SUMIF formula inside an IF with INDEXED cell references e.g. INDEX(A:A,"row number"):INDEX(A:A,"row number") and some simple MATCH, LOOKUP and nested MATCH/LOOKUP formulas for the row of first ON, Row of last OFF, Row of last ON, and to get the date of the last ON

I nested it all together which I probably shouldn't of done until after I posted this.


Example.xlsx
ABCD
116-Feb-22 00:03:23ON16-Feb-22 00:00:0017-Feb-22 00:00:00
216-Feb-22 00:10:28OFF1Row of first ON
316-Feb-22 00:16:19ON118Row of last OFF
416-Feb-22 00:23:31OFF16-Feb-22 23:34:02Date of last ON
516-Feb-22 00:28:50ON117Row of last ON
616-Feb-22 00:35:34OFF0.617933773formula
Sheet1
Cell Formulas
RangeFormula
D1D1=C1+1
C2C2=MATCH("ON",B:B,0)
C3C3=MATCH(LOOKUP(2,1/(B:B="OFF"),A:A),A:A,0)
C4C4=LOOKUP(2,1/(B:B="ON"),A:A)
C5C5=MATCH(LOOKUP(2,1/(B:B="ON"),A:A),A:A,0)
C6C6=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)))+(D1-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))))
 
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