monthsend - script and chart function
This function returns a value corresponding to a timestamp of the last 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.
Syntax:
MonthsEnd(n_months, date[, period_no [, first_month_of_year]])
Return data type: dual
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. |
Examples and results:
These examples use the date format DD/MM/YYYY. The date format is specified in the SET DateFormat statement at the top of your load script. Change the format in the examples to suit your requirements.
Example 1:
monthsend(4, '19/07/2013')
Returns 31/08/2013.
Example 2:
monthsend(4, '19/10/2013', -1)
Returns 31/08/2013.
Example 3:
monthsend(4, '19/10/2013', 0, 2)
Returns 31/01/2014.
Because the start of the year becomes month 2.
Example 4:
Add the example script to your document and run it. Then add, at least, the fields listed in the results column to a sheet in your document to see the result.
This example finds the end of the final day of the bi-month period for each invoice date, shifted forwards by one bi-month period.
TempTable:
LOAD RecNo() as InvID, * Inline [
InvDate
28/03/2012
10/12/2012
5/2/2013
31/3/2013
19/5/2013
15/9/2013
11/12/2013
2/3/2014
14/5/2014
13/6/2014
7/7/2014
4/8/2014
];
InvoiceData:
LOAD *,
MonthsEnd(2, InvDate, 1) AS BiMthsEnd
Resident TempTable;
Drop table TempTable;
The resulting table contains the original dates and a column with the return value of the MonthsEnd() function.
InvDate | BiMthsEnd |
---|---|
28/03/2012 | 30/06/2012 |
10/12/2012 | 28/02/2013 |
5/2/2013 | 30/04/2013 |
31/3/2013 | 30/06/2013 |
19/5/2013 | 31/08/2013 |
15/9/2013 | 31/12/2013 |
11/12/2013 | 28/02/2014 |
2/3/2014 | 30/06/2014 |
14/5/2014 | 31/08/2014 |
13/6/2014 | 31/08/2014 |
7/7/2014 | 31/10/2014 |
4/8/2014 | 31/10/2014 |