Home » excel » excel – Sum Column based on prior 12 columns' row entries (rolling count)

# excel – Sum Column based on prior 12 columns' row entries (rolling count)

Questions:

Please see the picture. I have a data table with multiple rows (the real data I’m working with has 2,000+ rows).

I need to sum the row entries in each column, but I need to put those sums in two different categories: “new”, and “return”. New starts at the first date the value appears, and then it’s considered new for the next 12 months (so 10/1/2005 – 10/1/2006). Any value for that Type after the initial 12 month window is then considered “return”. The dates the first value appears at is always different though, for each type (Rock, Paper, Scissors), so there’s not one static date I can measure from, for each row the first value could appear at any date.

So even if a value occurs, then a value occurs 9 months later, both those values are “new”. Only after the 12 months since the first occurrence does it become “return”.

I can do this obviously manually, but with over 2,000 rows it could take forever and lead to a lot of errors. Any solution is helpful- conditional format, formula, VBA.

WOW!! – This one was a doozie for me, but it can be done using array formulas. (I wanted to see if I could do it – The nicer solution, though, would definitely be to do this in VBA)

The way I would do it is, suppose you set up your data exactly like you have it in your sample picture – such that your `10/1/2005` data would be in column B.
So, to see how you would do it for yearly, suppose you’re working on the column for `2/1/2007` (given this example, it would be in column R.

So, your Return calculation (in cell `R6`) would look as follows:

``````=SUMPRODUCT(--(MMULT(IF(F2:Q4="",0,F2:Q4),TRANSPOSE({1,1,1,1,1,1,1,1,1,1,1,1}))>0),R2:R4)
``````

Note that THIS MUST BE ENTERED AS ARRAY FUNCTION (using ctrl + shift + Enter once entered) for this to work.

Then, your New calculation is simply the sum of this column minus your returns. So, in cell `R5`, you would have:

``````=SUM(R2:R4)-R6
``````

(Entered as a normal formula).

As a simple explanation, you’re using matrix multiplication to multiply the values in each row by 1 and summing that together on a per-row basis (The `MMULT()` part), then you’re simply examining if it returns `>0` – meaning there is at least a single value in that row for the last 12 months.

Then, if there is a value, then you add in the current month’s value to the Return sum (This is done using the `SUMPRODUCT()` function)… That gives you what you need.

The nice thing about this is that it is easily good for any number of rows of data, so your situation should be satisfied, the bad thing is that it will be fairly processor-intensive, but it will work!!

I hope this at least helps set you on your way!

(My answer copied from other question for anyone interested)

I added a helper column C that finds the first non blank in the row (my data went from column D to column AZ)

``````=MATCH(TRUE,INDEX((D2:AZ2<>0),0),0)
``````

This was then the formula I put in cell D5 and copied it across

``````=SUMIFS(D2:D4,\$C2:\$C4,">"&COLUMN(D1)-12-3)
``````

The 12 is for 12 months and the 3 is for Column C where the data starts.

The return business is in D6 and across

``````=SUM(D2:D4)-D5
``````