Create Year-Based Sequence Numbers That Stay with Rows in Excel
Question details
Create sequence numbers that restart each year and remain associated with the correct records when sorted.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet contains dated records that need a chronological sequence within each year.
- Observed behavior
- Each record should receive a year-specific sequence number based on its complete timestamp, including after the records are sorted.
Ensure the timestamps are real Excel date-and-time values, not text, and that each record has the correct year. Add a unique numeric ID for each record to break ties when timestamps match.
Calculate a chronological sequence for each year
Use COUNTIFS with the full timestamp and a unique ID so the sequence does not depend on the current row order.
This example uses timestamps in column A, years in column B, unique numeric IDs in column C, and sequence numbers in column D. Adjust every $2:$1000 range to cover all your records.
In cell D2, enter =COUNTIFS($B$2:$B$1000,B2,$A$2:$A$1000,"<"&A2)+COUNTIFS($B$2:$B$1000,B2,$A$2:$A$1000,A2,$C$2:$C$1000,"<="&C2).
Drag the fill handle from D2 down through the last record. Each year starts at 1, and records with matching timestamps are ordered by their numeric IDs.
Select the entire data range, including the sequence column, and use Data > Sort to sort by timestamp or another field. Keep all columns together when prompted.

Freeze generated sequence numbers as values
Use this after calculating the sequence if each assigned number must remain fixed.
Create year-based sequences in WPS Spreadsheets
Use COUNTIFS in WPS Spreadsheets to number records by year and timestamp, then sort the complete table or paste the results as values.
- 1. Open the workbook: Open your Excel workbook in WPS Spreadsheets and arrange timestamps, years, and unique numeric IDs in columns A, B, and C.
- 2. Enter the formula: In D2, enter =COUNTIFS($B$2:$B$1000,B2,$A$2:$A$1000,"<"&A2)+COUNTIFS($B$2:$B$1000,B2,$A$2:$A$1000,A2,$C$2:$C$1000,"<="&C2), adjusting the ranges to cover your data.
- 3. Fill and sort: Drag the fill handle down the sequence column, then select the entire table and use Data > Sort to sort the records.
- 4. Fix numbers if needed: Copy the sequence cells and use Paste Special > Values to make the assigned numbers permanent.

Frequently Asked Questions
Why do records with the same timestamp need an ID?
A timestamp alone cannot determine which matching record comes first. Give each record a unique numeric ID in column C so the formula can use it as a tie-breaker.
Can I sort by another column without losing the sequence?
Yes. Select the entire data range, including the sequence column, before using Data > Sort. Do not sort the sequence column by itself.
Will the numbers change when I add a record?
Formula results can change if a new record falls earlier in the same year's chronological order. Copy the sequence cells and use Paste Special > Values if the assigned numbers must stay fixed.
How do I create the year column from a timestamp?
If the timestamp is in A2, enter =YEAR(A2) in B2 and fill the formula down. The timestamp must be a valid Excel date-and-time value.




