Applying a running count to the output from a array directly within the formula that produces the array

ok_lets

New Member
Joined
Jan 12, 2025
Messages
6
Office Version
  1. 365
Platform
  1. Windows
(One more before I quit for the day and what a gloomy day here in England)

I am counting the occurrences of items on a sheet and outputting by counts each day:

countif of unique items by date:
=COUNTIFS(Sheet22!C:C,B5#,Sheet22!A:A,Sheet21!E4:Sheet21!I4)
- b5# is list of unique items on Sheet22 column c
- sheet 21 e4:i4 is a row of dates.
- range c5:c22 is a static starting balance for each item in b5:b22

The formula above ouputs the count correctly but what I want is the rolling count i.e.
jackdaw starts at 5 and there are in the example none for each so for each date: 5, 5, 5, etc rather than 0,0,0,etc
Further down coat has 44 starting and rolling count would be: 44, 43, 43, 43, etc rather than 0,1,0,0,etc

I would like to wrap the formula above in another formula to achieve this byrow seemed most obvious but doesn't work ???

Seems tricky - I am fairly new to O365 and dynamic functions - but that's no excuse :) - I am determined to learn

Thanks in advance and thanks for looking

test.xlsm
BCDEFGHI
4new statring balance31.01.202502/02/2504/02/2505/02/2506/02/25
5jackdaw5000000
6orange7000000
7lemon8000000
8coat44101000
9jockdaw1000000
10tree3000000
11hat99000000
12treeflower11000000
13flower14000000
14river63000000
15green albatross12000000
16garage14000000
17shoes32000000
18yellow blackbird7000000
19alfa romeo18201100
20belt1101000
21red robin1000000
22house7000000
Sheet21
Cell Formulas
RangeFormula
E4E4=B3
F4F4=E4+2
G4G4=E4+4
H4:I4H4=G4+1
B5:B22B5=DROP(UNIQUE(Sheet22!C:C,,FALSE),-1)
D5:D22D5=BYROW(E5#,LAMBDA(row,SUM(row)))
E5:I22E5=COUNTIFS(Sheet22!C:C,B5#,Sheet22!A:A,Sheet21!E4:Sheet21!I4)
Dynamic array formulas.
 
How about:

ABCDEFGHIJKLMNOPQ
1
2
3
4Start31 Jan 20252 Feb 20254 Feb 20255 Feb 20256 Feb 2025
5jackdaw55555500000jackdaw
6orange77777700000orange
7lemon88888800000lemon
8coat44444343434301000coat
9jockdaw11111100000jockdaw
10tree33333300000tree
11hat99999999999900000hat
12treeflower11111111111100000treeflower
13flower14141414141400000flower
14river63636363636300000river
15green albatross12121212121200000green albatross
16garage14141414141400000garage
17shoes32323232323200000shoes
18yellow blackbird77777700000yellow blackbird
19alfa romeo18181716161601100alfa romeo
20belt11000001000belt
21red robin11111100000red robin
22house77777700000house
23
Sheet1
Cell Formulas
RangeFormula
B5:B22B5=Q5:Q22
E5:I22E5=LET(a,K5:O22,C5:C22-MAKEARRAY(ROWS(a),COLUMNS(a),LAMBDA(r,c,SUM(INDEX(a,r,SEQUENCE(c))))))
Dynamic array formulas.

You can replace my reference K5:O22 with your COUNTIFS formula.
 
Last edited:
Upvote 0
Solution
How about:

ABCDEFGHIJKLMNOPQ
1
2
3
4Start31 Jan 20252 Feb 20254 Feb 20255 Feb 20256 Feb 2025
5jackdaw55555500000jackdaw
6orange77777700000orange
7lemon88888800000lemon
8coat44444343434301000coat
9jockdaw11111100000jockdaw
10tree33333300000tree
11hat99999999999900000hat
12treeflower11111111111100000treeflower
13flower14141414141400000flower
14river63636363636300000river
15green albatross12121212121200000green albatross
16garage14141414141400000garage
17shoes32323232323200000shoes
18yellow blackbird77777700000yellow blackbird
19alfa romeo18181716161601100alfa romeo
20belt11000001000belt
21red robin11111100000red robin
22house77777700000house
23
Sheet1
Cell Formulas
RangeFormula
B5:B22B5=Q5:Q22
E5:I22E5=LET(a,K5:O22,C5:C22-MAKEARRAY(ROWS(a),COLUMNS(a),LAMBDA(r,c,SUM(INDEX(a,r,SEQUENCE(c))))))
Dynamic array formulas.

You can replace my reference K5:O22 with your COUNTIFS formula.
Brill, Stephen, brill. I delayed until I had time to have a good long look, at first I didn't quite get it completely - but do now. Is it considered bad form on this forum to ask for an explanation of an offered solution - I'm keen to fully to get to grips with the new features of O365 - dynamic arrays, spilling, let and lambda etc and am very much a novice at the moment so while the solution alone is welcome (though a lot of what I do is experimentation with the new features) a good understanding of the solution is better.

Many thanks (must sat so far I like this forum)
 
Upvote 0
Another possibility using SCAN:
Excel Formula:
=DROP(SCAN(,HSTACK(IFS({0,1},B5#:C5),COUNTIFS(Sheet22!C1:C10000,B5#,Sheet22!A1:A10000,E4:I4)),LAMBDA(a,v,IF(ISNA(a),v,a-v))),,2)

IFS({0,1},B5#:C5) returns a 2-column array: the first column is filled with #N/A errors (which is the default result for FALSE or 0 when only a single logical_test argument is provided for the IFS function); the second column contains the starting balances from column C. HSTACK appends this 2-column array to the left of the COUNTIFS results, and SCAN iterates through the entire array in row major order (left-to-right, top-to-bottom). The #N/A errors are used as the reset point for each row in the array: IF(ISNA(a),v,a-v). Lastly, DROP removes the first 2 helper columns from the final output.

Note: when using a static array such as {0,1}, the row/column separators may differ, depending on your Regional and Language Settings (locale). On my system, comma ( , ) is the column separator and semi-colon ( ; ) is the row separator. You can also replace {0,1} with HSTACK(0,1) if it's giving you grief.

Also: for performance reasons, it's not a good idea to use entire column references with functions like COUNTIFS, SUMIFS, etc., which is why I only referenced the first 10,000 rows in this example. Adjust the number of rows for each range reference to adequately accommodate the size of your dataset.
 
Upvote 0

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