Week Of ... : How do I get the starting Monday (or Sunday) for a given day ?

 


There is a fairly common scenario where we need report weekly progress on events : it could be weekly sales or weekly registrations or audience growth, etc. 

Your spreadsheet typically contains daily numbers Imagine you have events (eg "orders") on a given date but you want to aggregate those events by week. 

We could use the weeknum() function which will return a number between 1 and 52, but it's not super intuitive (when exactly is week number 27?). 

A more intuitive and common option is aggregate daily events into "week starting" dates and there's an easy formula for that : 

With your source date on cell A2 : 

=A2-WEEKDAY(A2,3) (week starting Monday)

=A2-(WEEKDAY(A2,1)-1) (week starting Sunday)


How does this work ?

Most spreadsheets allow you to add / subtract any number of days to / from a date to obtain a new date. Try this in the formula bar :  ="25/1/2021" + 1.  The end result should be 26/1/2021. 

The weekday(date, type) function gives you a "date id", 1 to 7 or 0 to 6 depending on the type (a value between 1 and 3).  Knowing the day id helps you make a simple calculation to find the first day of the week.


Weeks starting on Monday
If you set the type to 3 on your weekday() function, then Monday is the first day of the week and its id is 0. The calculation is then super simple (date in non-US format) :

  • Tuesday Feb 9, 2021 is day id 1, if I type in the formula bar "9/02/2021" - 1  =  Mon, Feb 8 2021
  • Thursday Feb 18, 2021 is day id 3, "18/02/2021" - 3 = Mon, Feb 15 2021
  • Automatically : =A2 - weekday(A2,3) with A2 being 18 Feb 2021.

Weeks starting on Sunday (US-style)

In order to have weeks starting on Sundays, you need to set the type of the weekday() to 1. 

This is the default value, so you can actually skip adding the type entirely, but because we are at day id 1, we need to subtract -1 from our calculation. 

Example
This is Thursday, February 4, Thursday is day id 5. In order to get to Sunday, we need to subtract 4, in other words the current day id minus 1. 

  • cell A2 = Thursday Feb 18, 2021 
  • Thursday is a US Day # 5
  • Sunday 31st Jan =A2 - (weekday(A2,1) - 1)   or simply 
  • Sunday 31st Jan =A2 - (weekday(A2) - 1)  because type=1 is the default value


Have fun with spreadsheets !




Comments

Popular posts from this blog

How to calculate the quarter for a given date in Google Sheets

How to sort pivot data in descending order in Google Sheets (Part 1)

How to sort pivot data in descending order in Google Sheets (Part 2)