Skip to main content Skip to complementary content

week - script and chart function

This function returns an integer representing the week number corresponding to the date entered.

Syntax:  

week(timestamp [, first_week_day [, broken_weeks [, reference_day]]])

Return data type: integer

Arguments
Argument Description
timestamp The date or timestamp to evaluate.
first_week_day

Specifies the day on which the week starts. If omitted, the value of variable FirstWeekDay is used.

The possible values first_week_day are 0 for Monday, 1 for Tuesday, 2 for Wednesday, 3 for Thursday, 4 for Friday, 5 for Saturday, and 6 for Sunday.

For more information about the system variable, see FirstWeekDay.

broken_weeks

If you don't specify broken_weeks, the value of variable BrokenWeeks will be used to define if weeks are broken or not.

For more information about the system variable, see BrokenWeeks

reference_day

If you don't specify reference_day, the value of variable ReferenceDay will be used to define which day in January to set as reference day to define week 1. By default, Qlik Sense functions use 4 as the reference day. This means that week 1 must contain January 4, or put differently, that week 1 must always have at least 4 days in January.

For more information about the system variable, see ReferenceDay

The week() function determines which week the date falls into and returns the week number.

 In Qlik Sense, the regional settings are fetched when the app is created, and the corresponding settings are stored in the script as environment variables. These are used to determine the week number.

This means that most European app developers gets the following environment variables, corresponding to the ISO 8601 definition:

Set FirstWeekDay =0; // Monday as first week day Set BrokenWeeks =0; // Use unbroken weeks Set ReferenceDay =4; // Jan 4th is always in week 1

A North American app developer often gets the following environment variables:

Set FirstWeekDay =6; // Sunday as first week day Set BrokenWeeks =1; // Use broken weeks Set ReferenceDay =1; // Jan 1st is always in week 1

The first day of the week is determined by the FirstWeekDay system variable. You can also change the first day of the week by using the first_week_day argument in the week() function.

If your application uses broken weeks, the week number count begins on January 1 and ends on the day prior to the FirstWeekDay system variable regardless of how many days have occurred.

If your application is using unbroken weeks, week 1 can begin in the previous year or in the first few days of January. This depends on how you use the FirstWeekDay and the ReferenceDay environment variables.

When to use it

The week() function is useful when you would like to compare aggregations by weeks. For example, it could be used if you would like to see the total sales of products by week. The week() function is chosen over weekname() when the user would like the calculation to not necessarily use the application’s BrokenWeeks, FirstWeekDay, or ReferenceDay system variables.

For example, if you want to see the total sales of products by week.

If the application is using unbroken weeks, week 1 may contain dates from December of the previous year or exclude dates in January of the current year. If the application is using broken weeks, week 1 may contain less than seven days..

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. For more information, see Modifying regional settings for apps and scripts.

Default regional settings in apps are based on the user profile. These regional format settings are not related to the language displayed in the Qlik Cloud user interface. Qlik Cloud will be displayed in the same language as the browser you are using.

If you are an app creator, you can set the default region for apps you create. For more information, see Setting your preferred regional settings for creating apps and scripts in Qlik Cloud Analytics.

The examples below assume

Set DateFormat= 'MM/DD/YYYY'; Set FirstWeekDay=0; Set BrokenWeeks=0; Set ReferenceDay=4;
Function examples
Example Result

week('12/28/2021')

Returns 52.

week(44614) Returns 8, since this is the serial number for 02/22/2022.
week('01/03/2021') Returns 53.
week('01/03/2021',6) Returns 1.

Example 1 – Default system variables

Example 2 – first_week_day

Example 3 – unbroken_weeks

Example 4 – reference_day

Example 5 – Chart object example

Example 6 – Scenario

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!