How to generate the correct week number for every year

Hi Guys,

Does anyone know how to generate the correct week numbers across different years?
An example would be that week 53 in 2020 spans from 28 Dec 2020 - 3 Jan 2021.

Extracting date & time fields would give you week 53 between 28 Dec - 31 Dec 2020 and week 1 from 1 Jan 2021 - 3 Jan 2021 in my example. The epoch calendar(Week Numbers for 2020) would give you week 1 as 4 Jan 2021 - 10 Jan 2021.

Knime presents 4 Jan 2021 - 10 Jan 2021 as week 2 which is incorrect. Any ideas on how to align these with the correct numbers without manual manipulation for each year individually?

Thanks in advance.

Hi @Albert123

Maybe the following link is the solution:

Hope it helps.

Best

Ael

Hi @aworker,

Thanks, but this just gets us to the extract date&time node which I know isn’t generating the week numbers correctly.

I need a flow which corrects the weeks per year. An example would be that 2020 has 53 weeks whereas 2021 has 52 weeks.
The week numbers generated by this node in knime doesn’t always align with the week numbers according to the epoch calendar. That is my problem. The weeks in knime also don’t run from Monday - Sunday, which is something else I can’t (or don’t know how to) configure.

I’m left to filter for specific years currently, check the epoch calendar, do manual manipulations to make my weeks run through from Monday - Sunday and adjust the weeks. The weeks generated by the node is usually 1 week ahead of the actual week.

I’m looking for a way to generate the right week numbers for different years without the manual work arounds.

I guess you want to do it they way ISO designated it?
Week#Determining Week 1 - Wikipedia

I was going to propose a way of working around that, but then I found out that KNIME starts the week mondays and also the first week of the year the ISO way. 04.01.2021 is also part of week 1. See attached workflow.

Extracting the calendar week from the last day of the year should (almost) be enough. If 31.12.YYYY part of the first week of next year, I guess it’s a 52 week year? If I’m wrong, check the 7th last day of the year and take that value in case the 31st is a 1.


generate week number.knwf (25.6 KB)

1 Like

@Thyme, yes, I believe that it is the ISO standard I’m interested in. I’m not entirely sure that this workflow is what I’m looking for.

If we look at week 53 of 2020, which spans from 28 Dec 2020 - 2 Jan 2021, the ‘Extract Date&Time Fields’ node will split this week into week 53 for 2020 and week 1 for 2021.

When checking the actual weeks for 2021, week 1 starts on Monday 4 Jan 2021. In my example above, Monday 4 Jan 2021 would be indicated to be week 2 for 2021.

Hoping to find an easier way to get the right week numbers for each year. At the moment I’m looking at individual years, checking their week numbers and making sure that they’re not 1 week ahead of other calendars.

I’ve got a slightly older version of knime, seems my weeks start on a Saturday instead of a Monday. Maybe an upgrade of knime will solve my issue?

Are you sure it is the 4th.
When I run it in python snippet week 1 starts at the 3rd.
aas
not sure if that solves your issue
br

Hi @Daniel_Weikert,

We’re looking at 2021, not 2022.
Also considering that my weeks are running from Monday to Sunday.

Thank you.

Oh sorry my bad. In that case this is what I get
sol

Maybe you want to try this and see whether it fits your needs
Hope that helps
br and take care

2 Likes

Works perfectly! Thanks @Daniel_Weikert!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.