How to sum the last 12 non-zero numbers dynamically by linking to "today" function

Chukmiester

New Member
Joined
Jan 17, 2023
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
1673993188532.png

I believe I was able to figure out a formula for a perpetual YTD by having excel track today's date which is automatic, but now I am trying to figure out how to have excel now look at the last non-zero cell dynamically and sum the last 12 spaces. i.e. the 7 in the YTD. should be 18; summing the last 12 periods starting with that 7 and if I add a number after that then begin there and count the last 12 and so on.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Upvote 0
A couple of other options:
(with row 5 being the date row)

By the way unless you use a Total row to "look at the last non-zero cell", I think this is really bad idea.
It is likely that the result would be that each lines' total will cover a different period and this makes the figure misleading.

Using Offset:
Excel Formula:
=SUM(OFFSET(INDEX($CT$11:$DZ$11,0,MATCH(LOOKUP(2,1/($CT$11:$DZ$11<>0),$CT$5:$DZ$5),$CT$5:$DZ$5,)),,-11,,12))

Without Offset
Excel Formula:
=SUM(
             INDEX($CT$11:$DZ$11, 0, MATCH( LOOKUP(2,1/($CT$11:$DZ$11<>0), $CT$5:$DZ$5), $CT$5:$DZ$5)) :
             INDEX($CT$11:$DZ$11, 0, MATCH( LOOKUP(2,1/($CT$11:$DZ$11<>0), $CT$5:$DZ$5), $CT$5:$DZ$5 ) - 11))
 
Upvote 0
Welcome to the MrExcel board!

Comments on the above suggestions.
  • OFFSET is a volatile function so best avoided if reasonable alternatives exist, which they do here.
  • Both the OFFSET formulas are, in my view, risky anyway since early in the year when less than 12 months of data may be available they could incorrectly & without warning include any numerical values to the left of the range being looked at. (Alex's second formula errors in this situation, which at least is a safer option. 😎 )
  • Alex, I'm sure you wouldn't have meant to use absolute row values for row 11?
I have assumed:
  • That no non-zero values will exist forward of the current month.
  • That the values in the header row are actual dates being the 1st of each month but formatted to show month & year only.
  • That the dashes in the original screen shot are actually zero formatted to show dash.
  • That for YTD you would be including Jan-23 back to and including Feb-22.
I'm avoiding OFFSET. Column DU is a formula for YTD, column DV sums 12 columns (or less if they don't exist) back from the last non-zero value.
I have hidden some (zero) columns to keep the mini sheet a bit smaller.

23 01 18.xlsm
CTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDSDTDUDV
5Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22Jan-23Feb-23Dec-23YTDLast 12
6062211100210307001818
7505412032814705003737
80000000000000000000
932324000000000000614
YTD
Cell Formulas
RangeFormula
DU6:DU9DU6=SUMIFS(CT6:DS6,CT$5:DS$5,"<="&TODAY(),CT$5:DS$5,">"&EDATE(TODAY(),-12))
DV6:DV9DV6=SUM(INDEX(CT6:DS6,MAX(IFERROR(AGGREGATE(14,6,(COLUMN(CT6:DS6)-COLUMN(CT6)+1)/(CT6:DS6<>0),1),1)-11,1)):DS6)
 
Last edited:
Upvote 0
By the way unless you use a Total row to "look at the last non-zero cell", I think this is really bad idea.
It is likely that the result would be that each lines' total will cover a different period and this makes the figure misleading.
Using Peter's data and referring to the last 12 mths column:
• Rows 6 & 7 - Sums Feb 2022 to Jan 2023
• Row 9 - Sums Nov 2021 to Mar 2022 (This is only 5 months)
Not only is the 12 month's ending Mar 2022 a different last 12 month period to other rows which I don't think your users will be expecting but in this case it is only 5 months not 12 months (per the heading).
So I personally would not be comfortable this approach.
 
Upvote 0
Using Peter's data and referring to the last 12 mths column:
Just to clarify: My post does not have a 'last 12 mths' column. It is simply a 'Last 12' column, based on:
now I am trying to figure out how to have excel now look at the last non-zero cell dynamically and sum the last 12 spaces.

