## Explanation

In this example, the goal is to calculate the previous Sunday based on any given date. At a high level, this means we need to subtract some number of days from the given date. For example, if the given date is a Monday, we need to subtract 1 day. If the given date is a Tuesday, we need to subtract 2 days, and so on. The main challenge is to figure out how many days to subtract and for this, we use the WEEKDAY function.

*Note: the formulas below use simple math to adjust the date. This works because Excel dates are large serial numbers.*

### WEEKDAY solution

The WEEKDAY function accepts a date and returns a number between 1-7 representing the day of week. By default, WEEKDAY returns 1 for Sunday and 7 for Saturday, but this is configurable, based on a second argument called *return_type*. In this example, because we want to roll back the date to the previous Sunday, we want to set WEEKDAY to return 1 for Monday and 7 for Sunday. To do this, we can use either 2 or 11 for return_type:

```
=WEEKDAY(A1,2) // returns 1 for Monday
=WEEKDAY(A1,11) // returns 1 for Monday
```

Now that we have WEEKDAY returning 1 for Monday and 7 for Sunday, we can simply subtract the result from the given date:

```
=B5-WEEKDAY(B5,11)
```

If the date is a Sunday, WEEKDAY will return 7. Subtracting 7 from the original date in B5 will return the previous Sunday. If the date is a Tuesday, weekday will return 2, moving the date back 2 days to Sunday. The formula works the same way for each day of the week.

### Other previous weekdays

In the above formula, you might wonder why we're using 11 for *return_type* instead of 2? This is done for consistency To adapt the formula to return different previous weekdays, *return_type *needs to be adjusted Using 11 allows the different values to "line up" logically, as seen below:

```
=A1-WEEKDAY(A1,11) // previous Sunday
=A1-WEEKDAY(A1,12) // previous Monday
=A1-WEEKDAY(A1,13) // previous Tuesday
=A1-WEEKDAY(A1,14) // previous Wednesday
=A1-WEEKDAY(A1,15) // previous Thursday
=A1-WEEKDAY(A1,16) // previous Friday
=A1-WEEKDAY(A1,17) // previous Saturday
```

To be clear, using 2 instead of 11 for the previous Sunday will work just fine.

### CHOOSE function alternative

One feature of all formulas above is that they subtract days even when the given date is already the target weekday. In other words, if the date is already a Sunday, the result is the Sunday seven days prior. This isn't always desired behavior. Sometimes, the goal is to leave the date alone if it's already the right day of week. One way to accomplish this is with the CHOOSE function.

The CHOOSE function is used to select values by numeric position. For example, if we have the colors "red", "blue", and "green", we can use CHOOSE like this:

```
=CHOOSE(1,"red", "blue", "green") // returns "red"
=CHOOSE(2,"red", "blue", "green") // returns "blue"
=CHOOSE(3,"red", "blue", "green") // returns "green"
```

The first argument is called *index_num* and specified the index of the value to return. We can combine CHOOSE and WEEKDAY to give us the correct "roll back" number like this:

```
=CHOOSE(WEEKDAY(date),0,1,2,3,4,5,6) // rollback calculation
```

Here, the *index_num* argument is provided by the WEEKDAY function. Note WEEKDAY is in its default mode (no *return_type* provided), which returns 1 for Sunday and 7 for Saturday. The values that follow WEEKDAY are the actual rollback numbers. The first value is zero, which is the number of days to roll back if the date is already a Sunday. Putting the entire formula together, we get:

```
=B5-CHOOSE(WEEKDAY(B5),0,1,2,3,4,5,6) // previous Sunday
```

This formula will return the previous Sunday unless the date is already a Sunday. In that case, CHOOSE will return zero and the formula itself will return the original date unchanged. To extend this idea to handle other days of the week, you can adjust the rollback numbers as needed for each day of the week like this:

```
=date-CHOOSE(WEEKDAY(date),0,1,2,3,4,5,6) // prev Sun
=date-CHOOSE(WEEKDAY(date),6,0,1,2,3,4,5) // prev Mon
=date-CHOOSE(WEEKDAY(date),5,6,0,1,2,3,4) // prev Tue
=date-CHOOSE(WEEKDAY(date),4,5,6,0,1,2,3) // prev Wed
=date-CHOOSE(WEEKDAY(date),3,4,5,6,0,1,2) // prev Thu
=date-CHOOSE(WEEKDAY(date),2,3,4,5,6,0,1) // prev Fri
=date-CHOOSE(WEEKDAY(date),1,2,3,4,5,6,0) // prev Sat
```

Alternately, you can adjust the formula to use a *return_type* argument in WEEKDAY and leave the rollback values alone:

```
=date-CHOOSE(WEEKDAY(date,17),0,1,2,3,4,5,6) // prev Sun
=date-CHOOSE(WEEKDAY(date,11),0,1,2,3,4,5,6) // prev Mon
=date-CHOOSE(WEEKDAY(date,12),0,1,2,3,4,5,6) // prev Tue
=date-CHOOSE(WEEKDAY(date,13),0,1,2,3,4,5,6) // prev Wed
=date-CHOOSE(WEEKDAY(date,14),0,1,2,3,4,5,6) // prev Thu
=date-CHOOSE(WEEKDAY(date,15),0,1,2,3,4,5,6) // prev Fri
=date-CHOOSE(WEEKDAY(date,16),0,1,2,3,4,5,6) // prev Sat
```

Again, the behavior of these formulas is the same as the original formula above except the rollback *does not occur* if the given date is *already* the target day of week.

### Custom number formatting

All dates in the example shown use the following custom number format:

```
ddd d-mmm-yy
```

This number format displays a weekday abbreviation plus a date to make it easier to check results at a glance. The underlying date is unchanged.