简体   繁体   中英

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

在此处输入图像描述

I have the Year, Week of Year and Day of the Week and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture

I tried many formulas, but I am sure there might be an easy one.

I found the solution:

Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)

=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6

I found this solution, but you need to do further testing if it really works.

I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)

And then make date using: =DATE(A2;C2;E2)

在此处输入图像描述

I think you are counting the weeks starting from zero because for 9/1/2022 ( YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)) . In order to use the logic to multiply the number of weeks by 7 . You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date , the Sunday of the first week of the year, not the first day of the year ( YYYY/1/1 )

Here is the approach we use in cell E2 :

=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
 fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk 
  + XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))

We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year ( fDay ) was not a Sunday:

fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))

The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1 .

Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.

excel输出

Notice that the year 2021 started on a Friday, so if we want to find a day for the first week ( 0 ) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:

=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
 result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
  + XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
 IF(YEAR(result) <> y, "ERROR: Date from previous year", result))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM