Search code examples
google-sheets

Find number of rows between current cell and last cell with matching value in a column


I have a Google Sheet assigning shifts to employees:

10/1/2023   Sunday      Smith
10/2/2023   Monday      Baker
10/3/2023   Tuesday     Johnson
10/4/2023   Wednesday   Smith
10/5/2023   Thursday    Johnson
10/6/2023   Friday      Smith
10/7/2023   Saturday    Baker
10/8/2023   Sunday      Johnson
10/9/2023   Monday      Johnson
10/10/2023  Tuesday     Smith
10/11/2023  Wednesday   Baker
10/12/2023  Thursday    Baker
10/13/2023  Friday      Johnson

I would like to add an additional column to the right that displays a number representing the number of days since last shift.

The first row would be 0 of course because Smith has not had a shift yet. But next to Smith on 10/4/2023 there should be a 3.

Similary, for Baker on 10/2/2023, this would be 0 because Baker has not had a shift yet. But next to Baker on 10/7/2023 there should be a 5.


Solution

  • Here you have an option that looks row by row to the value in reverse order with XMATCH:

    =BYROW(C1:C,LAMBDA(each,IF(each="","",IFERROR(ROW(each)-XMATCH(each,C1:OFFSET(each,-1,0),0,-1),0))))
    

    enter image description here