avergeif function

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, I am using the following formula to average negative values in a column, and I would like to tweak it to only average the last "X" number of cells. Hmmm... I may even just assign another cell for this "X" value so that the user can change the number. Anyway, I am not sure how to tweak the formula for that, should I use an OFFSET, or maybe the COUNT? I tried a couple of things but it was not even close. I appreciate any suggestions, thank you.

Excel Formula:
=AVERAGEIF(C:C,"<0")
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Do you want the last X number of total cells or the last X number of negative cells?

Which version of excel are you using?
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Do you want the last X number of total cells or the last X number of negative cells?

Which version of excel are you using?
Thanks for the help jasonb75, It is Windows and Office 2019 - I have updated my profile.

To answer your question it would be the last X number of negative cells. This is basically a log of tank levels and I am trying to figure out how much we use per day/week - but I need to account for the "fills" when they refill the tank.
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Thanks for reminder Joe4, I should have done that a long time ago.
 
Upvote 0
Try: Not sure, but you may need to enter this as an array formula in Excel 2019 with CTRL-SHIFT-ENTER.

Book1
CDE
1DataNumber of <0 values to Average
2-125
3-3Average
4-14-8
5-18
630
7-6
8-18
9-12
10-3
114
12-5
137
14-2
15
Sheet1
Cell Formulas
RangeFormula
E4E4=IF(COUNTIF(C:C,"<0")<$E$2,"Too few values",AVERAGEIF(INDEX(C:C,LARGE(IF(C:C<0,ROW(C:C),""),$E$2)):INDEX(C:C,MATCH(2,1/(C:C<0))),"<0"))
 
Upvote 0
Solution
Try: Not sure, but you may need to enter this as an array formula in Excel 2019 with CTRL-SHIFT-ENTER.

Book1
CDE
1DataNumber of <0 values to Average
2-125
3-3Average
4-14-8
5-18
630
7-6
8-18
9-12
10-3
114
12-5
137
14-2
15
Sheet1
Cell Formulas
RangeFormula
E4E4=IF(COUNTIF(C:C,"<0")<$E$2,"Too few values",AVERAGEIF(INDEX(C:C,LARGE(IF(C:C<0,ROW(C:C),""),$E$2)):INDEX(C:C,MATCH(2,1/(C:C<0))),"<0"))


Geez, I wasn't even close! lol. So I needed to use the index and match functions, that's cool. It worked perfectly and yes, I had to enter it as an array. Thank you for the help and the knowledge - it is very appreciated.
 
Upvote 0
So I needed to use the index and match functions, that's cool.
There are many ways, it would work with offset or index but given that offset is volatile many people opt for alternatives where possible.
That said, there is no right or wrong way, although some people will often have you thinking that there is (yep, I'm guilty of it).
Formulas can also be made to work more effectively by making ranges dynamic, splitting complex calculations over several cells, or even a combination of both as shown below.
The arrays used in this method are much smaller than those that @AhoyNC has used, although both will include all data in the column mine excludes anything below the last row of data (over 1 million extra cells that don't need to be calculated).
Book1.xlsx
CDEFGH
1DataNumber of <0 values to Average
2-125last row14
3-3AverageRow of first -ve to include8
4-14-8
5-18
630
7-6
8-18
9-12
10-3
114
12-5
137
14-2
Sheet2
Cell Formulas
RangeFormula
H2H2=MATCH(1E+100,C:C)
H3H3=AGGREGATE(14,6,ROW(C2:INDEX(C:C,H2))/(C2:INDEX(C:C,H2)<0),E2)
E4E4=AVERAGEIF(INDEX(C:C,H3):INDEX(C:C,H2),"<0")
 
Upvote 0
There are many ways, it would work with offset or index but given that offset is volatile many people opt for alternatives where possible.
That said, there is no right or wrong way, although some people will often have you thinking that there is (yep, I'm guilty of it).
Formulas can also be made to work more effectively by making ranges dynamic, splitting complex calculations over several cells, or even a combination of both as shown below.
The arrays used in this method are much smaller than those that @AhoyNC has used, although both will include all data in the column mine excludes anything below the last row of data (over 1 million extra cells that don't need to be calculated).
Book1.xlsx
CDEFGH
1DataNumber of <0 values to Average
2-125last row14
3-3AverageRow of first -ve to include8
4-14-8
5-18
630
7-6
8-18
9-12
10-3
114
12-5
137
14-2
Sheet2
Cell Formulas
RangeFormula
H2H2=MATCH(1E+100,C:C)
H3H3=AGGREGATE(14,6,ROW(C2:INDEX(C:C,H2))/(C2:INDEX(C:C,H2)<0),E2)
E4E4=AVERAGEIF(INDEX(C:C,H3):INDEX(C:C,H2),"<0")
Thanks for the additional input, it is valued and I will try it out as well. I am always amazed on how you are able to build a formula that is so complex, and sometimes how it turns out to not be so complex as well. I can usually understand how it works as I study it out, but I am in awe of how they get put together.

I have quite a collection of "good" solutions from you gurus in my little black book, and it is indispensable to me
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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