This function returns an integer representing the year when the
expression is interpreted as a date
according to the standard number interpretation.
Syntax:
year(expression)
Return data type: integer
The year() function is available as both a script and chart function. The function returns the year for a particular date. It is commonly used to create a year field as a dimension in a Master Calendar.
When to use it
The year() function is useful when you would like to compare aggregations by year. For example, the function could be used if you would like to see the total sales of products by year.
These dimensions can be created either in the load script by using the function to create a field in a Master Calendar table. Alternatively, it could be used directly in a chart as a calculated dimension.
Function examples
Example
Result
year(
'2012-10-12' )
returns 2012
year(
'35648' )
returns 1997, because 35648 = 1997-08-06
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.
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset of dates, which is loaded into a table named Master Calendar.
The default DateFormat system variable (MM/DD/YYYY) is used.
A preceding load which is used to create an additional field, year, using the year() function.
Load script
SET DateFormat='MM/DD/YYYY';
Master_Calendar:
Load
date,
year(date) as year
;
Load
date
Inline
[
date
12/28/2020
12/29/2020
12/30/2020
12/31/2020
01/01/2021
01/02/2021
01/03/2021
01/04/2021
01/05/2021
01/06/2021
01/07/2021
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
year
Results table
date
year
12/28/2020
2020
12/29/2020
2020
12/30/2020
2020
12/31/2020
2020
01/01/2021
2021
01/02/2021
2021
01/03/2021
2021
01/04/2021
2021
01/05/2021
2021
01/06/2021
2021
01/07/2021
2021
Example 2 – ANSI Dates
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset of dates, which is loaded into a table named Master Calendar.
The default DateFormat system variable (MM/DD/YYYY) is used. However, the dates included in the dataset are in ANSI standard date format.
A preceding load, which is used to create an additional field, named year, using the year() function.
Load script
SET DateFormat='MM/DD/YYYY';
Master_Calendar:
Load
date,
year(date) as year
;
Load
date
Inline
[
date
2020-12-28
2020-12-29
2020-12-30
2020-12-31
2021-01-01
2021-01-02
2021-01-03
2021-01-04
2021-01-05
2021-01-06
2021-01-07
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
year
Results table
date
year
2020-12-28
2020
2020-12-29
2020
2020-12-30
2020
2020-12-31
2020
2021-01-01
2021
2021-01-02
2021
2021-01-03
2021
2021-01-04
2021
2021-01-05
2021
2021-01-06
2021
2021-01-07
2021
Example 3 – Unformatted dates
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset of dates in numerical format, which is loaded into a table named Master Calendar.
The default DateFormat system variable (MM/DD/YYYY) is used.
A preceding load, which is used to create an additional field, year, using the year() function.
The original unformatted date is loaded, named unformatted_date, and to provide clarity, a further additional field, named long_date, is used to convert the numerical date into a formatted date field using the date() function.
Load script
SET DateFormat='MM/DD/YYYY';
Master_Calendar:
Load
unformatted_date,
date(unformatted_date) as long_date,
year(unformatted_date) as year
;
Load
unformatted_date
Inline
[
unformatted_date
44868
44898
44928
44958
44988
45018
45048
45078
45008
45038
45068
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
unformatted_date
long_date
year
Results table
unformatted_date
long_date
year
44868
11/03/2022
2022
44898
12/03/2022
2022
44928
01/02/2023
2023
44958
02/01/2023
2023
44988
03/03/2023
2023
45008
03/23/2023
2023
45018
04/02/2023
2023
45038
04/22/2023
2023
45048
05/02/2023
2023
45068
05/22/2023
2023
45078
06/01/2023
2023
Example 4 – Chart object example
Overview
Open the Data load editor and add the load script below to a new tab.
In this example, a dataset of orders placed is loaded into a table named Sales. The table contains three fields:
id
sales_date
amount
Warranties on product sales last two years from the date of sale. The task is to create a measure in a chart to determine the year in which each warranty will expire.
Load the data and open a sheet. Create a new table and add this field as a dimension: sales_date.
Create the following measure:
=year(sales_date+365*2)
Results table
sales_date
=year(sales_date+365*2)
12/28/2020
2022
12/29/2020
2022
12/30/2020
2022
12/31/2020
2022
01/01/2021
2023
01/02/2021
2023
01/03/2021
2023
01/04/2021
2023
01/05/2021
2023
01/06/2021
2023
01/07/2021
2023
The results of this measure can be seen in the table above. To add two years to a date, multiply 365 by 2 and add the result to the sales date. Therefore, sales that took place in 2020 have an expiry year of 2022.
Chart
Charts are objects where calculations, aggregations, and groupings can be made. Graphical visualizations, such as bar charts and pie charts are common examples, but also non-graphical objects such as pivot tables are charts.
A chart consists of dimensions and measures, where the measures are calculated once per dimensional value. If the chart contains multiple dimensions, the measures are calculated once per combination of dimensional values.
A field contains values, loaded from a data source. At a basic level, a field corresponds to a column in a table. Fields are used to create dimensions and measures in visualizations.
A dimension is an entity used to categorize data in a chart. For example, the slices in a pie chart or the bars of a bar chart represent individual values in a dimension. Dimensions are often a single field with discrete values, but can also be calculated in an expression.
A dimension is a dataset in a data mart that forms part of the star schema. Dimension datasets hold the descriptive information for all related fields that are included in the fact table’s records. A few common examples of dimension datasets are Customer and Product. Since the data in a dimension dataset is often denormalized, dimension datasets have a large number of columns.
The load script is a sequence of statements that defines what data to load and how to link the different loaded tables. It can be generated with the Data manager, or with the Data load editor, where it also can be viewed and edited.
The term dataset is sometimes synonymous with table. It can refer to the original source table, the table after undergoing transformations, or the fact and dimension tables in a data mart.
It can also refer to a logical table, where there are several instance tables and views:
Current data
History, which holds previous versions of the table
A preceding load is a script construct that allows you to load from the following LOAD or SELECT statement without specifying that source. Preceding loads are often faster than resident loads.
A measure is a calculation base on one ore more aggregations. For example, the sum of sales is a single aggregation, while the sum of sales divided by the count of customers is a measure based on two aggregations.