Skip to main content Skip to complementary content

yeartodate - script and chart function

This function finds if the input timestamp falls within the year of the date the script was last loaded, and returns True if it does, False if it does not.

Syntax:  

YearToDate(timestamp[ , yearoffset [ , firstmonth [ , todaydate] ] ])

Return data type: Boolean

Information note

In Qlik Sense, the Boolean true value is represented by -1, and the false value is represented by 0.

Example diagram of yeartodate() function

Diagram displaying the date values for which the yeartodate function will return a value of True or False. In this case, it returns True for dates between January 1 and April 26, 2022, and False for all other dates.

If none of the optional parameters are used, the year to date means any date within one calendar year from January 1 up to and including the date of the last script execution.

In other words, the yeartodate() function, when triggered with no additional parameters, is used to evaluate a timestamp and return a Boolean result based on whether the date occurred within the calendar year up to and including the date that the reload took place.

However, it is also possible to supersede the start date of the year using the firstmonth argument, as well as to make comparisons with preceding or following years using the yearoffset argument.

Finally, in instances of historical datasets, the yeartodate() function provides a parameter to set todaydate, which will instead compare the timestamp to the calendar year up to and including the date provided in the todaydate argument.

Arguments
Argument Description
timestamp

The timestamp to evaluate, for example '10/12/2012'.

yearoffset

By specifying a yearoffset, yeartodate returns True for the same period in another year. A negative yearoffset indicates a previous year, a positive offset a future year. The most recent year-to-date is achieved by specifying yearoffset = -1. If omitted, 0 is assumed.

firstmonth

By specifying a firstmonth between 1 and 12 (1 if omitted), the beginning of the year may be moved forward to the first day of any month. For example, if you want to work with a fiscal year beginning on May 1, specify firstmonth = 5. A value of 1 would indicate a fiscal year starting on January 1, and a value of 12 would indicate a fiscal year starting on December 1.

todaydate By specifying a todaydate (timestamp of the last script execution if omitted) it is possible to move the day used as the upper boundary of the period.

When to use it

The yeartodate() 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 the evaluated date occurred in the year up to and including the last reload date of the application.

For example, the YearToDate() function can be used to identify all equipment manufactured so far in the current year.

The following examples assume last reload time = 11/18/2011.

Function examples
Example Result
yeartodate( '11/18/2010') returns False
yeartodate( '02/01/2011') returns True
yeartodate( '11/18/2011') returns True
yeartodate( '11/19/2011') returns False
yeartodate( '11/19/2011', 0, 1, '12/31/2011') returns True
yeartodate( '11/18/2010', -1) returns True
yeartodate( '11/18/2011', -1) returns False
yeartodate( '04/30/2011', 0, 5) returns False
yeartodate( '05/01/2011', 0, 5) returns True

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.

Example 1 – Basic example

Example 2 – yearoffset

Example 3 – firstmonth

Example 4 – todaydate

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!