NB: OP is in locale where comma is the decimal separator: $1,00
instead of $1.00
. This affects some formula syntax. North American locale syntax shown below each formula followed by // NA
.
It is unfortunate that you used an invented function to illustrate your problem as it makes it easy to misinterpret the question as being about a Sheets custom function rather than a formula.
There are a few approaches to determine the row difference between the current row and the first one above that also matches a condition.
FILTER w/ MAX
=ROW()-MAX(FILTER(ROW(A:A); ROW(A:A)<ROW(); A:A>655))
=ROW()-MAX(FILTER(ROW(A:A), ROW(A:A)<ROW(), A:A>655)) // NA
|
A |
B |
1 |
640,43 |
|
2 |
644,37 |
|
3 |
650,81 |
|
4 |
655,79 |
|
5 |
652,00 |
|
6 |
653,65 |
|
7 |
633,50 |
|
8 |
640,02 |
|
9 |
637,09 |
5 = ROW()-MAX(FILTER(ROW(A:A); ROW(A:A)<ROW(); A:A>655)) |
10 |
638,55 |
|
=9-FILTER(
{ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 };
{ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 } < 8;
{ 640,43; 644,37; 650,81; 655,79; 652,00;
653,65; 633,50; 640,02; 637,09; 638,55 } > 655))
=9-FILTER(
{ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 };
{ T; T; T; T; T; T; T; F; F; F };
{ F; F; F; T; F; F; F; F; F; F }))
=9-FILTER(
{ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 };
{ F; F; F; T; F; F; F; F; F; F }))
=9-4
=5
- FILTER is applied to
ROW(A:A)
to return all the row numbers from Column A that match both of the following conditions:
- The row number is less that the current row's number:
ROW(A:A)<ROW()
- The cell's value is greater than 655:
A:A>655
- The MAX row number returned by the FILTER is the matching row that is closest to but above the current row, which is then subtracted from the current row for the final result.
FILTER w/ MIN
=MIN(FILTER(ROW()-ROW(A:A); ROW(A:A)<ROW(); A:A>655))
=MIN(FILTER(ROW()-ROW(A:A), ROW(A:A)<ROW(), A:A>655)) // NA
Similar to MAX version but subtraction is now moved inside the FILTER function.
SORTN
=SORTN(IF((ROW()-ROW(A:A)>0) * A:A>655; ROW()-ROW(A:A);))
=SORTN(IF((ROW()-ROW(A:A)>0) * A:A>655, ROW()-ROW(A:A),)) // NA
- SORTN is an array function like FILTER so the IF function inside it is applied to each row in column A.
- The conditions are multiplied together and return 1 (true) if both are true or 0 (false) if at least one is false.
- IF's value_if_true is the subtraction of matching row numbers from the current row number.
- The default SORTN arguments (sort column=1, sort order=asc., max results=1, allow dupes=0) are perfect so they are simply omitted and the result of the formula is the MIN of valid
ROW()-ROW(A:A)
values.
XMATCH
This one is the shortest, but may not be appropriate depending on the use case. It matches based on first value equal-to or greater-than (not just greater-than) so you will need to add 0,01
onto your search string or if higher precision is required smaller values like 10^-5 (0,00001)
.
=ROW()-XMATCH(655,01; OFFSET(A:A;;;ROW());1;-1)
=ROW()-XMATCH(655.01, OFFSET(A:A,,,ROW()),1,-1) // NA