This function returns the year to which the week number belongs according to ISO 8601. The week number ranges between
1 and approximately 52.
Syntax:
weekyear(expression)
Return data type: integer
The weekyear() function determines which week of a year a date falls into. It then returns the year corresponding to that week number.
By default, Qlik apps use broken weeks (defined by the BrokenWeeks system variable) and week number 1 begins on January 1 and the year ends after week 52. Therefore, the weekyear() function will always return the same value as the week() function when the application uses broken weeks.
However, if the BrokenWeeks system variable is set to use unbroken weeks, week 1 must only contain a certain number of days in January based on the value specified in the ReferenceDay system variable.
For example, if a ReferenceDay value of 4 is used, week 1 must include at least four days in January. It is possible for week 1 to include dates in December of the previous year or for the final week number of a year to include dates in January of the following year. In situations like this, the weekyear() function will return a different value to the year() function.
When to use it
The weekyear() function is useful when you would like to compare aggregations by years. For example, if you would like to see the total sales of products by year. The weekyear() function is chosen over year() when the user would like to retain consistency with the BrokenWeeks system variable in the app.
Regional settings
Unless otherwise specified, the examples in this topic use the following date format: MM/DD/YYYY. The date format is specified in the SET DateFormat statement in your data load script. The default date formatting may be different in your system, due to your regional settings and other factors. You can change the formats in the examples below to suit your requirements. Or you can change the formats in your load script to match these examples.
Default regional settings in apps are based on the regional system settings of the computer or server where Qlik Sense is installed. If the Qlik Sense server you are accessing is set to Sweden, the Data load editor will use Swedish regional settings for dates, time, and currency. These regional format settings are not related to the language displayed in the Qlik Sense user interface. Qlik Sense will be displayed in the same language as the browser you are using.
Function examples
Example
Result
weekyear('12/30/1996')
Returns 1997, because week 1 of 1997 starts on 12/30/1996
weekyear('01/02/1997')
Returns 1997
weekyear('12/28/1997')
Returns 1997
weekyear('12/30/1997')
Returns 1998, because week 1 of 1998 starts on 12/29/1997
weekyear('01/02/1999')
Returns 1998, because week 53 of 1998 ends on 01/03/1999
Returns an integer representing the year when the expression is interpreted as a date according to the standard number interpretation.
Example 1 - Broken weeks
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset containing a set of transactions for the last week of 2020 and first week of 2021 which is loaded into a table called ‘Transactions’.
The BrokenWeeks variable which is set to 1.
A preceding load which contains the following:
The weekyear() function, set as the field ‘week_year’ that returns the year in which the transactions took place.
The week() function, set as the field ‘week’ that shows the week number of each transaction date.
Load script
SET BrokenWeeks=1;
Transactions:
Load
*,
week(date) as week,
weekyear(date) as week_year
;
Load
*
Inline
[
id,date,amount
8176,12/28/2020,19.42
8177,12/29/2020,23.80
8178,12/30/2020,82.06
8179,12/31/2020,40.56
8180,01/01/2021,37.23
8181,01/02/2021,17.17
8182,01/03/2021,88.27
8183,01/04/2021,57.42
8184,01/05/2021,67.42
8185,01/06/2021,23.80
8186,01/07/2021,82.06
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
week
week_year
Results table
id
date
week
week_year
8176
12/28/2020
53
2020
8177
12/29/2020
53
2020
8178
12/30/2020
53
2020
8179
12/31/2020
53
2020
8180
01/01/2021
1
2021
8181
01/02/2021
1
2021
8182
01/03/2021
2
2021
8183
01/04/2021
2
2021
8184
01/05/2021
2
2021
8185
01/06/2021
2
2021
8186
01/07/2021
2
2021
The ‘week_year’ field is created in the preceding load statement by using the weekyear() function and passing the date field as the function’s argument.
The BrokenWeeks system variable is set to 1 meaning that the app uses broken weeks. Week 1 begins on January 1.
Transaction 8181 takes place on January 2, which is part of week 1. Therefore, it returns a value of 2021 for the ‘week_year’ field.
Example 2 - Unbroken weeks
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset containing a set of transactions for the last week of 2020 and first week of 2021 which is loaded into a table called ‘Transactions’.
The BrokenWeeks variable which is set to 0.
A preceding load which contains the following:
The weekyear() function, set as the field ‘week_year’ that returns the year in which the transactions took place.
The week() function, set as the field ‘week’ that shows the week number of each transaction date.
However, in this example, the company policy is to use unbroken weeks.
Load script
SET BrokenWeeks=0;
Transactions:
Load
*,
week(date) as week,
weekyear(date) as week_year
;
Load
*
Inline
[
id,date,amount
8176,12/28/2020,19.42
8177,12/29/2020,23.80
8178,12/30/2020,82.06
8179,12/31/2020,40.56
8180,01/01/2021,37.23
8181,01/02/2021,17.17
8182,01/03/2021,88.27
8183,01/04/2021,57.42
8184,01/05/2021,67.42
8185,01/06/2021,23.80
8186,01/07/2021,82.06
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
week
week_year
Results table
id
date
week
week_year
8176
12/28/2020
53
2020
8177
12/29/2020
53
2020
8178
12/30/2020
53
2020
8179
12/31/2020
53
2020
8180
01/01/2021
53
2020
8181
01/02/2021
53
2020
8182
01/03/2021
1
2021
8183
01/04/2021
1
2021
8184
01/05/2021
1
2021
8185
01/06/2021
1
2021
8186
01/07/2021
1
2021
The BrokenWeeks system variable is set to 0 meaning that the application uses unbroken weeks. Therefore, week 1 is not required to begin on January 1.
Week 53 of 2020 continues until the end of January 2, 2021, with week 1 of 2020 beginning on Sunday, January 3, 2021.
Transaction 8181 takes place on January 2, which is part of week 1. Therefore, it returns a value of 2021 for the ‘week_year’ field.
Example 3 - Chart object example
Overview
The same dataset and scenario as the first example are used.
However, in this example the dataset is unchanged and loaded into the application. The calculation that returns the week number of the year when the transactions took place is created as a measure in a chart in the app.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
To calculate the week that a transaction takes place in, create the following measure:
=week(date)
To calculate the year that a transaction takes place in based on the week number, create the following measure:
=weekyear(date)
Results table
id
date
week
week_year
8176
12/28/2020
53
2020
8177
12/29/2020
53
2020
8178
12/30/2020
53
2020
8179
12/31/2020
53
2020
8180
01/01/2021
1
2021
8181
01/02/2021
1
2021
8182
01/03/2021
2
2021
8183
01/04/2021
2
2021
8184
01/05/2021
2
2021
8185
01/06/2021
2
2021
8186
01/07/2021
2
2021
The ‘week_year’ field is created in the preceding load statement by using the weekyear() function and passing the date field as the function’s argument.
The BrokenWeeks system variable is set to 1 meaning that the app uses broken weeks. week 1 begins on January 1.
Transaction 8181 takes place on January 2, which is part of week 1. Therefore, it returns a value of 2021 for the ‘week_year’ field.
Example 4 - Scenario
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset containing a set of transactions for the last week of 2020 and first week of 2021 which is loaded into a table called ‘Transactions’.
The BrokenWeeks variable which is set to 0. This means the app will use unbroken weeks.
The ReferenceDay variable which is set to 2. This means the year will begin on January 2 and will contain a minimum of two days in January.
The FirstWeekDay variable which is set to 1. This means the first day of the week will be Tuesday.
The company policy is to use broken weeks. The end user would like a chart that presents the total sales by year. The app uses unbroken weeks with week 1 containing a minimum of two days in January.
Load script
SET BrokenWeeks=0;
SET ReferenceDay=2;
SET FirstWeekDay=1;
Transactions:
Load
*
Inline
[
id,date,amount
8176,12/28/2020,19.42
8177,12/29/2020,23.80
8178,12/30/2020,82.06
8179,12/31/2020,40.56
8180,01/01/2021,37.23
8181,01/02/2021,17.17
8182,01/03/2021,88.27
8183,01/04/2021,57.42
8184,01/05/2021,67.42
8185,01/06/2021,23.80
8186,01/07/2021,82.06
];
Results
Load the data and open a sheet. Create a new table.
To calculate the year that a transaction takes place in based on the week number, create the following measure:
=weekyear(date)
To calculate total sales, create the following measure:
sum(amount)
Set the measure’s Number Formatting to Money.
Results table
weekyear(date)
=sum(amount)
2020
19.42
2021
373.37
Did this page help you?
If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!