Formula to calculate dinamically the average costs until the last non-blank cell

vladimiratanasiu

Active Member
Joined
Dec 17, 2010
Messages
346
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello!

I have a large table with monthly spendings. Some months they still don't happen, so that the correspondent cells are empty. I need a formula to calculate dinamically the average incurred costs until the last period with value, but counting also the previous months without spendings (see the results from column C).

Thank you!

Book1
ABC
1MonthEuroAverage
2Jan-2236.5736.57
3Feb-2244.7040.635
4Mar-22
5Apr-2246.2531.88
6May-22-7.1224.08
7Jun-22
8Jul-22
9Aug-2233.3219.215
10Sep-22
11Oct-22
12Nov-2214.3615.28
13Dec-22
14Average
Sheet1
Cell Formulas
RangeFormula
C2C2=B2/1
C3C3=SUM(B2:B3)/2
C5C5=SUM(B2:B5)/4
C6C6=SUM(B2:B6)/5
C9C9=SUM(B2:B9)/8
C12C12=SUM(B2:B12)/11
 
Ok, how about
Excel Formula:
=AVERAGE(--B2:INDEX(B1:B13,LOOKUP(99^99,B2:B13,ROW(B2:B13))))
 
Upvote 0
Solution

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Let me understand - You want average in Cell C14 from B2 to the cell in Column B that corresponds to the current month. Right or is it something different?
Is this what you needed - Check cell C14

All Records.xlsb
ABC
1MonthEuroAverage
2Jan-202236.5736.57
3Feb-202244.7040.64
4Mar-2022
5Apr-202246.2531.88
6May-2022(7.12)24.08
7Jun-2022
8Jul-2022
9Aug-202233.3219.22
10Sep-2022
11Oct-2022
12Nov-202214.3615.28
13Dec-2022
14Average 19.215
Sheet3
Cell Formulas
RangeFormula
C2C2=B2/1
C3C3=SUM(B2:B3)/2
C5C5=SUM(B2:B5)/4
C6C6=SUM(B2:B6)/5
C9C9=SUM(B2:B9)/8
C12C12=SUM(B2:B12)/11
B14B14=SUMIFS(B2:B13,A2:A13,"<="&TODAY())/COUNTIFS(A2:A13,"<="&TODAY())
 
Upvote 0
What are you expecting for the result, 15.28, 14 or something else?

Ok, how about
Excel Formula:
=AVERAGE(--B2:INDEX(B1:B13,LOOKUP(99^99,B2:B13,ROW(B2:B13))))
Book1
ABC
1MonthEuroAverage
201/01/202236.5736.57
301/02/202244.7040.635
401/03/2022
501/04/202246.2531.88
601/05/2022-7.1224.08
701/06/2022
801/07/2022
901/08/202233.3219.215
1001/09/2022
1101/10/2022
1201/11/202214.3615.28
1301/12/2022
14Average #VALUE!
Sheet1
Cell Formulas
RangeFormula
C2C2=B2/1
C3C3=SUM(B2:B3)/2
C5C5=SUM(B2:B5)/4
C6C6=SUM(B2:B6)/5
C9C9=SUM(B2:B9)/8
C12C12=SUM(B2:B12)/11
B14B14=AVERAGE(--B2:INDEX(B1:B13,LOOKUP(99^99,B2:B13,ROW(B2:B13))))


Am I missing any detail ? Is it possible on the B1 reference?
 
Upvote 0
Does column B contain formulae that return ""
 
Upvote 0
Is this what you needed - Check cell C14

All Records.xlsb
ABC
1MonthEuroAverage
2Jan-202236.5736.57
3Feb-202244.7040.64
4Mar-2022
5Apr-202246.2531.88
6May-2022(7.12)24.08
7Jun-2022
8Jul-2022
9Aug-202233.3219.22
10Sep-2022
11Oct-2022
12Nov-202214.3615.28
13Dec-2022
14Average 19.215
Sheet3
Cell Formulas
RangeFormula
C2C2=B2/1
C3C3=SUM(B2:B3)/2
C5C5=SUM(B2:B5)/4
C6C6=SUM(B2:B6)/5
C9C9=SUM(B2:B9)/8
C12C12=SUM(B2:B12)/11
B14B14=SUMIFS(B2:B13,A2:A13,"<="&TODAY())/COUNTIFS(A2:A13,"<="&TODAY())
Did you check the above post vladimiratanasiu
 
Upvote 0
Thank you very much, Fluff and SanjayGulatiMusafir! Both formulas run perfectly and I wish I could mark each one of them. However, giving the very large number of positive reviews accorded to Fluff, I decided to mark the SanjayGulatiMusafir solution. And hope it won't offend the other member.:)
 
Upvote 0
I would disagree they both give totally different answers with your test data. :unsure:
😳😳😳 Indeed, you are very right. After removing all values from both tables and inserting them again one by one, I noticed, to my shame, that the results are different. It was my mistake that I considered the two formulas available in any condition. Please excuse me, and if it's possible, change the marker from SanjayGulatiMusafir's solution to yours one. P.S. I'd appreciate if you could change the content from my previous message, keeping only the mention about your version.
 
Upvote 0
All you need to do is mark the post of your choice as the solution & it will override the one you have already marked. :)
 
Upvote 0
All you need to do is mark the post of your choice as the solution & it will override the one you have already marked. :)
Thank you! One more question: I had to do some changes in my worksheet, meaning to move the respective table in other cells. As a result, the formula is no more working, giving the results from the attached image. The table references from the photo are the real cells, so that they can be used in the new solution.

Book2
ABC
1
2
3MonthEuro
41/1/202236.57
52/1/202244.7
63/1/2022
74/1/202246.25
85/1/2022-7.12
96/1/2022
107/1/2022
118/1/202233.32
129/1/2022
13########
14########14.36
15########
16Average #REF!
Sheet1
Cell Formulas
RangeFormula
C16C16=AVERAGE(--C4:INDEX(C3:C15,LOOKUP(99^99,C4:C15,ROW(C4:C15))))
 

Attachments

  • Untitled.png
    Untitled.png
    17.3 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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