This function finds if a timestamp falls within the same month, bi-month, quarter, four-month period, or half-year as a base date. It is also possible to find if the timestamp falls within a previous or following time period.
In Qlik Sense, the Boolean true value is represented by -1, and the false value is represented by 0.
The inmonths() function divides the year into segments based on the n_months argument provided. It then determines whether each timestamp evaluated falls into the same segment as the base_date argument. If, however, a period_no argument is provided, the function determines whether the timestamps fall into a previous or following period from the base_date.
The following segments of the year are available in the function as n_month arguments.
n_month arguments
Period
Number of months
month
1
bi-month
2
quarter
3
four months
4
half-year
6
When to use it
The inmonths() function returns a Boolean result. Typically, this type of function will be used as a condition in an if expression. By using the inmonths() function, you can select the period that you want to be evaluated. For example, letting the user identify products manufactured in the month, quarter, or half-year of a certain period.
Return data type: Boolean
In Qlik Sense, the Boolean true value is represented by -1, and the false value is represented by 0.
Arguments
Argument
Description
n_months
The number of months that defines the period. An integer or expression that resolves to an integer that must be one of: 1 (equivalent to the inmonth() function), 2 (bi-month), 3 (equivalent to the inquarter()function), 4 (four-month period), or 6 (half year).
timestamp
The date that you want to compare with base_date.
base_date
Date that is used to evaluate the period.
period_no
The period can be offset by period_no, an integer, or expression resolving to an integer, where the value 0 indicates the period that contains base_date. Negative values in period_no indicate preceding periods and
positive values indicate succeeding periods.
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.
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
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.
Returns TRUE. Because the value of timestamp, 01/25/2013, lies within the four-month period 01/01/2013 to 04/30/2013, in which the value of base_date, 04/25/2013 lies.
inmonths(4, '05/25/2013', '04/25/2013', 0)
Returns FALSE. Because 05/25/2013 is outside the same period as the previous example.
inmonths(4, '11/25/2012', '02/01/2013', -1 )
Returns TRUE. Because the value of period_no, -1, shifts the search period back one period of four months (the value of n-months), which makes the search period 09/01/2012 to 12/31/2012.
inmonths(4, '05/25/2006', '03/01/2006', 0, 3)
Returns TRUE. Because the value of first_month_of_year is set to 3, which makes the search period 03/01/2006 to 07/30/2006 instead of 01/01/2006 to 04/30/2006.
Example 1 - No additional arguments
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 for 2022 is loaded into a table called ‘Transactions’.
A preceding load with an additional variable ‘in_months’, that determines which transactions took place in the same quarter as May 15, 2022.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
in_months
Results table
date
in_months
2/19/2022
0
3/7/2022
0
3/30/2022
0
4/5/2022
-1
4/16/2022
-1
5/1/2022
-1
5/7/2022
-1
5/22/2022
-1
6/15/2022
-1
6/26/2022
-1
7/9/2022
0
7/22/2022
0
7/23/2022
0
7/27/2022
0
8/2/2022
0
8/8/2022
0
8/19/2022
0
9/26/2022
0
10/14/2022
0
10/29/2022
0
The ‘in_months’ field is created in the preceding load statement by using the inmonths() function. The first argument provided is 3 which divides the year into quarter segments. The second argument identifies which field is being evaluated, the date field in this example. The third argument is a hard-coded date for the for May 15 which is the base_date and a period_no of 0 is the final argument.
May falls into the second quarter of the year. Therefore, any transaction that occurs between April 1 and June 30 will return a Boolean result of TRUE. This is validated in the results table.
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 for 2022 is loaded into a table called ‘Transactions’.
A preceding load with an additional variable, ‘previous_quarter’, that determines whether transactions took place in the quarter before May 15, 2022.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
previous_quarter
Results table
date
previous quarter
2/19/2022
-1
3/7/2022
-1
3/30/2022
-1
4/5/2022
0
4/16/2022
0
5/1/2022
0
5/7/2022
0
5/22/2022
0
6/15/2022
0
6/26/2022
0
7/9/2022
0
7/22/2022
0
7/23/2022
0
7/27/2022
0
8/2/2022
0
8/8/2022
0
8/19/2022
0
9/26/2022
0
10/14/2022
0
10/29/2022
0
The function evaluates whether transactions occurred in the first quarter of the year by using -1 as the period_no argument in the inmonths() function. May 15 is the base_date and falls into the second quarter of the year (April-June).
Therefore, any transaction that occurs between January and March 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:
A dataset containing a set of transactions for 2022 is loaded into a table called ‘Transactions’.
A preceding load with an additional variable, ‘in_months’, that determines which transactions took place in the same quarter as May 15, 2022.
In this example, the organizational policy is for March to be the first month of the financial year.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
in_months
Results table
date
in_months
2/19/2022
0
3/7/2022
-1
3/30/2022
-1
4/5/2022
-1
4/16/2022
-1
5/1/2022
-1
5/7/2022
-1
5/22/2022
-1
6/15/2022
0
6/26/2022
0
7/9/2022
0
7/22/2022
0
7/23/2022
0
7/27/2022
0
8/2/2022
0
8/8/2022
0
8/19/2022
0
9/26/2022
0
10/14/2022
0
10/29/2022
0
By using 3 as the first_month_of_year argument in the inmonths() function, the function begins the year on March 1. The inmonths() function then divides the year into quarters: Mar-May, Jun-Aug, Sep-Nov, Dec-Feb. Therefore, May 15 falls into the first quarter of the year (March-May).
Any transaction that occurs in these months will return a Boolean result of TRUE.
Example 4 - Chart object example
Overview
The same dataset and scenario from the first example are used.
However, in this example, the dataset is unchanged and loaded into the application. The calculation that determines whether transactions took place in the same quarter as May 15, 2022 is created as a measure in a chart in the app.
Load the data and open a sheet. Create a new table and add this field as a dimension:
date
To calculate whether transactions took place in the same quarter as May 15, create the following measure:
=inmonths(3,date,'05/15/2022', 0)
Results table
date
=inmonths(3,date,'05/15/2022', 0)
2/19/2022
0
3/7/2022
0
3/30/2022
0
4/5/2022
-1
4/16/2022
-1
5/1/2022
-1
5/7/2022
-1
5/22/2022
-1
6/15/2022
-1
6/26/2022
-1
7/9/2022
0
7/22/2022
0
7/23/2022
0
7/27/2022
0
8/2/2022
0
8/8/2022
0
8/19/2022
0
9/26/2022
0
10/14/2022
0
10/29/2022
0
The ‘in_months’ field is created in the chart by using the inmonths() function. The first argument provided is 3 which divides the year into quarter segments. The second argument identifies which field is being evaluated, the date field in this example. The third argument is a hard-coded date for the for May 15 which is the base_date and a period_no of 0 is the final argument.
May falls into the second quarter of the year. Therefore, any transaction that occurs between April 1 and June 30 will return a Boolean result of TRUE. This is validated in the results table.
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’.
The table contains the following fields:
product ID
product type
manufacture date
cost price
The end user would like a chart that displays, by product type, the cost of products manufactured in the first segment of 2021. The user would like to be able to define the length of this segment.
The inmonths() function uses the user input as its argument to define the size of the starting segment of the year. The function passes in the manufacture date of each of the products as the inmonths() function’s second argument. By using January 1 as the third argument in the inmonths() function, products with manufacture dates that fall in the opening segment of the year will return a Boolean value of TRUE and therefore the sum function will add the costs of those products.
Dataset
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 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 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.
A Qlik Sense or QlikView app: Apps are task-specific, purpose-built applications. Apps contain data loaded from data sources that is interpreted through visualizations.
The Qlik Sense Mobile app: A mobile app for iOS and Android devices. In the mobile app, you connect to and interact with your cloud data. You can work with your available apps.
Sheets are components of Qlik Sense apps. They present visualizations to app users so they can explore, analyze, and discover data. Sheets can be public or private.
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.
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.