简体   繁体   中英

Excel formula: For each instance of peak/bottom value in column, get range/distance to the second next peak/bottom

I am looking to solve the following problem in Excel:

ID     Value   Distance
1      1       3
2      0       0
3      -1      3
4      1       0
5      0       0
6      -1      0
7      0       0

Essential the distance column is what I want. It looks at peak/bottom values(1 and -1), then scrolling down to find the second next peak or bottom and compute the distance. For example, for ID 1, since it is peak, we looking for the second peak/bottom, ID 3 should be skipped since its the first, so we look at ID 4 and get distance = 4-1 = 3

Try following formula:

=IFERROR(AGGREGATE(15,6,A2:$A$18/ABS(B2:$B$18),3)/ABS(B2)-A2,0)

Explanation:

AGGREGATE function with first two parameters 15 , 6 and last 3 returns the third smallest value in the array A2:$A$18/ABS(B2:$B$18) ignoring errors - in the first row after division the array looks like this [1, #DIV/0,, 3, 4, #DIV/0,, 6. #DIV/0.. ...] and returns 4 .

Next, this value is divided by the absolute value of column B of the current row (if we divide by 0, then we get an error and the IFERROR function returns 0).

Then we subtract the value of column A of the current row from the obtained result (in the first row 1 ) and we get the desired distance - 3

To get the third and subsequent values, increase the last parameter of the AGGREGATE function accordingly.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM