How to calculate a moving count?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
As part of my moving sums and averages project described here:


I also need to calculate a moving count of cells containing numbers. My first try was with CountIfs, but I discovered here that CountIfs cannot be used with IsNumber and to use SumProduct:


I tried using SumProduct, as shown in the Count column in the minisheet below, but it isn't working. I can get what I want with two columns, IsNumber and Countx, but would prefer a single column. Is there a formula for the Count column that will get the result in the Countx column without needing the IsNumber column?

Moving Totals.xlsx
BCDEF
25Number of rows
3
4#ValueIsNumberCountxCount
515110
628120
73 020
842130
956140
106 030
1179130
1283140
139 030
14104130
Sheet2
Cell Formulas
RangeFormula
D5:D14D5=ISNUMBER([@Value])+0
E5:E14E5=SUM(TAKE(Table2[[#Headers],[IsNumber]]:[@IsNumber],-NumRows))
F5:F14F5=SUMPRODUCT(ISNUMBER(TAKE(Table2[[#Headers],[Value]]:[@Value],-NumRows)))
B5:B14B5=ROW([@['#]])-ROW(Table2[[#Headers],['#]])
Named Ranges
NameRefers ToCells
NumRows=Sheet2!$C$2E5:F14
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You can do it that way, but you'll need to convert the TRUE/FALSE values from ISNUMBER to 1's and 0's:
Excel Formula:
=SUMPRODUCT(--ISNUMBER(TAKE(Table2[[#Headers],[Value]]:[@Value],-NumRows)))
...and then, SUM is shorter.
Excel Formula:
=SUM(--ISNUMBER(TAKE(Table2[[#Headers],[Value]]:[@Value],-NumRows)))
 
Upvote 0
Solution
You can do it that way, but you'll need to convert the TRUE/FALSE values from ISNUMBER to 1's and 0's:
Excel Formula:
=SUMPRODUCT(--ISNUMBER(TAKE(Table2[[#Headers],[Value]]:[@Value],-NumRows)))
...and then, SUM is shorter.
Excel Formula:
=SUM(--ISNUMBER(TAKE(Table2[[#Headers],[Value]]:[@Value],-NumRows)))
Dang! I thought I tried that. Thanks
 
Upvote 0
Hmmm... I guess I still don't get it. I added a Time column (C) with some missing times. I want to count how many missing times there are in the moving range. In the minisheet below, the Bad column (H) is off by +1 for the first n-1 values, where n is the length of the moving tally (NumRows). But the Good column (I), which is just the sum of the Not a Number column (J), is correct for all values of NumRows.

If I set NumRows to 1, everything is correct:

1713164114173.png


If I set it to 2, H7 is off by +1; the rest of H is correct.

1713164189632.png


If I set it to 3, H7 & H8 are off by +1; the rest of H is correct.

What am I doing wrong now?

Moving Totals.xlsx
BCDEFGHIJ
4 3Number of rows
5CDEFGHIJ
6DateTimeValueSumAverage#Missing Times Bad#Missing Times GoodTime Not a Number
771/01/242:51p222.00100
881/02/24463.00211
991/03/2410:25a393.00110
10101/04/245124.00221
11111/05/248:15a193.00110
12121/06/2410:32a062.00110
13131/07/249:46a231.00000
14141/08/249:29a793.00000
15151/09/243124.00111
16161/10/244:24p2124.00110
Sheet1
Cell Formulas
RangeFormula
F7:F16F7=SUM(TAKE(Table1[[#Headers],[Value]]:[@Value],-NumRows))
G7:G16G7=AVERAGE(TAKE(Table1[[#Headers],[Value]]:[@Value],-NumRows))
H7:H16H7=SUM(1-ISNUMBER(TAKE(Table1[[#Headers],[Time]]:[@Time],-NumRows)))
I7:I16I7=SUM(TAKE(Table1[[#Headers],[Time Not a Number]]:[@[Time Not a Number]],-NumRows))
J7:J16J7=1-ISNUMBER([@Time])
B7:B16B7=ROW()
Named Ranges
NameRefers ToCells
NumRows=Sheet1!$D$4F7:I16
 
Upvote 0
Can't you just use COUNTBLANK?

Excel Formula:
=COUNTBLANK(TAKE(Table1[[#Headers],[Time]]:[@Time],-NumRows))
 
Upvote 0
I agree with Rory's suggestion. It sounds as if you don't want to count numbers, but blanks. Your original formula includes the column heading, so it will be a factor until you move down the column far enough such that -NumRows no longer reaches back far enough to include it. When the header is included, ISNUMBER evaluates it as FALSE (converted to 0), just like any of the blank cells. And if you are using this formula to count blank cells, then the count will be too high by 1 in some cases due to this bad assumption that ISNUMBER=FALSE means blank (because it doesn't when the header is considered). It is better to count the blanks directly. Assuming the cells are truly blank, check whether Rory's suggestion works okay.
 
Upvote 0
Can't you just use COUNTBLANK?

Excel Formula:
=COUNTBLANK(TAKE(Table1[[#Headers],[Time]]:[@Time],-NumRows))
That seems to work. Thanks.

The reason I was counting "not numbers" is to make sure that a cell that looks empty, but contains one or more spaces, doesn't screw up the count.
 
Upvote 0
I agree with Rory's suggestion. It sounds as if you don't want to count numbers, but blanks. Your original formula includes the column heading, so it will be a factor until you move down the column far enough such that -NumRows no longer reaches back far enough to include it. When the header is included, ISNUMBER evaluates it as FALSE (converted to 0), just like any of the blank cells. And if you are using this formula to count blank cells, then the count will be too high by 1 in some cases due to this bad assumption that ISNUMBER=FALSE means blank (because it doesn't when the header is considered). It is better to count the blanks directly. Assuming the cells are truly blank, check whether Rory's suggestion works okay.
Aha! I should have thought of that. I guess I assumed that the range was starting with the first cell after the header.

Is there any way to easily specify that first cell without using Offset? As I replied to Rory, I would like to keep cells that look empty, but are not, from screwing up the count.

Thanks
 
Upvote 0
I see...maybe you do want ISNUMBER then, as you won't necessarily know whether apparent blanks are true blanks, or spaces, or something that you can't see. You could revert to the ISNUMBER formula, and because you have an official table, you'd like to exclude the first header row from consideration. An easy option to do that is to wrap the range selection in DROP, and then drop the first row...and then look back NumRows for the values to consider:
Excel Formula:
=SUM(--ISNUMBER(TAKE(DROP(Table2[[#Headers],[Value]]:[@Value],1),-NumRows)))
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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