google-sheetsindex-match

Complex Shifts Schedule Formula


I'm trying to build a shift scheduler system. Here are the data & conditions:

I have 3 patterns of shifts. All the employees work on Mondays. But when it comes to their 2 days off, they progress from one week to another.

Week 1: Tu & We (Days off)
Week 2: Tu & Fr (Days off)
Week 3: Sa & Su (Days off)
Week 4: loop (like Week 1)
Week 5: loop (like Week 2) etc...

Here's the complete map of the patterns:

Pattern 1:
Week 1: Mo (work) | Tu & We (Days Off) | Th & Fr & Sa & Su (Work)
Week 2: Mo (work) | Tu & We (Work) | Th & Fr (Days off) | Sa & Su (Work)
Week 3: Mo (work) | Tu & We & Th & Fr (Work) | Sa & Su (Days off)

Pattern 2:
Week 1: Mo (work) | Tu & We (Work) | Th & Fr (Days off) | Sa & Su (Work)
Week 2: Mo (work) | Tu & We & Th & Fr (Work) | Sa & Su (Days off)
Week 3: Mo (work) | Tu & We (Days Off) | Th & Fr & Sa & Su (Work)

Pattern 3:
Week 1: Mo (work) | Tu & We & Th & Fr (Work) | Sa & Su (Days off)
Week 2: Mo (work) | Tu & We (Days Off) | Th & Fr & Sa & Su (Work)
Week 3: Mo (work) | Tu & We (Work) | Th & Fr (Days off) | Sa & Su (Work)

Besides this, I have a select Date where I can select a custom Start Date, and a list from the range to select the desired pattern code for each employee.

Here's what should happen:

  1. I set a custom Start Date

  2. I select a specific pattern code for an employee

  3. GSheets should compare the chosen pattern code with patterns table

  4. GSheets should then pick the day of the custom start date I set (eg: Mo) and compare it with the table of pattern (according to the pattern chosen)

  5. Here's a little catch, it must pick the first occurrence of Mo, then when 7 days goes, it should pick the 2nd occurrence, same when it comes to the 3rd occurrence, for the 4th week, it should go back to the 1st occurrence, and so on for the rest upcoming weeks.

What I've done so far is I was able to:

But I couldn't give any further than this because I don't how to make the formula loop when the pattern ends.

=IFERROR(INDEX(D10:X12,MATCH(B19,C10:C12,0),MATCH(D17,D9:X9,0)),"")

Please check this sample link. I've created my desired scenario manually vs. the scenario I tried with a detailed explanation:

Link: https://docs.google.com/spreadsheets/d/1SwD6tiVNfaaYKirf69SwWwbOlGukTP52FIQiKqckjW4/edit?usp=sharing

The main problem is that the formula I used doesn't take into consideration the day of the selected Date as a starting point, and it just goes to the very first day of the pattern table and starts populating from there.

Thanks in advance!


Solution

  • Added formula to your sheet here, Please do test it out:

    =let(Σ,filter({D10:X12,D10:X12},C10:C12=B23),Λ,xmatch(left(text(N5,"ddd"),2),{D9:X9,D9:X9}),
         choosecols(Σ,sequence(1,28,Λ,1)))
    

    enter image description here

    {D10:X12,D10:X12} arranging 2 side by side pattern arrays for loop to go through

    filter({D10:X12,D10:X12},C10:C12=B23) filter out the pattern row specified for the employee

    left(text(N5,"ddd"),2) get the first 2 weekday alphabets from date in Cell_N5

    xmatch(left(text(N5,"ddd"),2),{D9:X9,D9:X9}) searches for Su in pattern header row and receives an output of 7 since thats the first match value it finds

    choosecols(Σ,sequence(1,28,Λ,1)) In this step I'm saying the output needs to be for 28 columns starting from Column 7. You can change the 28 part here if you need more days to loop through. max is 42 for this current formula iteration (since we used 2 hstacked arrays {D10:X12,D10:X12}); if it needs to go for >42 then horizontal stack more alike {D10:X12,D10:X12,D10:X12} and so on...