IF function with 3 consecutive condition

Researchstudent

New Member
Joined
Jan 11, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I need to write a formula to determine whether an event took place or not. Definition of event is at least 3 consecutive column cells with a value lower than 65. I would like to determine this for a certain period of time, for example in the coming 5 minutes. Every cell represents 20 seconds, so for 5 minutes I will be looking at 15 cells. The question is: are there 3 consecutive values lower than 65 within the 15 cells? If yes, give 1 (event), if no give 0 (no event) . It can also be more than 3 consecutive cells, but it should be at least 3 consecutive values

If I write IF(A1:A15<65;"1";"0") then I am not including the at least 3 consecutive condition. How can I do this?

Here is an example of what my data look like, and I manually added 1 to indicate that the formula should give a 1 in this case

1610373659199.png
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Offset to define your range.
=IF(COUNTIF(OFFSET(A1,0,0,15),"<65")>=3,1,0)
also might replace "<65" with "<"&cellreference to make it updatable.
 
Upvote 0
Oops! Doesn't test for consecutive in the last one.

Offset to define your range in a helper column. Next to the values from A1 down:
=IF(COUNTIF(OFFSET(A1,-2,0,3),"<65")>=3,1,0)
also might replace "<65" with "<"&cellreference to make it updatable.
Then next to that column (C1?):
=IF(COUNTIF(OFFSET(B1,0,0,15),">0")>0,1,0)
 
Upvote 0
Hi Researchstudent,

Here's my long-winded way:

Researchstudent.xlsx
ABCD
1TimeValueResult
215:54:04771
315:54:2474
415:54:4471
515:55:0468
615:55:2465
715:55:4477
815:56:0474
915:56:2471
1015:56:4468
1115:57:0465
1215:57:2462
1315:57:4459
1415:58:0456
1515:58:2453
1615:58:4450
17
Sheet1
Cell Formulas
RangeFormula
D2D2=IFERROR(ISNUMBER(AGGREGATE(15,6,ROW($A$2:$A$9999)/(($B$2:$B$9999<65)*($B$3:$B$10000<65)*($B$4:$B$10001<65)*($B$2:$B$9999<>"")),1)),0)+0
 
Upvote 0
Hi Researchstudent,

Here's my long-winded way:

Researchstudent.xlsx
ABCD
1TimeValueResult
215:54:04771
315:54:2474
415:54:4471
515:55:0468
615:55:2465
715:55:4477
815:56:0474
915:56:2471
1015:56:4468
1115:57:0465
1215:57:2462
1315:57:4459
1415:58:0456
1515:58:2453
1615:58:4450
17
Sheet1
Cell Formulas
RangeFormula
D2D2=IFERROR(ISNUMBER(AGGREGATE(15,6,ROW($A$2:$A$9999)/(($B$2:$B$9999<65)*($B$3:$B$10000<65)*($B$4:$B$10001<65)*($B$2:$B$9999<>"")),1)),0)+0

Hi Toadstool,

Thank you for your reply! Unfortunately this formula gives me a 1 all the time in my dataset, even if there is no value below 65 within the 15 cells. Does this formula look at the entire A column? I can't think another reason why it would always give 1 as result. How can I set it to check only within 15 cells (so within 5 minutes) in A column?
 
Upvote 0
Oops! Doesn't test for consecutive in the last one.

Offset to define your range in a helper column. Next to the values from A1 down:
=IF(COUNTIF(OFFSET(A1,-2,0,3),"<65")>=3,1,0)
also might replace "<65" with "<"&cellreference to make it updatable.
Then next to that column (C1?):
=IF(COUNTIF(OFFSET(B1,0,0,15),">0")>0,1,0)

Hello C moore,

Thank you for your reply. It almosts works but not exactly. This first formula tests only for the 3 consecutives from upper rows, but I also need to test it for the 3 consecutives from lower rows. Here is a screenshot from another part of my dataset. I put the first formula in D and the second in E. According to this formula, I get 0 in column D on row 77 and 78 even though the correct answer is 1 (because both rows are followed by 2 values that are lower than 65). It does work well for row 79 and 80 since it checks for the 3 consecutives from upper rows. If I can set it to check for both upper and lower 3 consecutive rows (for every value in column B) then I would have the right answer. Is there a way to do this?

1610446464225.png
 
Upvote 0
Hi Toadstool,

Thank you for your reply! Unfortunately this formula gives me a 1 all the time in my dataset, even if there is no value below 65 within the 15 cells. Does this formula look at the entire A column? I can't think another reason why it would always give 1 as result. How can I set it to check only within 15 cells (so within 5 minutes) in A column?

I'd forgotten to check the last row isn't followed by empty cells (required for this to work as per your example).

