[英]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:我希望在 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.
它查看峰值/谷值(1 和 -1),然后向下滚动以找到下一个峰值或谷值并计算距离。 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
例如,对于 ID 1,由于它是峰值,我们寻找第二个峰值/底部,ID 3 从第一个开始就应该被跳过,所以我们查看 ID 4 并得到距离 = 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
. AGGREGATE
function 与前两个参数15
、 6
和最后3
返回数组A2:$A$18/ABS(B2:$B$18)
忽略错误的第三个最小值 - 在除法后的第一行中,数组看起来像这样[1, #DIV/0,, 3, 4, #DIV/0,, 6. #DIV/0.. ...]
并返回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).接下来,将该值除以当前行
B
列的绝对值(如果除以 0,则会得到错误, IFERROR
function 返回 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
然后我们从获得的结果(在第一行
1
中)中减去当前行的列A
的值,我们得到所需的距离 - 3
To get the third and subsequent values, increase the last parameter of the AGGREGATE
function accordingly.要获得第三个和后续值,请相应地增加
AGGREGATE
function 的最后一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.