Increment count in column based on value in column

2.7k views Asked by At

I've 2 columns A and B. A contains names and B contains the count of those names till that record as shown below.

-----------------------------------
      |      A      |      B      |
-----------------------------------
1     | Fruits      |      1      |
2     | Flowers     |      1      |
3     | Fruits      |      2      |

So, want to have a formula for this. Expecting an array formula. Even if an array formula is not possible, a general formula

Attached a spreadsheet so that it can be explained better.

https://docs.google.com/spreadsheets/d/1wlWqdFwgv90s50iP-bXXBHciyualohj610qFiSatcmQ/edit#gid=1997586177

2

There are 2 answers

0
JPV On BEST ANSWER

In a google spreadsheet you may want to try:

=ArrayFormula(iferror(SORT(ROW(A1:A),SORT(ROW(A1:A),A1:A,1),1)-MATCH(A1:A,SORT(A1:A),0)-ROW()+2))

Example sheet

0
nwhaught On

You do not need an array formula, and I would avoid them when possible. You can accomplish your task with

=COUNTIF(A$1:A1,A1)

Where A1 if the first value in the column of values you want to count. The $ allows you to anchor the top of your COUNTIF range while leaving the bottom dynamic.