This just checks rows 2 to 16 and requires rows 17 and 18 to be empty.

Researchstudent.xlsx
ABCDEF
1TimeValueResultStarting at Row
215:54:04770 
315:54:2474
415:54:4471
515:55:0468
615:55:2466
715:55:4465
815:56:0444
915:56:2471
1015:56:4466
1115:57:0455
1215:57:2471
1315:57:4466
1415:58:0444
1515:58:2466
1615:58:4444
Sheet2
Cell Formulas
RangeFormula
D2D2=IFERROR(ISNUMBER(AGGREGATE(15,6,ROW($A$2:$A$16)/(($B$2:$B$16<65)*($B$3:$B$17<65)*($B$4:$B$18<65)*($B$2:$B$16<>"")*($B$3:$B$117<>"")*($B$4:$B$18<>"")),1)),0)+0
F2F2=IFERROR(AGGREGATE(15,6,ROW($A$2:$A$16)/(($B$2:$B$16<65)*($B$3:$B$17<65)*($B$4:$B$18<65)*($B$2:$B$16<>"")*($B$3:$B$117<>"")*($B$4:$B$18<>"")),1),"")
 
Upvote 0
I'd forgotten to check the last row isn't followed by empty cells (required for this to work as per your example).

This just checks rows 2 to 16 and requires rows 17 and 18 to be empty.

Researchstudent.xlsx
ABCDEF
1TimeValueResultStarting at Row
215:54:04770 
315:54:2474
415:54:4471
515:55:0468
615:55:2466
715:55:4465
815:56:0444
915:56:2471
1015:56:4466
1115:57:0455
1215:57:2471
1315:57:4466
1415:58:0444
1515:58:2466
1615:58:4444
Sheet2
Cell Formulas
RangeFormula
D2D2=IFERROR(ISNUMBER(AGGREGATE(15,6,ROW($A$2:$A$16)/(($B$2:$B$16<65)*($B$3:$B$17<65)*($B$4:$B$18<65)*($B$2:$B$16<>"")*($B$3:$B$117<>"")*($B$4:$B$18<>"")),1)),0)+0
F2F2=IFERROR(AGGREGATE(15,6,ROW($A$2:$A$16)/(($B$2:$B$16<65)*($B$3:$B$17<65)*($B$4:$B$18<65)*($B$2:$B$16<>"")*($B$3:$B$117<>"")*($B$4:$B$18<>"")),1),"")

But my dataset consists of 10000 rows. With this formula I would have it to add 2 empty rows after every 15 cells which would take a long time. Do you think this is the only way?
 
Upvote 0
Welcome to the MrExcel board!

Can you give us a set of sample data and the expected results with XL2BB ?
Then explain again how you get those results manually in relation to that specific sample data.
 
Upvote 0
But my dataset consists of 10000 rows. With this formula I would have it to add 2 empty rows after every 15 cells which would take a long time. Do you think this is the only way?

OK, this checks up to row 20003, requiring the two cells after the last entry in column B be empty. The single "There are at least 3 consecutive rows less than 65" indicator remains in D2 with column F giving the first row of such a set and column G giving the Time. I generated random numbers to get the test data down to row 10,000.

Researchstudent.xlsx
ABCDEFG
1TimeValueResultStarting at RowStart Time
215:54:0477193821:06:04
315:54:247495921:13:04
415:54:4471115422:18:04
515:55:0468132023:13:24
615:55:246616130:51:04
715:55:446516140:51:24
815:56:044418011:53:44
915:56:247131819:33:44
1015:56:4466342510:55:04
1115:57:0455357511:45:04
1215:57:2471357611:45:24
1315:57:4466444616:35:24
1415:58:0444481118:37:04
1515:58:246673568:45:24
1615:58:444474579:19:04
1715:59:0411175019:33:44
1815:59:2477767610:32:04
1915:59:4466930919:36:24
2016:00:0474  
2116:00:2499  
Sheet2
Cell Formulas
RangeFormula
D2D2=IFERROR(ISNUMBER(AGGREGATE(15,6,ROW($A$2:$A$20000)/(($B$2:$B$20000<65)*($B$3:$B$20001<65)*($B$4:$B$20002<65)*($B$2:$B$20000<>"")*($B$3:$B$20001<>"")*($B$4:$B$20002<>"")),1)),0)+0
F2:F21F2=IFERROR(AGGREGATE(15,6,ROW($A$2:$A$20000)/(($B$2:$B$20000<65)*($B$3:$B$20001<65)*($B$4:$B$20002<65)*($B$2:$B$20000<>"")*($B$3:$B$20001<>"")*($B$4:$B$20002<>"")),ROW()-ROW($F$1)),"")
G2:G21G2=IFERROR(INDEX(A:A,F2),"")
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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