This function returns
a display value representing the range of the months of the period (formatted according
to the MonthNames script variable) as well as the year. The underlying numeric
value corresponds to a timestamp of the first millisecond of the month, bi-month, quarter, four-month period, or half-year containing a base date.
The monthsname() function divides the year into segments based on the n_months agrument provided. It then evaluates the segment to which each provided date belongs, and returns the start and end month names of that segment, as well as the year. The function also provides the ability to return these boundaries 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 monthsname() function is useful when you would like to provide the user with the functionality to compare aggregations by a period of their choosing. For example, you could provide an input variable to let the user see the total sales of products by month, quarter, or half-year.
These dimensions can be created either in the load script by adding the function as a field in a Master Calendar table, or alternatively, by creating the dimension directly in a chart as a calculated dimension.
Function examples
Example
Result
monthsname(4, '10/19/2013')
Returns 'Sep-Dec 2013.' In this and the other examples, the SET Monthnames statement is set to Jan;Feb;Mar, and so on.
monthsname(4, '10/19/2013', -1)
Returns 'May-Aug 2013'.
monthsname(4, '10/19/2013', 0, 2)
Returns 'Oct-Jan 2014', since the year is specified to begin in month 2. Therefore, the four-month period ends on the first month of the following 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.
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 – Basic example
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_range, that groups transactions into bi-monthly segments and returns the boundary names of that segment for each transaction.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
bi_monthly_range
Results table
date
bi_monthly_range
2/19/2022
Jan-Feb 2022
3/7/2022
Mar-Apr 2022
3/30/2022
Mar-Apr 2022
4/5/2022
Mar-Apr 2022
4/16/2022
Mar-Apr 2022
5/1/2022
May-Jun 2022
5/7/2022
May-Jun 2022
5/22/2022
May-Jun 2022
6/15/2022
May-Jun 2022
6/26/2022
May-Jun 2022
7/9/2022
Jul-Aug 2022
7/22/2022
Jul-Aug 2022
7/23/2022
Jul-Aug 2022
7/27/2022
Jul-Aug 2022
8/2/2022
Jul-Aug 2022
8/8/2022
Jul-Aug 2022
8/19/2022
Jul-Aug 2022
9/26/2022
Sep-Oct 2022
10/14/2022
Sep-Oct 2022
10/29/2022
Sep-Oct 2022
The bi_monthly_range field is created in the preceding load statement by using the monthsname() 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 monthsname() 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 these months in the MonthNames system variable format, as well as the year, May-Jun 2022.
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 inline dataset and scenario as the first example.
The creation of a field, prev_bi_monthly_range, that groups transactions into bi-monthly segments and returns the previous segment boundary names for each transaction.
Add your other text here, as needed, with lists etc.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
prev_bi_monthly_range
Results table
date
prev_bi_monthly_range
2/19/2022
Nov-Dec 2021
3/7/2022
Jan-Feb 2022
3/30/2022
Jan-Feb 2022
4/5/2022
Jan-Feb 2022
4/16/2022
Jan-Feb 2022
5/1/2022
Mar-Apr 2022
5/7/2022
Mar-Apr 2022
5/22/2022
Mar-Apr 2022
6/15/2022
Mar-Apr 2022
6/26/2022
Mar-Apr 2022
7/9/2022
May-Jun 2022
7/22/2022
May-Jun 2022
7/23/2022
May-Jun 2022
7/27/2022
May-Jun 2022
8/2/2022
May-Jun 2022
8/8/2022
May-Jun 2022
8/19/2022
May-Jun 2022
9/26/2022
Jul-Aug 2022
10/14/2022
Jul-Aug 2022
10/29/2022
Jul-Aug 2022
In this example, -1 is used as the period_no argument in the monthsname() function. After initially dividing a year into bi-monthly segments, the function then returns the previous segment boundaries for 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, and so the function returns Mar-Apr 2022.
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 inline dataset and scenario as the first example.
The creation of a different field, bi_monthly_range, that groups transactions into bi-monthly segments and returns the segment boundaries 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_range
Results table
date
bi_monthly_range
2/19/2022
Feb-Mar 2021
3/7/2022
Feb-Mar 2021
3/30/2022
Feb-Mar 2021
4/5/2022
Apr-May 2022
4/16/2022
Apr-May 2022
5/1/2022
Apr-May 2022
5/7/2022
Apr-May 2022
5/22/2022
Apr-May 2022
6/15/2022
Jun-Jul 2022
6/26/2022
Jun-Jul 2022
7/9/2022
Jun-Jul 2022
7/22/2022
Jun-Jul 2022
7/23/2022
Jun-Jul 2022
7/27/2022
Jun-Jul 2022
8/2/2022
Aug-Sep 2022
8/8/2022
Aug-Sep 2022
8/19/2022
Aug-Sep 2022
9/26/2022
Aug-Sep 2022
10/14/2022
Oct-Nov 2022
10/29/2022
Oct-Nov 2022
By using 4 as the first_month_of_year argument in the monthsname() 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.
Paragraph text for Results.
Transaction 8195 took place on May 22 and falls into the segment between April 1 and May 31. Therefore, the function returns Apr-May 2022.
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 inline dataset and scenario as the first example. However, in this example, the unchanged dataset is loaded into the application. The calculation that groups transactions into bi-monthly segments and returns the segment boundaries 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 measure:
=monthsname(2,date)
Results table
date
=monthsname(2,date)
2/19/2022
Jan-Feb 2022
3/7/2022
Mar-Apr 2022
3/30/2022
Mar-Apr 2022
4/5/2022
Mar-Apr 2022
4/16/2022
Mar-Apr 2022
5/1/2022
May-Jun 2022
5/7/2022
May-Jun 2022
5/22/2022
May-Jun 2022
6/15/2022
May-Jun 2022
6/26/2022
May-Jun 2022
7/9/2022
Jul-Aug 2022
7/22/2022
Jul-Aug 2022
7/23/2022
Jul-Aug 2022
7/27/2022
Jul-Aug 2022
8/2/2022
Jul-Aug 2022
8/8/2022
Jul-Aug 2022
8/19/2022
Jul-Aug 2022
9/26/2022
Sep-Oct 2022
10/14/2022
Sep-Oct 2022
10/29/2022
Sep-Oct 2022
The bi_monthly_range field is created as a measure in the chart object by using the monthsname() 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 monthsname() 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 these months in the MonthNames system variable format, as well as the year, May-Jun 2022.
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 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 end user would like a chart object that displays total sales by a period of their own choosing. This could be achieved even when this dimension is not available in the data model, using the monthsname() function as a calculated dimension that is dynamically modified by a variable input control.
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:
Create a new table and add the following calculated dimension:
=monthsname($(vPeriod),date)
Add this measure to calculate the total sales:
=sum(amount)
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 tertial option is selected:
Results table
monthsname($(vPeriod),date)
=sum(amount)
Jan-Apr 2022
253.89
May-Aug 2022
713.58
Sep-Dec 2022
248.12
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!