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.

Original valuesSubstitution values
1
01.5
2
02.333
02.667
3
04
5
03.5
2
02.2
02.4
02.6
02.8
3

<tbody>
</tbody>

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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
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:

Original
Values
Substitution
Values
Basic mathematical functionExplanation
1=IF(A2<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.
01.51 + 1*( (2-1) / (1+1) )

= 1.5
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
2=IF(A4<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.
02.3332 + 1*( (3-2) / (2+1) )

= 2.333
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
02.6672 + 2 * 1/3

= 2.667
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
3=IF(A7<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.
043 + 1*( (5-3) / (1+1) )

= 4
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
5=IF(A9<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.
03.55 + 1*( (2-5) / (1+1) )

= 3.5
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
2=IF(A11<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.
02.22 + 1*( (3-2) / (4+1) )

= 2.2
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
02.42 + 2 * 1/5

= 2.4
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
02.62 + 3 * 1/5

= 2.6
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
02.82 + 4 * 1/5

= 2.8
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
3=IF(A16<>0,"", [part I can't figure out] )If there is a non-zero value in the A column, don't do anything.

<tbody>
</tbody>
 
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,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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