0

I am working on redoing a Google Sheets spreadsheet my wife uses for work. Each sheet has a section for Monday, Tues, Wed, Thurs, and Sat (no programs on Friday and Sunday). Starting with early next year, the first sheet has Monday, January 2, Tuesday, January 3, and so on through Sunday, January 7.

enter image description here

What I am trying to do (instead of manually editing 52 different sheets) is come up with a function that iterates so that the second weeks' spreadsheet will show Monday, January 9, Tuesday, January 10, and so on. Each week would iterate the previous sheets corresponding day by 7.

Now, this is easy enough with a simple function, but where it gets complicated is when we get to the end of the month. I want to see if it is possible to iterate months as well. So, the week towards the end of the month would have Monday 29, Tuesday 30, Wednesday 31, but then Thursday Feb 1, and Saturday Feb 3.

Any ideas?

1
  • can you share a copy / sample of your sheet? Commented Dec 9, 2022 at 21:47

1 Answer 1

1

You can use this formula to create a list of dates in the format you want of a whole year (365 days):

=ArrayFormula(LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
 LAMBDA(DATES,
  {DATES,BYROW(DATES,LAMBDA(DATE,
   {
    INDEX(WEEKS,WEEKDAY(DATE)),
    INDEX(MONTHS,MONTH(DATE)),
    DAY(DATE),
    INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
   }
  ))}
 )(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
)(
 DATE(2023,1,1),
 {"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
 {"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
))

Result:

image


Which you can than use a QUERY() to easily remove what you don't need, such as:

(Assume the list of dates are placed in A:E)

=QUERY({$A:$E},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday'",0)

result in something like this:

image2


You can also use limit and offset to define the range to be shown in a QUERY(), such as:

(Assume the result of the last query is placed in F:F)

=QUERY(F1:F,"LIMIT 5 OFFSET "&((1-1)*5))

results:

image3

With this formula, you only need to change the number x in ((x-1)*5) to the week number you want, and that will returns you the 5 days in the format you requested.


You can combine everything above to form something like this:

=ArrayFormula(
LAMBDA(SHOWWEEK,
 LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
  LAMBDA(DATES,
   LAMBDA(DATA,
    LAMBDA(RESULTS,
     RESULTS
    )(QUERY({DATA},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday' LIMIT 5 OFFSET "&((SHOWWEEK-1)*5),0))
   )(
    {DATES,BYROW(DATES,LAMBDA(DATE,
     {
      INDEX(WEEKS,WEEKDAY(DATE)),
      INDEX(MONTHS,MONTH(DATE)),
      DAY(DATE),
      INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
     }
    ))}
   )
  )(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
 )(
  DATE(2023,1,1),
  {"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
  {"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
 )
)(1)
)

in which all you need to do is changing the number inside the last () to the number of week you want, the 5 dates in the format you requested will be returned.

don't forget that you can always use INDEX() to define which value of an array do you want to return for your final result.


Since you have mentioned that What I am trying to do (instead of manually editing 52 different sheets) is come up with a function that iterates so that...,

We can go one step further, add a simple apps-script to get the sheet number count, we assume that your file only have 52 sheets, 1 for a week, and the sheets are arranged in ascending order (otherwise you will need another method), add this script into your spreadsheet:

function getSheetNum() {
  const sss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (const [index,sheet] of sheets.entries()) {
    if(sheet.getName() === sss.getActiveSheet().getName()) return index;
  }
}

This function returns the index of sheet you are currently working on, start from 0, you can call it from your spreadsheet by simply type in =getSheetNum() in any cell.

p.s. if you have no idea what is apps-script, you may want to read some documentation: https://developers.google.com/apps-script/overview

Combining this custom function with our formula, like this:

=ArrayFormula(
LAMBDA(SHOWWEEK,
 LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
  LAMBDA(DATES,
   LAMBDA(DATA,
    LAMBDA(DATA,
     DATA
    )(QUERY({DATA},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday' LIMIT 5 OFFSET "&(SHOWWEEK*5),0))
   )(
    {DATES,BYROW(DATES,LAMBDA(DATE,
     {
      INDEX(WEEKS,WEEKDAY(DATE)),
      INDEX(MONTHS,MONTH(DATE)),
      DAY(DATE),
      INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
     }
    ))}
   )
  )(BYROW(SEQUENCE(365,1,0),LAMBDA(ROW,FIRSTOFYEAR+ROW)))
 )(
  DATE(2023,1,1),
  {"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
  {"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
 )
)(GETSHEETNUM())
)

be noticed that, instead of ((SHOWWEEK-1)*5), we do (SHOWWEEK*5) in this version, because the custom function we created using Zero-based array indexing.

results:

image4

image5

image6

It will return a different week of dates in format you requested in each sheet.


Update 2022-12-30:

While I don't really understand why your week 1 start from 2nd of Jan and last for 7 days, is it 1st of Jan considered as Week 0 in that case??

Anyway, I come up with this formula which gives you a list of week count with date range for your reference.

According to the comments, if all you need is the week count of the year, you can simply use WEEKNUM(DATE):

=ArrayFormula(
LAMBDA(FIRSTOFYEAR,
 LAMBDA(DATES,
  LAMBDA(WEEKS,
   LAMBDA(DATA,
    BYROW(ARRAY_CONSTRAIN(DATA,MAX(WEEKS),4),LAMBDA(ROW,JOIN(" ",ROW)))
   )(TO_TEXT(QUERY(QUERY({WEEKS,"Week "&WEEKS&":",DATES,DATES},"SELECT Col2,MIN(Col3),'~',MAX(Col4),Col1 GROUP BY Col1,Col2",0),"OFFSET 1 FORMAT Col2'm/d',Col4'm/d'",0)))
  )(WEEKNUM(DATES))
 )(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
)(DATE(2023,1,1))
)

The formula works in the same concept as my other formulas.

Sign up to request clarification or add additional context in comments.

10 Comments

this is awesome! Thanks! Looking at the original formula, is there a way to also output the week number? Use-case, she wants a cell on the first sheet that is "Week 1 : 1/2 - 1/8". So I would assume that the week number could be generated with that first formula, and then I'd probably have to tweak one of your other formula's to output the date range of the week..
sure you can do that, inside the 1st formula, the DATE variable is actually a date format which you can use any kind of date function to work with. In the example, I used INDEX(WEEKS,WEEKDAY(DATE)), INDEX(MONTHS,MONTH(DATE)), and DAY(DATE), which combined with the WEEKS and MONTHS variables created in the outer LAMBDA(), the function is able to return the full name of weeks and months. But if all you need is the week count of the year, you can simply use WEEKNUM(DATE), it returns the number of week count of the year of the given date.
Awesome! So while traditional calendars have Sunday as the first day of the week, many companies (including my wife's) use Monday as the first day of the week and Sunday as the final. Thus the reason for 'Week 1: 1/2 - 1/8'. So using your newest formula above, I changed to ....BY Col1,Col2",1),....
Although on second thought that still doesn't get me where I need to be. It gives the list of week numbers correctly, but I still need to figure out how to get the week number based on the sheet number (much like you did with the dates in the OP)
According to my knowledge, if you Count Mon as the 1st day of a week, and when the 1st of Jan is not Mon, I will consider that this year has a short week 1, like in 2023, since 1/1 is Sun, shouldn’t the 1st week of 2023 be considered as 1/1 ~ 1/1 which last only 1 day? Since Sun is the end of a week afterall… Well, that is not the main issue though. Anyway there are so many way you will be able to get the week count of each week by the original formula and functions I’ve provided.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.