I took the expression "last non-zero cell" to mean 'last non-zero cell for the current row' whereas you are considering it might mean 'last non-zero cell in any row'.
Since we don't really know what the various rows of data represent or what the OP is actually trying to do so I think that your caution/warning is well-advised. :)

Hopefully we will find out in due course.
 
Upvote 0
it might mean 'last non-zero cell in any row'.
If the goal is to add 12* columns always counting back from the column that has the last non-zero value in any row (column DG in my sample below) then column DW has a suggestion for that.
* .. or less than 12 columns if the last non-zero column in the 'longest' row is less than 11 columns to the right of the first column in the range.

23 01 18.xlsm
CTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDSDTDUDVDW
5Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22Jan-23Feb-23Dec-23YTDLast 12Last 12 (2)
606221110021030000111913
758541203281478000404545
800000000000000000000
9323240000000000006149
YTD
Cell Formulas
RangeFormula
DU6:DU9DU6=SUMIFS(CT6:DS6,CT$5:DS$5,"<="&TODAY(),CT$5:DS$5,">"&EDATE(TODAY(),-12))
DV6:DV9DV6=SUM(INDEX(CT6:DS6,MAX(IFERROR(AGGREGATE(14,6,(COLUMN(CT6:DS6)-COLUMN(CT6)+1)/(CT6:DS6<>0),1),1)-11,1)):DS6)
DW6:DW9DW6=SUM(INDEX(CT6:DS6,MAX(IFERROR(AGGREGATE(14,6,(COLUMN(CT6:DS6)-COLUMN(CT6)+1)/(CT$6:DS$9<>0),1),1)-11,1)):DS6)
 
Upvote 0
Welcome to the MrExcel board!

Comments on the above suggestions.
  • OFFSET is a volatile function so best avoided if reasonable alternatives exist, which they do here.
  • Both the OFFSET formulas are, in my view, risky anyway since early in the year when less than 12 months of data may be available they could incorrectly & without warning include any numerical values to the left of the range being looked at. (Alex's second formula errors in this situation, which at least is a safer option. 😎 )
  • Alex, I'm sure you wouldn't have meant to use absolute row values for row 11?
I have assumed:
  • That no non-zero values will exist forward of the current month.
  • That the values in the header row are actual dates being the 1st of each month but formatted to show month & year only.
  • That the dashes in the original screen shot are actually zero formatted to show dash.
  • That for YTD you would be including Jan-23 back to and including Feb-22.
I'm avoiding OFFSET. Column DU is a formula for YTD, column DV sums 12 columns (or less if they don't exist) back from the last non-zero value.
I have hidden some (zero) columns to keep the mini sheet a bit smaller.

23 01 18.xlsm
CTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDSDTDUDV
5Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22Jan-23Feb-23Dec-23YTDLast 12
6062211100210307001818
7505412032814705003737
80000000000000000000
932324000000000000614
YTD
Cell Formulas
RangeFormula
DU6:DU9DU6=SUMIFS(CT6:DS6,CT$5:DS$5,"<="&TODAY(),CT$5:DS$5,">"&EDATE(TODAY(),-12))
DV6:DV9DV6=SUM(INDEX(CT6:DS6,MAX(IFERROR(AGGREGATE(14,6,(COLUMN(CT6:DS6)-COLUMN(CT6)+1)/(CT6:DS6<>0),1),1)-11,1)):DS6)
Hello, you seem to be onto something. Your formula almost worked. The first assumption is not correct and that is my fault. There may actually be zero numbers going forward. The dates are the first of each month.
 
Upvote 0
Your formula almost worked.
Which formula (I posted 3 of them)?

You would also have seen discussion in the thread about two different interpretations of your statement " look at the last non-zero cell dynamically and sum the last 12 spaces."
You have not clarified which is the correct one. Please do so. That is, for row 6 in my last mini sheet, is the correct answer
19 - finding the last non-zero value in row 6 (Nov-22) and summing row 6 twelve columns back from the Nov-22 column
or
13 - finding the last non-zero value in any row (Dec-22 in row 7) and summing row 6 twelve columns back from the Dec-22 column
or
something else?
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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