Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Week numbers are often used in calendars, although not as commonly in some countries as in others. In northern Europe, it is very common to refer to a week by its number, but in many other countries it is not used at all. Just as with the week start, week numbers are defined differently depending on country, so you may need to add code in QlikView to generate your own week numbers.
So, how do you count the weeks? Is Jan 1st always part of week one? Not necessarily.
If week 53 starts as late as Dec 28th, does Jan 1st also belong to week 53? Sometimes, yes.
There is a definition made by the International Organization for Standardization (ISO 8601) that QlikView uses to calculate week numbers. It states that
These three bullets define the three parameters you need to define general week numbers:
Set vCal_FD = 0; // First Day of the week {0=Mon, 1=Tue, ... , 6=Sun}
Set vCal_BW = 0; // Broken Weeks allowed {0=No, 1=Yes}
Set vCal_RD = 4; // Reference day = This day in Jan defines week one {1..7}
How the week start - the first parameter - influences the week number can be seen in the following table. It shows how the week number would change for the days around New Year 2013 if different week starts are used. The other parameters are kept constant.
The second parameter concerns whether or not broken weeks should be used. If they are, a new week number will always be used on Jan 1st, and as a consequence the first and last weeks of the year can have less than 7 days.
And finally, the third parameter, the reference day. It defines which day that always belongs to week 1. In the table below, the reference day is 4; hence Jan 4th always belongs to week 1, which can be clearly seen. This number also defines the minimal number of days of week 1 that fall in the new year.
The ISO standard is thus a 0/0/4 week numbering. In countries where Sunday is used as first day of the week, I have seen several different variants: 6/1/1, 6/0/3 and 6/0/4.
If you copy the above parameters to your QlikView script and the following lines to your Master Calendar definition, you can redefine the week numbers any way you want:
Load *,
Div( Date - WeekStart( WeekYearRefDate, 0, $(vCal_FD) ) + 7, 7 ) as WeekNumber,
Year( WeekYearRefDate ) as WeekYear;
Load *,
Date( YearStart( If( $(vCal_BW), Date, WeekRefDate )) + $(vCal_RD) - 1) as WeekYearRefDate ;
Load *,
Date( WeekStart( Date, 1, $(vCal_FD) ) - $(vCal_RD) ) as WeekRefDate ;
The fields WeekYearRefDate (Jan 4th in the ISO definition) and WeekRefDate (the Thursday of the week in the ISO definition) are really not necessary, but the expressions become somewhat simpler if these are used.
Until we get a general week numbering functionality built into the QlikView standard functions (and, yes, we are looking into this) you will have to redefine the week numbers using the above script. Good luck!
Further reading related to this topic:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.