FormulaCraft

How to number rows within each group in Excel and Google Sheets

Topic:Data cleaning & shaping
Excel & Google Sheets
=COUNTIF($A$2:A2,A2)

Verified example

Computed by a real spreadsheet engine on the sample data below.

GroupRow_Num
Sales
Sales
HR

=COUNTIF($A$2:A2,A2)1

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1Ensure your data has a group column (e.g., department names in column A) with rows sorted by group.
  2. 2In column B row 2, enter =COUNTIF($A$2:A2,A2) — the first reference is anchored ($A$2) while the second expands (A2) as you copy down.
  3. 3Copy the formula down column B — it counts how many times the current group value has appeared so far, giving each row its position within the group.
  4. 4Sort your data by the group column first for clean sequential numbering (otherwise the count resets whenever the same group reappears).

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

How do I number rows within groups without sorting the data first?

Use =COUNTIFS($A$2:A2,A2) which has the same behavior — for truly unsorted data you need a helper sort or a more complex SUMPRODUCT formula.

Can I use this to restart numbering with each new month or date?

Yes — replace the group column reference with a month/year expression: =COUNTIFS($B$2:B2,B2) where B holds MONTH(date) or TEXT(date,"YYYY-MM").

More on Data cleaning & shaping

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: