Here's my way of calculating running count by groups in Sheets:
=LAMBDA(a,INDEX(if(a="",,COUNTIFS(a,a,row(a),"<="&row(a)))))(B4:B)
The complexity of this formula is R^2 = 1000000 operations for 1K rows. I'd love to make more efficient formula, and tried combinations of LABMDA and SCAN. For now I've found only the way to do it fast with 1 group at a time:
=INDEX(IF(B4:B=" Corn",SCAN(0,B4:B,LAMBDA(i,v,if(v=" Corn",i+1,i))),))
Can we do the same for all groups? Do you have an idea?
Note: the script solution would use object and hash to make it fast.
Legal Tests
We have a list of N items total with m groups. Group m(i) is a unique item which may repeat randomly. Samlpe dataset:
a
b
b
b
a
↑ Sample for 5 items total and 2 groups: N=5; m=2. Groups are "a" and "b"
The task is to find the function which will work faster for different numbers of N and m:
- Case #1. 1000+ accurances of an item from a group
m(i) - Case #2. 1000+ different groups
m - General case sagnificant number of total items
N~ 50K+
Playground
Samlpe Google Sheet with 50K rows of data. Please click on the button 'Use Tamplate':
Speed Results
Tested solutions:
Countifsfrom the question andCountifand from answer.Xlookupfrom answer- Complex
Matchlogic from answer Sortinglogic from the answer
In my enviroment, the sorting option works faster than other provided solutions. Test results are here, tested with the code from here.




