AVERAGE previous and next non-zero values over variable number of rows

bigmyk2k

Board Regular
Joined
Feb 9, 2012
Messages
104
Trying to figure out a way that I can populate a column with the average of the next and previous non-zero values and divide the difference between the rows that separate them.

Consider the following. I want to take the first column, and have a function that returns the values in the second column.

[TABLE="class: grid, width: 128"]
<tbody>[TR]
[TD="width: 64"]Original values[/TD]
[TD="width: 64"]Substitution values[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]1.5[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.333[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.667[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]3.5[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.2[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.4[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.6[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[TD="align: right"]2.8[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I feel like there has to be some combination of INDEX, MATCH, COUNTIFS and COUNTA that works, but I can't find it.
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Your request is poorly worded and thus we can't figure out how you calculated each 'Substitution Value.' Please enlighten us by providing the calculations for each.
 
Upvote 0
Your request is poorly worded and thus we can't figure out how you calculated each 'Substitution Value.' Please enlighten us by providing the calculations for each.

Sorry, I thought I had been pretty clear. I am trying to find a function to put in column B that figures an average (or not) based on column A.

In all of the explanations below, the two parts I can't figure out are:

1) How to tell excel to count how many rows you have to go up and how many rows down (in column A) to find a non-zero value, and

2) How to tell excel to look for and return the value of the first non-zero values (in column A) both above and below the current zero-value row.

Technically, in the data I am working with there won't be any negative numbers, but I don't think that effects the formula.

I will say it every way I can think of; see if this helps:

[TABLE="class: grid, width: 900"]
<tbody>[TR]
[TD]Original
Values[/TD]
[TD]Substitution
Values[/TD]
[TD]Basic mathematical function[/TD]
[TD]Explanation[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD]=IF(A2<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]1.5[/TD]
[TD]1 + 1*( (2-1) / (1+1) )

= 1.5[/TD]
[TD]There is a non-zero value of 1 one row above the current zero-value cell, and a non-zero value of 2 one row below the current zero-value cell. The cell value should be calculated as follows:

1 (previous non-zero value) +

1
(number of rows up) * 2 (next value) - 1 (previous value) = 1 (numerator)
1 (rows up) + 1 (rows down) = 2 (denominator)

Put another way, the average change per row between 1 and 2 (1; the numerator) is 1/(1+1) or 0.5 (there are two changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 1, and added to the previous non-zero value.

1 (previous non-zero value) + 1 (# of rows since a non-zero value) *1/2 (average change) = 1.5

The value I want to see in this cell is 1.5[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]=IF(A4<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.333[/TD]
[TD]2 + 1*( (3-2) / (2+1) )

= 2.333[/TD]
[TD]There is a non-zero value of 2 one row above the current zero-value cell, and a non-zero value of 3 two rows below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

1 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator)
1 (rows up) + 2 (rows down) = 3 (denominator)

Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(2+1) or 0.333 (there are three changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 1, and added to the previous non-zero value.

2 (previous non-zero value) + 1 (# of rows since a non-zero value) *1/3 (average change) = 2.333

The value I want to see in this cell is 2.333[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.667[/TD]
[TD]2 + 2 * 1/3

= 2.667[/TD]
[TD]There is a non-zero value of 2 two rows above the current zero-value cell, and a non-zero value of 3 one row below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

2 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator)
2 (rows up) + 1 (rows down) = 3 (denominator)

Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(2+1) or 0.333 (there are three changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 2, and added to the previous non-zero value.

2 (previous non-zero value) + 2 (# of rows since a non-zero value) *1/3 (average change) = 2.667

The value I want to see in this cell is 2.667[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]=IF(A7<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]4[/TD]
[TD]3 + 1*( (5-3) / (1+1) )

= 4[/TD]
[TD]There is a non-zero value of 3 one row above the current zero-value cell, and a non-zero value of 5 one row below the current zero-value cell. The cell value should be calculated as follows:

3 (previous non-zero value) +

1 (number of rows up) * 5 (next value) - 3 (previous value) = 2 (numerator)
1
(rows up) + 1 (rows down) = 2 (denominator)Put another way, the average change per row between 3 and 5 (2; the numerator) is 2/(1+1) or 1 (there are two changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 1, and added to the previous non-zero value.

3 (previous non-zero value) + 1 (# of rows since a non-zero value) *1 (average change) = 4

The value I want to see in this cell is 4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]=IF(A9<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]3.5[/TD]
[TD]5 + 1*( (2-5) / (1+1) )

= 3.5[/TD]
[TD]There is a non-zero value of 5 one row above the current zero-value cell, and a non-zero value of 2 one row below the current zero-value cell. The cell value should be calculated as follows:

5 (previous non-zero value) +

1 (number of rows up) *5 (next value) - 2 (previous value) =-3 (numerator)
1
(rows up) + 1 (rows down) = 2 (denominator)Put another way, the average change per row between 5 and 2 (-3; the numerator) is -3/(1+1) or -1.5 (there are two changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 1, and added to the previous non-zero value.

5 (previous non-zero value) + 1 (# of rows since a non-zero value) *-1.5 (average change) = 3.5
The value I want to see in this cell is 3.5[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]=IF(A11<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.2[/TD]
[TD]2 + 1*( (3-2) / (4+1) )

= 2.2[/TD]
[TD]There is a non-zero value of 2 one row above the current zero-value cell, and a non-zero value of 3 four rows below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

1 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator)
1
(rows up) + 4 (rows down) = 5 (denominator)Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(4+1) or 0.2 (there are five changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 1, and added to the previous non-zero value.

2 (previous non-zero value) + 1 (# of rows since a non-zero value) * 0.2 (average change) = 2.2

The value I want to see in this cell is 2.2[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.4[/TD]
[TD]2 + 2 * 1/5

= 2.4[/TD]
[TD]There is a non-zero value of 2 two rows above the current zero-value cell, and a non-zero value of 3 three rows below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

2 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator) 2 (rows up) + 3 (rows down) = 5 (denominator)

Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(2+3) or 0.2 (there are five changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 2, and added to the previous non-zero value.

2 (previous non-zero value) + 2 (# of rows since a non-zero value) * 0.2 (average change) = 2.4

The value I want to see in this cell is 2.4[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.6[/TD]
[TD]2 + 3 * 1/5

= 2.6[/TD]
[TD]There is a non-zero value of 2 three rows above the current zero-value cell, and a non-zero value of 3 two rows below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

3 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator)
3 (rows up) + 2 (rows down) = 5 (denominator)

Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(3+2) or 0.2 (there are five changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 3, and added to the previous non-zero value.

2 (previous non-zero value) + 3 (# of rows since a non-zero value) * 0.2 (average change) = 2.6

The value I want to see in this cell is 2.6[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]2.8[/TD]
[TD]2 + 4 * 1/5

= 2.8[/TD]
[TD]There is a non-zero value of 2 four rows above the current zero-value cell, and a non-zero value of 3 one row below the current zero-value cell. The cell value should be calculated as follows:

2 (previous non-zero value) +

4 (number of rows up) * 3 (next value) - 2 (previous value) = 1 (numerator)
4 (rows up) + 1 (rows down) = 5 (denominator)

Put another way, the average change per row between 2 and 3 (1; the numerator) is 1/(3+2) or 0.2 (there are five changes in row between the non-zero values; the number in the denominator will be the number of zero-value cells, plus 1). This is multiplied by the number of rows we've moved down from the previous non-zero value, 3, and added to the previous non-zero value.

2 (previous non-zero value) + 4 (# of rows since a non-zero value) * 0.2 (average change) = 2.8

The value I want to see in this cell is 2.8[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]=IF(A16<>0,"", [part I can't figure out] )[/TD]
[TD]If there is a non-zero value in the A column, don't do anything.[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Try this. It is an array formula so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. The formula can then be copied down.
Where I have used $20 in the formula, make sure that is at least one row below the last row of data in column A.

Excel Workbook
AB
1Original valuesSubstitution values
21 
301.5
42
502.333333333
602.666666667
73
804
95
1003.5
112
1202.2
1302.4
1402.6
1502.8
163
17
Split Gaps
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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