This function returns
a four-digit year as display value with an underlying numeric value corresponding
to a timestamp of the first millisecond of the first day of the year
containing
date.
The yearname() function is different to the year() function as it lets you offset the date you want evaluated and lets you set the first month of the year.
If the first month of the year is not January, the function will return the two four-digit years across the twelve month period that contain the date. For example, if the start of the year is April and the date being evaluated is 06/30/2020, the result returned would be 2020-2021.
period_no is an integer, where the value
0 indicates the year which contains date.
Negative values in period_no indicate
preceding years and positive values indicate succeeding years.
first_month_of_year
If you
want to work with (fiscal) years not starting in January, indicate
a value between 2 and 12 in first_month_of_year.
The display value will then be a string showing two years.
You can use the following values to set the first month of year in the first_month_of_year argument:
first_month_of_year values
Month
Value
February
2
March
3
April
4
May
5
June
6
July
7
August
8
September
9
October
10
November
11
December
12
When to use it
The yearname() function is useful for comparing aggregations by year. For example, if you want to see the total sales of products by year.
These dimensions can be created in the load script by using the function to create a field in a Master Calendar table. They can also be created in a chart as calculated dimensions
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.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
year_name
Results table
date
year_name
01/13/2020
2020
02/26/2020
2020
03/27/2020
2020
04/16/2020
2020
05/21/2020
2020
08/14/2020
2020
10/07/2020
2020
12/05/2020
2020
01/22/2021
2021
02/03/2021
2021
03/17/2021
2021
04/23/2021
2021
05/04/2021
2021
06/30/2021
2021
07/26/2021
2021
12/27/2021
2021
06/06/2022
2022
07/18/2022
2022
11/14/2022
2022
12/12/2022
2022
The ‘year_name’ field is created in the preceding load statement by using the yearname() function and passing the date field as the function’s argument.
The yearname() function identifies which year the date value falls into and returns this as a four-digit year value.
Example 2 – period_no
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 between 2020 and 2022 is loaded into a table called ‘Transactions’.
The DateFormat system variable which is set to ‘MM/DD/YYYY’.
A preceding load that uses the yearname() and which is set as the year_name field.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
prior_year_name
Results table
date
prior_year_name
01/13/2020
2019
02/26/2020
2019
03/27/2020
2019
04/16/2020
2019
05/21/2020
2019
08/14/2020
2019
10/07/2020
2019
12/05/2020
2019
01/22/2021
2020
02/03/2021
2020
03/17/2021
2020
04/23/2021
2020
05/04/2021
2020
06/30/2021
2020
07/26/2021
2020
12/27/2021
2020
06/06/2022
2021
07/18/2022
2021
11/14/2022
2021
12/12/2022
2021
Because a period_no of -1 is used as the offset argument in the yearname() function, the function first identifies the year that the transactions take place in. The function then shifts one year prior and returns the resulting year.
Example 3 – first_month_of_year
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The same dataset from the first example.
The DateFormat system variable which is set to ‘MM/DD/YYYY’.
A preceding load that uses the yearname() and which is set as the year_name field.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
year_name
Results table
date
year_name
01/13/2020
2019-2020
02/26/2020
2019-2020
03/27/2020
2019-2020
04/16/2020
2020-2021
05/21/2020
2020-2021
08/14/2020
2020-2021
10/07/2020
2020-2021
12/05/2020
2020-2021
01/22/2021
2020-2021
02/03/2021
2020-2021
03/17/2021
2020-2021
04/23/2021
2021-2022
05/04/2021
2021-2022
06/30/2021
2021-2022
07/26/2021
2021-2022
12/27/2021
2021-2022
06/06/2022
2022-2023
07/18/2022
2022-2023
11/14/2022
2022-2023
12/12/2022
2022-2023
Because the first_month_of_year argument of 4 is used in the yearname() function, the start of the year moves from January 1 to April 1. Therefore, each twelve month period crosses two calendar years and the yearname() function returns the two four-digit years for dates evaluated.
Transaction 8198 takes place on March 17, 2021. The yearname() function sets the beginning of the year on April 1 and the ending on March 30. Therefore, transaction 8198 occurred in the year period from April 1, 2020 and March 30, 2021. As a result, the yearname() function returns the value 2020-2021.
Example 4 – Chart object example
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The same dataset from the first example.
The DateFormat system variable which is set to ‘MM/DD/YYYY’.
However, the field that returns the year that the transaction took place in is created as a measure in a chart object.
Load the data and open a sheet. Create a new table and add this field as a dimension:
date
To calculate the ‘year_name’ field, create this measure:
=yearname(date)
Results table
date
=yearname(date)
01/13/2020
2020
02/26/2020
2020
03/27/2020
2020
04/16/2020
2020
05/21/2020
2020
08/14/2020
2020
10/07/2020
2020
12/05/2020
2020
01/22/2021
2021
02/03/2021
2021
03/17/2021
2021
04/23/2021
2021
05/04/2021
2021
06/30/2021
2021
07/26/2021
2021
12/27/2021
2021
06/06/2022
2022
07/18/2022
2022
11/14/2022
2022
12/12/2022
2022
The ‘year_name’ measure is created in the chart object using the yearname() function and passing the date field as the function’s argument.
The yearname() function identifies which year the date value falls into and returns this as a four-digit year value.
Example 5 – Scenario
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The same dataset from the first example.
The DateFormat system variable which is set to ‘MM/DD/YYYY’.
The end user would like a chart that presents the total sales by quarter for the transactions. Use the yearname() function as a calculated dimension to create this chart when the yearname() dimension is not available in the data model.