Skip to main content

monthsname - script and chart function

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.

Syntax:  

MonthsName(n_months, date[, period_no[, first_month_of_year]])

Return data type: dual

Diagram of monthsname function

Example diagram showing the range of months  returned by the monthsname function, given a specific input 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 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. For more information, see Modifying app regional settings.

Default regional settings in apps are based on a combination of identity provider settings, system settings, or browser settings. 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.

Example 1 – Basic example

Example 2 – period_no

Example 3 – first_month_of_year

Example 4 – Chart object example

Example 5 – Scenario