简体   繁体   English

周数和年份组合

[英]week number and year combination

Several questions have been asked in the past on Weeknumber and Year in Excel, however, I have a specific question that I couldn't find the answer to.过去在 Excel 的 Weeknumber 和 Year 上提出了几个问题,但是,我有一个具体的问题我找不到答案。

I'm trying to get a combination of year and week number in the format of YYWW, so for example 1752, or 1801, based on a column with dates, something like this:我正在尝试获取 YYWW 格式的年份和周数的组合,例如 1752 或 1801,基于带有日期的列,如下所示:

Date         Year  Week
01-01-2018   18    01
02-01-2018   18    01
...
31-12-2018   18    01

Year is simply determined by using the YEAR function on the date, and the week number is determined using the ISOWEEKNUM function on the date.年份只需使用日期上的YEAR function 确定,星期数使用日期上的ISOWEEKNUM function 确定。 This is then followed by some further processing, summing data based on year and week number, and the issue is that then also the data of the 31st of December 2018 is taken into account for week 1 of 2018, while the 31st of December is obviously not in week 1 of 2018, but in week 1 of 2019.然后是一些进一步的处理,根据年份和周数对数据求和,问题是 2018 年第 1 周也考虑了 2018 年 12 月 31 日的数据,而 12 月 31 日显然是不是在 2018 年的第 1 周,而是在 2019 年的第 1 周。

So the question is, how could I "fix" this, so how can I get Excel to give me 19 & 01 for 31-12-2018?所以问题是,我怎样才能“解决”这个问题,那么我怎样才能让 Excel 在 2018 年 12 月 31 日给我 19 和 01? Ideally I am looking for a solution that does not involve any VBA.理想情况下,我正在寻找不涉及任何 VBA 的解决方案。

Any help is much appreciated!任何帮助深表感谢!

For ISO week numbers the Thursday of each week will always be within the “isoweekyear”, so for any date, to get the correct year it's sufficient to find the year of that week's Thursday. 对于ISO周数,每周的星期四始终位于“等周年”内,因此对于任何日期,要获取正确的年份,只需找到该周星期四的年份即可。 You can do that like this: 您可以这样做:

=YEAR(A1+3-WEEKDAY(A1;3))

So in the end, thanks to the feedback of Nick and ImaginaryHuman072889, I fixed it like this: 因此,最后,由于尼克和ImaginaryHuman072889的反馈,我像这样修复了它:

=YEAR(A1)+IF(AND(MONTH(A1)=12;ISOWEEKNUM(A1)=1);1;0)+IF(AND(MONTH(A1)=1;ISOWEEKNUM>=52);-1;0)

I preferred not to nest the IF statements for readability and because they will never result in a TRUE at the same time anyway. 我宁愿不嵌套IF语句以提高可读性,因为无论如何它们永远不会同时导致TRUE

Just a couple of other options that worked for me:只有其他几个对我有用的选项:

=YEAR(A1)+AND(MONTH(A1)=12;ISOWEEKNUM(A1)=1)-AND(MONTH(A1)=1;ISOWEEKNUM>=52) =YEAR(A1)+AND(MONTH(A1)=12;ISOWEEKNUM(A1)=1)-AND(MONTH(A1)=1;ISOWEEKNUM>=52)

=YEAR(A1)+SWITCH(MONTH(A1)&ISOWEEKNUM(A1);"121";1;"152";-1;"153";-1;0) =YEAR(A1)+SWITCH(MONTH(A1)&ISOWEEKNUM(A1);"121";1;"152";-1;"153";-1;0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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