In Qlik Sense, the Boolean true value is represented by -1, and the false value is represented by 0.
The inyeartodate() function will segment a particular portion of the year with the base_date, identifying the maximum allowed date for that year segment. The function then evaluates whether a date field or value falls into this segment and returns a Boolean result.
Arguments
Argument
Description
timestamp
The date that you want to compare with base_date.
base_date
Date that is used to evaluate the year.
period_no
The year can be offset by period_no.
period_no is an integer, where the
value 0 indicates the year that contains base_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.
When to use it
The inyeartodate() function returns a Boolean result. Typically, this type of function will be used as a condition in an if expression. This would return an aggregation or calculation dependent on whether a date evaluated occurred in the year up to and including the date in question.
For example, the inyeartodate() function can be used to identify all equipment manufactured in a year up to a specific date.
These examples use the date format MM/DD/YYYY. The date format is specified in the SET DateFormat statement at the top of your data load script. Change the format in the examples to suit your requirements.
Function examples
Example
Result
inyeartodate ('01/25/2013', '02/01/2013', 0)
Returns TRUE.
inyeartodate ('01/25/2012', '01/01/2013', 0)
Returns FALSE.
inyeartodate ('01/25/2012', '02/01/2013', -1)
Returns TRUE.
inyeartodate ('11/25/2012', '01/31/2013', 0, 4)
Returns TRUE. The value of timestamp falls inside the fiscal year beginning in the fourth month and before the value of base_date.
inyeartodate ('3/31/2013', '01/31/2013', 0, 4 )
Returns FALSE. Compared with the previous example, the value of timestamp is still inside the fiscal year, but it is after the value of base_date, so it falls outside the part of the year.
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.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
in_year_to_date
Results table
date
in_year_to_date
01/13/2020
0
02/26/2020
0
03/27/2020
0
04/16/2020
0
05/21/2020
0
06/14/2020
0
08/07/2020
0
09/05/2020
0
01/22/2021
-1
02/03/2021
-1
03/17/2021
-1
04/23/2021
-1
05/04/2021
-1
06/30/2021
-1
07/26/2021
-1
07/27/2021
0
06/06/2022
0
07/18/2022
0
11/14/2022
0
12/12/2022
0
The in_year_to_date field is created in the preceding load statement by using the inyeartodate() function. The first argument provided identifies which field is being evaluated.
The second argument is a hard-coded date for the for the July 26, 2021, which is the base_date that identifies the end boundary of the year segment. A period_no of 0 is the final argument, meaning that the function is not comparing years preceding or following the segmented year.
Any transaction that occurs in between January 1 and July 26 returns a Boolean result of TRUE. Transactions dates before 2021 and beyond July 26, 2021 return FALSE.
Example 2 – period_no
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The same dataset and scenario as the first example.
The creation of a field, previous_year_to_date, that determines which transactions took place a full year before the year segment ending on July 26, 2021.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
previous_year_to_date
Results table
date
previous_year_to_date
01/13/2020
-1
02/26/2020
-1
03/27/2020
-1
04/16/2020
-1
05/21/2020
-1
06/14/2020
-1
08/07/2020
0
09/05/2020
0
01/22/2021
0
02/03/2021
0
03/17/2021
0
04/23/2021
0
05/04/2021
0
06/30/2021
0
07/26/2021
0
07/27/2021
0
06/06/2022
0
07/18/2022
0
11/14/2022
0
12/12/2022
0
A period_no value of -1 indicates that the inyeartodate () function compares the input quarter segment to the preceding year. With an input date of July 26, 2021, the segment from January 1, 2021 to July 26, 2021 was initially identified as the year-to-date. The period_no then offsets this segment by a full year earlier, causing the date boundaries to become January 1 to July 26, 2020.
Therefore, any transaction that occurs between January 1 and July 26, 2020 will return a Boolean result of TRUE.
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 and scenario as the first example.
The creation of a field, in_year_to_date, that determines which transactions took place in the same year up to July 26, 2021.
In this example, we set March as the first month of the fiscal year.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
in_year_to_date
Results table
date
in_year_to_date
01/13/2020
0
02/26/2020
0
03/27/2020
0
04/16/2020
0
05/21/2020
0
06/14/2020
0
08/07/2020
0
09/05/2020
0
01/22/2021
0
02/03/2021
0
03/17/2021
-1
04/23/2021
-1
05/04/2021
-1
06/30/2021
-1
07/26/2021
-1
07/27/2021
0
06/06/2022
0
07/18/2022
0
11/14/2022
0
12/12/2022
0
By using 3 as the first_month_of_year argument in the inyeartodate() function, the function begins the year on March 1. The base_date of July 26, 2021 then sets the end date for that year segment.
Therefore, any transaction that occurs between March 1 and July 26, 2021 will return a Boolean result of TRUE, while transactions with dates outside these boundaries will return a value of FALSE.
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 and scenario as the first example. However, in this example, the unchanged dataset is loaded into the application. The calculation that determines which transactions took place in the same year up to July 26, 2021 is created as a measure in a chart object in the application.
Load the data and open a sheet. Create a new table and add this field as a dimension:date.
Create the following measure:
=inyeartodate(date,'07/26/2021', 0)
Results table
date
=inyeartodate(date,'07/26/2021', 0)
01/13/2020
0
02/26/2020
0
03/27/2020
0
04/16/2020
0
05/21/2020
0
06/14/2020
0
08/07/2020
0
09/05/2020
0
01/22/2021
-1
02/03/2021
-1
03/17/2021
-1
04/23/2021
-1
05/04/2021
-1
06/30/2021
-1
07/26/2021
-1
07/27/2021
0
06/06/2022
0
07/18/2022
0
11/14/2022
0
12/12/2022
0
The in_year_to_date measure is created in the chart object by using the inyeartodate() function. The first argument provided identifies which field is being evaluated. The second argument is a hard-coded date for July 26, 2021, which is the base_date that identifies the end boundary of the comparator year segment. A period_no of 0 is the final argument, meaning that the function is not comparing years preceding or following the segmented year.
Any transaction that occurs between January 1 and July 26, 2021 returns a Boolean result of TRUE. Transaction dates before 2021 and after July 26, 2021 return FALSE.
Example 5 – Scenario
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset which is loaded into a table called Products.
Information concerning product ID, product type, manufacture date, and cost price.
The end user would like a chart object that displays, by product type, the cost of the products manufactured in 2021 up to July 26.
The inyeartodate() function returns a Boolean value when evaluating the manufacturing dates of each of the products. For any product manufactured in 2021 before July 27, the inyeartodate() function returns a Boolean value of TRUE and sums the cost_price.
Product D is the only product that was also manufactured after July 26th in 2021. The entry with product_ID 8203 was manufactured on December 27 and cost $25.12. Therefore, this cost was not included in the total for Product D in the chart object.
Field
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.
An aggregation is a calculation using multiple records in the source tables. Often it is a single field aggregated with a function such as sum, count, min, max, or average. For example, the sum of sales.
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.