This function returns a value corresponding to the timestamp of the first millisecond of the month, bi-month, quarter, four-month period, or half-year containing a base date. It is also possible to find the timestamp for a previous or following time period.The default output format is the DateFormat set in the script.
The monthsstart() function divides the year into segments based on the n_months argument provided. It then evaluates what segment each date provided falls into and returns the first millisecond of that segment, in date format. The function also provides the ability to return the start timestamp from preceding or following segments, as well as redefining which is the first month of the year.
The following segments of the year are available in the function as n_month arguments:
Possible n_month arguments
Periods
Number of months
month
1
bi-month
2
quarter
3
four months
4
half-year
6
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).
date
The date or timestamp to evaluate.
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.
When to use it
The monthsstart() function is commonly used as part of an expression when the user would like the calculation to use the fraction of a period that has not yet occurred. This could be used, for example, to provide an input variable to let the user calculate the total interest that has been accumulated so far in the month, quarter, or half-year.
Function examples
Example
Result
monthsstart(4, '10/19/2013')
Returns
09/01/2013.
monthsstart(4, '10/19/2013, -1)
Returns 05/01/2013.
monthsstart(4, '10/19/2013', 0, 2
)
Returns 10/01/2013, because the start of the year becomes month 2.
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.
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, which is loaded into a table called Transactions.
The date field provided in the DateFormat system variable (MM/DD/YYYY) format.
The creation of a field, bi_monthly_start, that groups transactions into bi-monthly segments and returns the starting timestamp of the segment for each transaction.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
bi_monthly_start
bi_monthly_start_timestamp
Results table
date
bi_monthly_start
bi_monthly_start_timestamp
2/19/2022
01/01/2022
1/1/2022 12:00:00 AM
3/7/2022
03/01/2022
3/1/2022 12:00:00 AM
3/30/2022
03/01/2022
3/1/2022 12:00:00 AM
4/5/2022
03/01/2022
3/1/2022 12:00:00 AM
4/16/2022
03/01/2022
3/1/2022 12:00:00 AM
5/1/2022
05/01/2022
5/1/2022 12:00:00 AM
5/7/2022
05/01/2022
5/1/2022 12:00:00 AM
5/22/2022
05/01/2022
5/1/2022 12:00:00 AM
6/15/2022
05/01/2022
5/1/2022 12:00:00 AM
6/26/2022
05/01/2022
5/1/2022 12:00:00 AM
7/9/2022
07/01/2022
7/1/2022 12:00:00 AM
7/22/2022
07/01/2022
7/1/2022 12:00:00 AM
7/23/2022
07/01/2022
7/1/2022 12:00:00 AM
7/27/2022
07/01/2022
7/1/2022 12:00:00 AM
8/2/2022
07/01/2022
7/1/2022 12:00:00 AM
8/8/2022
07/01/2022
7/1/2022 12:00:00 AM
8/19/2022
07/01/2022
7/1/2022 12:00:00 AM
9/26/2022
09/01/2022
9/1/2022 12:00:00 AM
10/14/2022
09/01/2022
9/1/2022 12:00:00 AM
10/29/2022
09/01/2022
9/1/2022 12:00:00 AM
The bi_monthly_start field is created in the preceding load statement by using the monthsstart() function. The first argument provided is 2, dividing the year into bi-monthly segments. The second argument identifies which field is being evaluated.
Transaction 8195 takes place on May 22. The monthsstart() function initially divides the year into bi-monthly segments. Transaction 8195 falls into the segment between May 1 and June 30. Therefore, the function returns the first millisecond of this segment, May 1, 2022 at 12:00:00 AM.
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, prev_bi_monthly_start, that returns the first millisecond of the bi-monthly segment before the transaction took place.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
prev_bi_monthly_start
prev_bi_monthly_start_timestamp
Results table
date
prev_bi_monthly_start
prev_bi_monthly_start_timestamp
2/19/2022
11/01/2021
11/1/2021 12:00:00 AM
3/7/2022
01/01/2022
1/1/2022 12:00:00 AM
3/30/2022
01/01/2022
1/1/2022 12:00:00 AM
4/5/2022
01/01/2022
1/1/2022 12:00:00 AM
4/16/2022
01/01/2022
1/1/2022 12:00:00 AM
5/1/2022
03/01/2022
3/1/2022 12:00:00 AM
5/7/2022
03/01/2022
3/1/2022 12:00:00 AM
5/22/2022
03/01/2022
3/1/2022 12:00:00 AM
6/15/2022
03/01/2022
3/1/2022 12:00:00 AM
6/26/2022
03/01/2022
3/1/2022 12:00:00 AM
7/9/2022
05/01/2022
5/1/2022 12:00:00 AM
7/22/2022
05/01/2022
5/1/2022 12:00:00 AM
7/23/2022
05/01/2022
5/1/2022 12:00:00 AM
7/27/2022
05/01/2022
5/1/2022 12:00:00 AM
8/2/2022
05/01/2022
5/1/2022 12:00:00 AM
8/8/2022
05/01/2022
5/1/2022 12:00:00 AM
8/19/2022
05/01/2022
5/1/2022 12:00:00 AM
9/26/2022
07/01/2022
7/1/2022 12:00:00 AM
10/14/2022
07/01/2022
7/1/2022 12:00:00 AM
10/29/2022
07/01/2022
7/1/2022 12:00:00 AM
By using -1 as the period_no argument in the monthsstart() function, after initially dividing a year into bi-monthly segments, the function then returns the first millisecond of the previous bi-monthly segment to when a transaction takes place.
Transaction 8195 occurs in the segment between May and June. Therefore, the previous bi-monthly segment was between March 1 and April 30, so the function returns the first millisecond of this segment, March 1, 2022 at 12:00:00 AM.
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, bi_monthly_start, that groups transactions into bi-monthly segments and returns the starting timestamp of the set for each transaction.
However, in this example, we also need to set April as 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
bi_monthly_start
bi_monthly_start_timestamp
Results table
date
bi_monthly_start
bi_monthly_start_timestamp
2/19/2022
02/01/2022
2/1/2022 12:00:00 AM
3/7/2022
02/01/2022
2/1/2022 12:00:00 AM
3/30/2022
02/01/2022
2/1/2022 12:00:00 AM
4/5/2022
04/01/2022
4/1/2022 12:00:00 AM
4/16/2022
04/01/2022
4/1/2022 12:00:00 AM
5/1/2022
04/01/2022
4/1/2022 12:00:00 AM
5/7/2022
04/01/2022
4/1/2022 12:00:00 AM
5/22/2022
04/01/2022
4/1/2022 12:00:00 AM
6/15/2022
06/01/2022
6/1/2022 12:00:00 AM
6/26/2022
06/01/2022
6/1/2022 12:00:00 AM
7/9/2022
06/01/2022
6/1/2022 12:00:00 AM
7/22/2022
06/01/2022
6/1/2022 12:00:00 AM
7/23/2022
06/01/2022
6/1/2022 12:00:00 AM
7/27/2022
06/01/2022
6/1/2022 12:00:00 AM
8/2/2022
08/01/2022
8/1/2022 12:00:00 AM
8/8/2022
08/01/2022
8/1/2022 12:00:00 AM
8/19/2022
08/01/2022
8/1/2022 12:00:00 AM
9/26/2022
08/01/2022
8/1/2022 12:00:00 AM
10/14/2022
10/01/2022
10/1/2022 12:00:00 AM
10/29/2022
10/01/2022
10/1/2022 12:00:00 AM
By using 4 as the first_month_of_year argument in the monthsstart() function, the function begins the year on April 1. It then divides the year into bi-monthly segments: Apr-May,Jun-Jul,Aug-Sep,Oct-Nov,Dec-Jan,Feb-Mar.
Transaction 8195 took place on May 22 and falls into the segment between April 1 and May 31. Therefore, the function returns the first millisecond of this segment, April 1, 2022 at 12:00:00 AM.
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 which groups transactions into bi-monthly segments and returns the starting timestamp of the set for each transaction is created as a measure in a chart object of the application.
Load the data and open a sheet. Create a new table and add this field as a dimension: date.
Create the following measures:
=monthsstart(2,date)
=timestamp(monthsstart(2,date))
These calculations will retrieve the starting timestamp of the bi-monthly segment in which each transaction took place.
Results table
date
=monthsstart(2,date)
=timestamp(monthsstart(2,date))
9/26/2022
09/01/2022
9/1/2022 12:00:00 AM
10/14/2022
09/01/2022
9/1/2022 12:00:00 AM
10/29/2022
09/01/2022
9/1/2022 12:00:00 AM
7/9/2022
07/01/2022
7/1/2022 12:00:00 AM
7/22/2022
07/01/2022
7/1/2022 12:00:00 AM
7/23/2022
07/01/2022
7/1/2022 12:00:00 AM
7/27/2022
07/01/2022
7/1/2022 12:00:00 AM
8/2/2022
07/01/2022
7/1/2022 12:00:00 AM
8/8/2022
07/01/2022
7/1/2022 12:00:00 AM
8/19/2022
07/01/2022
7/1/2022 12:00:00 AM
5/1/2022
05/01/2022
5/1/2022 12:00:00 AM
5/7/2022
05/01/2022
5/1/2022 12:00:00 AM
5/22/2022
05/01/2022
5/1/2022 12:00:00 AM
6/15/2022
05/01/2022
5/1/2022 12:00:00 AM
6/26/2022
05/01/2022
5/1/2022 12:00:00 AM
3/7/2022
03/01/2022
3/1/2022 12:00:00 AM
3/30/2022
03/01/2022
3/1/2022 12:00:00 AM
4/5/2022
03/01/2022
3/1/2022 12:00:00 AM
4/16/2022
03/01/2022
3/1/2022 12:00:00 AM
2/19/2022
01/01/2022
1/1/2021 12:00:00 AM
Transaction 8195 took place on May 22. The monthsstart() function initially divides the year into bi-monthly segments. Transaction 8195 falls into the segment between May 1 and June 30. Therefore, the function returns the first millisecond of this segment, 05/01/2022 12:00:00 AM.
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 containing a set of loan balances, which is loaded into a table called Loans.
Data consisting of loan IDs, the balance at the start of the month, and the simple interest rate charged on each loan per annum.
The end user would like a chart object that displays, by loan ID, the current interest that has been accrued on each loan for the period of their choosing. The financial year begins in January.
At the start of the load script, a variable (vPeriod) has been created that will be tied to the variable input control. Next, configure the variable as a custom object in the sheet.
Do the following:
In the assets panel, click Custom objects.
Select Qlik Dashboard bundle, and create a Variable input object.
Enter a title for the chart object.
Under Variable, select vPeriod as the Name and set the object to show as a Drop down.
Under Values, configure the object to use dynamic values. Enter the following: ='1~month|2~bi-month|3~quarter|4~tertial|6~half-year'
Next, create the results table.
Do the following:
Create a new table. Add the following fields as dimensions.
employee_id
employee_name
Create a measure to calculate the accumulated interest:
Set the measure's Number formatting to Money. Click Done editing. You can now modify the data shown in the table by adjusting the time segment in the variable object.
This is what the results table will look like when the month period option is selected:
The monthsstart() function, using the user’s input as its first argument and today’s date as its second argument, returns the start date of the period of the user’s choosing. By subtracting that result from the current date, the expression returns the number of days that have elapsed so far in this period.
This value is then multiplied by the interest rate and divided by 365 to return the effective interest rate incurred for this period. The result is then multiplied by the starting balance of the loan to return the interest that has been accrued so far this period.
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!