This will give you an incrementing number with the behaviour you want, assuming ID1 is an incrementing number. If its not you will need some other way to order the data since order is inherent to the behaviour you want.
Here I have assumed we can order by ID1 since its not used anywhere else in the logic
create table #t
(
ID1 INT,
ID2 INT,
ID3 INT
)
insert into #t(ID1, ID2, ID3) values(1,1,1),(2,1,1),(3,2,1),(4,2,31),(5,2,1),(6,2,1),(7,2,23),(8,2,31);
with c1 as
(
select ID1, ID2, ID3,
case when ID3 != 1 or lag(ID3,1,2) over (order by ID1) != 1 then 1 else 0 end as IncrementHere
from #t
)
select ID1, ID2, ID3, sum(IncrementHere) over (order by ID1 rows unbounded preceding) as IncrementingNumber
from c1
sql-server-2014. Differences in syntax and features often affect the answers.