Reversed weeknumber function?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
I have like this:
Source Date: December 31st, 2020
Result: 53/2020
Formula: WEEKNUM(A1;21)&"/"&YEAR(A1)

But how about the same backwards, what would you suggest? The exact date would preferably be the particular Monday.

So input: 53/2020 (which can be parsed obviously to 53 and 2020 with a split at the slash)
should somehow calculate 44193 (which is December 28th 2020).

I was thinking of making simply a large lookup table, but I have a feeling that's unnecessary.

Oh and I need to do this without VBA, just by using Excel functions.
 
Hi Felix,

Not to step on your toes, but I realize now that what OP is looking for is to REVERSE his INPUT and OUTPUT process. I though at first when OP stated "backwards", it meant instead of current output of WW/YYYY to be YYYY/WW.


This is way above my paygrade... but I wanted to share a shorter formula I found that produces the same results as yours that I think you may like. I found it on ablebits.
Excel Formula:
=DATE(B2, 1, -2) - WEEKDAY(DATE(B2, 1, 3)) + A2 * 7
View attachment 104919
Best regards,
The input should be WW/YYYY. With backwards I mean that when original one takes A as the input and gives B as the result, the reversed version takes B as the input and gives A as the result.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Here is another option. It may need to be adjusted, depending on the system used (assuming Week 1 is the week containing January 1) and weeks begin on Sunday. Inputs need by formula in blue, output in green. I left some formulas in the sheet to show what certain parts of the formula are doing. EDIT: Based on your comment about 1/2021 ideally returning something between January 4 and January 10, I'm guessing that a different "Week 1" convention is desired (other than Week is the week containing January 1)?
MrExcel_20240112.xlsx
ABCD
1Input Date12/31/202012/16/20184/18/2013
2Fwd conversion53/202051/201816/2013
3Parse weeknum535116
4Parse year202020182013
51st Sunday of year1/5/20201/7/20181/6/2013
61st Monday of year1/6/20201/1/20181/7/2013
7Monday of Weeknum12/28/202012/17/20184/15/2013
Sheet3
Cell Formulas
RangeFormula
B2:D2B2=WEEKNUM(B1,1)&"/"&YEAR(B1)
B3:D3B3=LEFT(B2,FIND("/",B2)-1)+0
B4:D4B4=RIGHT(B2,4)+0
B5:D5B5=DATE(B4,1,8)-WEEKDAY(DATE(B4,1,7))
B6:D6B6=DATE(B4,1,8)-WEEKDAY(DATE(B4,1,6))
B7:D7B7=IF(DATE(B4,1,8)-WEEKDAY(DATE(B4,1,6))>DATE(B4,1,8)-WEEKDAY(DATE(B4,1,7)),B3-2,B3-1)*7+DATE(B4,1,8)-WEEKDAY(DATE(B4,1,6))
At least here in Finland the January 1 is not necessarily on Week 1. January 1 can be also on Week 53. That happens when the week following week 52 has its Thursday belonging to the old year (i.e. more old year days than new year days). So the week following Week 52 can be either Week 53 (Thursday belongs to the old year) or Week 1 (at least four new year days). Here's more info: ISO week date - Wikipedia

Example of 52 week year: 2023
Example of 53 week year: 2020

I will check those formulas you sent, I can see that at least you have tried, I'm very grateful for that :)
 
Upvote 0
You can test my function the other way around...
Starting from every monday you can think of, getting year and week (with WEEKNUM) and feeding those to the formula I provided, and see if you get the same date again. Just for testing purposes of course, i know you problem is different.

Like this (sorry still no desktop app with XL2BB):

1705143664478.png
 

Attachments

  • 1705143512090.png
    1705143512090.png
    35.5 KB · Views: 4
Upvote 0
Upvote 0
Thanks for the clarification about week numbering and week start conventions. I was working with a different set of assumptions. Also, thanks for updating your profile to show the latest version of Excel being used...that often makes a difference regarding which functions can be used and how the formulas can be written.

Here is an updated formula that uses input in the form of ww/yyyy, splits the text apart to determine the week number and year. Checks to see what the latest week number possible is for that year and displays an error message if the input ww exceeds the maximum possible for that year. Beyond that, the methodology is similar to what I showed before, except I'm targeting different dates to determine how to adjust the number of weeks from a baseline date. Please check carefully, but this seems to test okay for me. The single formula in the yellow-shaded cells is the only one needed for the result. The other formulas were used to return intermediate results for my reference during testing. If desired, this formula could be converted to a function that accepts a single argument (ww/yyyy).
MrExcel_20240112.xlsx
ABCDEFGHIJ
2Input ww/yyyy53/202052/201816/201352/202126/20147/20157/20161/201453/2014
3Monday of Weeknum12/28/202012/24/20184/15/201312/27/20216/23/20142/9/20152/15/201612/30/2013max wknum 52
4
5Parse weeknum535216522677153
6Parse year202020182013202120142015201620142014
71st Thursday of year1/2/20201/4/20181/3/20131/7/20211/2/20141/1/20151/7/20161/2/20141/2/2014
81st Monday of year1/6/20201/1/20181/7/20131/4/20211/6/20141/5/20151/4/20161/6/20141/6/2014
Sheet3
Cell Formulas
RangeFormula
B3:J3B3=LET(wy,B2,w,LEFT(wy,LEN(wy)-5)+0,y,RIGHT(wy,4)+0,weoy,WEEKNUM(DATE(y,12,31),21),maxw,IF(weoy=1,52,weoy),deght,DATE(y,1,8),fstTh,deght-WEEKDAY(DATE(y,1,3)),fstMo,deght-WEEKDAY(DATE(y,1,6)),IF(w>maxw,"max wknum 52",IF(fstMo>fstTh,w-2,w-1)*7+fstMo))
B5:J5B5=LEFT(B2,LEN(B2)-5)+0
B6:J6B6=RIGHT(B2,4)+0
B7:J7B7=DATE(B6,1,8)-WEEKDAY(DATE(B6,1,3))
B8:J8B8=DATE(B6,1,8)-WEEKDAY(DATE(B6,1,6))
 
Upvote 0
Another way to look at it is that if you take the formula I posted in post 7:

Excel Formula:
=LET(
y, B3,
week,C3,
firstDayOfYear, DATE(y,1,1),
firstWeeksMonday, WORKDAY.INTL(firstDayOfYear+2,-1,"0111111"),
firstWeeksMonday+7*(week-1)
)

And feed it first year 2014 and week 53, and then year 2015 week 1, it should return the same result as it is actually the same week.

1705189817087.png
 
Upvote 0
And here is the formula with the parsing:

1705190288817.png


In Cell B2 enter this formula and copy down:

Excel Formula:
=LET(wy,TEXTSPLIT(A2,"/"),
y, CHOOSECOLS(wy,2),
week, CHOOSECOLS(wy,1),
firstDayOfYear, DATE(y,1,1),
firstWeeksMonday, WORKDAY.INTL(firstDayOfYear+2,-1,"0111111"),
firstWeeksMonday+7*(week-1)
)
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top