# monthsstart 脚本和图表功能

此函数用于返回与包含基准日期的一个月、两个月、季度、四个月期间或半年的第一毫秒的时间戳对应的值。另外，它也可以用于判断上一个或下一个时间周期的时间戳。默认的输出格式是脚本中 DateFormat 设置的格式。

MonthsStart(n\_months, date[, period\_no [, first\_month\_of\_year]])

返回数据类型： 双

`monthsstart()` 函数的图表

`monthsstart()` 函数的图表

`monthsstart()`monthsstart() 函数的图表 ![Example diagram showing how each of the elements of the function work together to return the timestamp result. The year is broken up into a number of segments and given a date input, the function returns a timestamp value.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-monthsstart-general.png)

`monthsstart()` 函数根据提供的 `n_months` 参数将一年划分为若干段。然后，它评估所提供的每个日期属于哪个段，并以日期格式返回该段的第一毫秒。该函数还提供了从前面或后面的段返回开始时间戳的功能，以及重新定义一年中的第一个月。

函数中提供了一年中的以下时段作为 `n_month` 参数：

可能的 `n_month`n\_month 参数

| 期间 | 月数 |
| --- | --- |
| 月 | 1 |
| 双月 | 2 |
| 季 | 3 |
| 四个月 | 4 |
| 半年 | 6 |

参数

| 参数 | 描述 |
| --- | --- |
| n\_months | 用于定义时段的月数。整数或解算为整数的表达式必须为下列之一：1（相当于 inmonth() 函数）、2（两个月）、3（相当于 inquarter()函数）、4（四个月期间）或 6（半年）。 |
| date | 要评估的日期或时间戳。 |
| period\_no | 该周期可通过 period\_no 偏移，其为整数，或解算为整数的表达式，其中值 0 表示该周期包含 base\_date。period\_no 为负数表示前几个时段，为正数则表示随后的几个时段。 |
| first\_month\_of\_year | 如果您不想从一月开始处理（财政）年，可在 first\_month\_of\_year 中指定一个介于 2 和 12 之间的值。 |

## 适合使用它的情况

当用户希望计算使用当前期间尚未发生的分数时，`monthsstart()` 函数通常用作表达式的一部分。例如，这可以用来提供一个输入变量，让用户计算到目前为止该月、季度或半年累计的总利息。

函数示例

| 示例 | 结果 |
| --- | --- |
| `monthsstart(4, '10/19/2013')` | 返回 09/01/2013。 |
| `monthsstart(4, '10/19/2013, -1)` | 返回 05/01/2013。 |
| `monthsstart(4, '10/19/2013', 0, 2<br>)` | 返回 10/01/2013，因为该年度的开始时间变成 2 月。 |

## 区域设置

除非另有规定，本主题中的示例使用以下日期格式：MM/DD/YYYY。日期格式已经在数据加载脚本中的 `SET DateFormat` 语句中指定。由于区域设置和其他因素，系统中的默认日期格式可能有所不同。您可以更改以下示例中的格式以满足您的要求。或者，您可以更改加载脚本中的格式以匹配这些示例。

应用程序中的默认区域设置基于安装 Qlik Sense 的计算机或服务器的区域系统设置。如果您访问的 Qlik Sense 服务器设置为瑞典，则数据加载编辑器将使用瑞典地区设置的日期、时间、和货币。这些区域格式设置与 Qlik Sense 用户界面中显示的语言无关。Qlik Sense 将以您正在使用的浏览器所用的相同语言显示。

## 示例 1 – 没有其他参数

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/monthsstart.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 一个包含 2022 年交易集的 数据集，该数据集加载到名为 `Transactions` 的表中。

* 日期字段已以 `DateFormat` 系统变量 (MM/DD/YYYY) 格式提供。

* 创建一个字段 `bi_monthly_start`，将交易分组为两个月一次的时段，并返回每个交易时段的开始时间戳。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions:
    Load
        *,
        monthsstart(2,date) as bi_monthly_start,
        timestamp(monthsstart(2,date)) as bi_monthly_start_timestamp
        ;
Load
*
Inline
[
id,date,amount
8188,2/19/2022,37.23
8189,3/7/2022,17.17
8190,3/30/2022,88.27
8191,4/5/2022,57.42
8192,4/16/2022,53.80
8193,5/1/2022,82.06
8194,5/7/2022,40.39
8195,5/22/2022,87.21
8196,6/15/2022,95.93
8197,6/26/2022,45.89
8198,7/9/2022,36.23
8199,7/22/2022,25.66
8200,7/23/2022,82.77
8201,7/27/2022,69.98
8202,8/2/2022,76.11
8203,8/8/2022,25.12
8204,8/19/2022,46.23
8205,9/26/2022,84.21
8206,10/14/2022,96.24
8207,10/29/2022,67.67
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `bi_monthly_start`

* `bi_monthly_start_timestamp`

结果表

| 日期 | 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 |

`bi_monthly_start` 字段是在 前置 Load 语句中使用 `monthsstart()` 函数创建的。提供的第一个参数是 2，将一年分成两个月的段。第二个参数标识要计算的字段。

`monthsstart()` 函数图表，示例没有额外参数

`monthsstart()` 函数图表，示例没有额外参数

`monthsstart()`monthsstart() 函数图表，示例没有额外参数 ![Diagram showing the results of using the monthsstart function to determine the segment of the year in which a transaction took place. In this case, the year is broken up into segments and the input date is converted to a timestamp.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-monthsstart-example-no-add-arg.png)

交易 8195 发生在 5 月 22 日。`monthsstart()` 函数最初将一年划分为两个月的段。交易 8195 发生在 5 月至 6 月期间。因此，该函数返回此段的第一毫秒，即 2022 年 5 月 1 日中午 12:00:00。

## 示例 2 – period\_no

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/monthsstart.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 与第一个示例相同的数据集和场景。

* 创建一个字段 `prev_bi_monthly_start`，该字段返回交易发生前的双月段的第一毫秒。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions:
    Load
        *,
        monthsstart(2,date,-1) as prev_bi_monthly_start,
	    timestamp(monthsstart(2,date,-1)) as prev_bi_monthly_start_timestamp
	    ;
Load
*
Inline
[
id,date,amount
8188,2/19/2022,37.23
8189,3/7/2022,17.17
8190,3/30/2022,88.27
8191,4/5/2022,57.42
8192,4/16/2022,53.80
8193,5/1/2022,82.06
8194,5/7/2022,40.39
8195,5/22/2022,87.21
8196,6/15/2022,95.93
8197,6/26/2022,45.89
8198,7/9/2022,36.23
8199,7/22/2022,25.66
8200,7/23/2022,82.77
8201,7/27/2022,69.98
8202,8/2/2022,76.11
8203,8/8/2022,25.12
8204,8/19/2022,46.23
8205,9/26/2022,84.21
8206,10/14/2022,96.24
8207,10/29/2022,67.67
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `prev_bi_monthly_start`

* `prev_bi_monthly_start_timestamp`

结果表

| 日期 | 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 |

通过在 `period_no` 函数中使用 -1 作为 `monthsstart()` 参数，在最初将一年划分为双月段之后，该函数返回第一个双月段的最后一毫秒，直到交易发生。

`monthsstart()` 函数的图表，period\_no 示例

`monthsstart()` 函数的图表，period\_no 示例

`monthsstart()`monthsstart() 函数的图表，period\_no 示例 ![Diagram showing the results of using the monthsstart function to determine the segment of the year in which a transaction took place. In this case, the year is broken up into segments and the input date is converted to a timestamp.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-monthsstart-example-period-no.png)

交易 8195 发生在 5 月至 6 月期间。因此，上一个双月段是在 3 月 1 日至 4 月 30 日之间，因此函数返回该段的第一毫秒，即 2022 年 3 月 1 号中午 12:00:00。

## 示例 3 – first\_month\_of\_year

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/monthsstart.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 与第一个示例相同的数据集和场景。

* 创建一个字段 `bi_monthly_start`，将交易分组为两个月一次的时段，并返回每个交易集的开始时间戳。

然而，在本例中，我们还需要将 4 月设置为财政年度的第一个月。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions:
    Load
        *,
        monthsstart(2,date,0,4) as bi_monthly_start,
        timestamp(monthsstart(2,date,0,4)) as bi_monthly_start_timestamp
        ;
Load
*
Inline
[
id,date,amount
8188,1/7/2022,17.17
8189,1/19/2022,37.23
8190,2/28/2022,88.27
8191,2/5/2022,57.42
8192,3/16/2022,53.80
8193,4/1/2022,82.06
8194,5/7/2022,40.39
8195,5/16/2022,87.21
8196,6/15/2022,95.93
8197,6/26/2022,45.89
8198,7/9/2022,36.23
8199,7/22/2022,25.66
8200,7/23/2022,82.77
8201,7/27/2022,69.98
8202,8/2/2022,76.11
8203,8/8/2022,25.12
8204,8/19/2022,46.23
8205,9/26/2022,84.21
8206,10/14/2022,96.24
8207,10/29/2022,67.67
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `bi_monthly_start`

* `bi_monthly_start_timestamp`

结果表

| 日期 | 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 |

通过在 `monthsstart()` 函数中使用 4 作为 `first_month_of_year` 参数，函数从 4 月 1 日开始一年。它随后将一年划分为双月段：Apr-May,Jun-Jul,Aug-Sep,Oct-Nov,Dec-Jan,Feb-Mar。

`monthsstart()` 函数 first\_month\_of\_year 示例的图表

`monthsstart()` 函数 first\_month\_of\_year 示例的图表

`monthsstart()`monthsstart() 函数 first\_month\_of\_year 示例的图表 ![Diagram showing the results of using the monthsstart function to determine the segment of the year in which a transaction took place. In this case, the year is broken up into segments and the input date is converted to a timestamp.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-monthsstart-example-first-mth-yr.png)

交易 8195 发生在 5 月 22 日，属于 4 月 1 日至 5 月 31 日期间。因此，该函数返回此段的第一毫秒，即 2022 年 4 月 1 日中午 12:00:00。

## 示例 4 – 图表对象

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/monthsstart.htm)

#### 加载脚本和图表表达式

概述

打开数据加载编辑器，并将下面的加载脚本添加到新选项卡。

加载脚本包含与第一个示例相同的数据集和场景。

然而，在本例中，未更改的数据集被加载到应用程序中。将季度分组为两个月一次的段并返回每个季度的开始时间戳的计算是作为应用程序的图表对象中的度量创建的。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions:
Load
*
Inline
[
id,date,amount
8188,2/19/2022,37.23
8189,3/7/2022,17.17
8190,3/30/2022,88.27
8191,4/5/2022,57.42
8192,4/16/2022,53.80
8193,5/1/2022,82.06
8194,5/7/2022,40.39
8195,5/22/2022,87.21
8196,6/15/2022,95.93
8197,6/26/2022,45.89
8198,7/9/2022,36.23
8199,7/22/2022,25.66
8200,7/23/2022,82.77
8201,7/27/2022,69.98
8202,8/2/2022,76.11
8203,8/8/2022,25.12
8204,8/19/2022,46.23
8205,9/26/2022,84.21
8206,10/14/2022,96.24
8207,10/29/2022,67.67
];
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度： date。

创建以下度量：

=monthsstart(2,date)

=timestamp(monthsstart(2,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 |

monthsstart() 函数的图表，图表对象示例

monthsstart() 函数的图表，图表对象示例

monthsstart()monthsstart() 函数的图表，图表对象示例 ![Diagram showing the results of using the monthsstart function to determine the segment of the year in which a transaction took place. In this case, the year is broken up into segments and the input date is converted to a timestamp.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-monthsstart-example-chart-object.png)

交易 8195 发生在 5 月 22 日。monthsstart() 函数最初将一年划分为两个月的段。交易 8195 发生在 5 月至 6 月期间。因此，该函数返回此段的第一毫秒，即 2022 年 5 月 1 日中午 12:00:00。

## 示例 5 – 场景

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/monthsstart.htm)

#### 加载脚本和图表表达式

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 包含一组贷款余额的数据集，该数据集加载到名为 Loans 的表中。

* 数据包括贷款 ID、月初余额和每年每笔贷款的简单利率。

最终用户想要一个图表对象，该对象按贷款 ID 显示在他们选择的时期内每个贷款的当前利息。财政年度从一月份开始。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Loans:
Load
*
Inline
[
loan_id,start_balance,rate
8188,$10000.00,0.024
8189,$15000.00,0.057
8190,$17500.00,0.024
8191,$21000.00,0.034
8192,$90000.00,0.084
];
```

结果

加载数据并打开工作表。

在加载脚本开始时，创建了一个变量 (vPeriod)，该变量将绑定到变量输入控件。接下来，将变量配置为工作表中的自定义对象。

执行以下操作：

1. 在资产面板中，单击 图表。

2. 在 仪表板对象 下，将 变量输入 拖动到工作表上。

3. 输入图表对象的标题。

4. 在变量下，选择 vPeriod 作为名称，并将对象设置为显示为下拉列表。

5. 在值下，将对象配置为使用动态值。输入以下内容：  
   ='1~month|2~bi-month|3~quarter|4~tertial|6~half-year'

接下来，创建结果表。

执行以下操作：

1. 新建表格。添加以下字段作为维度：

   * employee\_id

   * employee\_name

2. 创建一个度量来计算累计利息：

   =start\_balance\*(rate\*(today(1)-monthsstart($(vPeriod),today(1)))/365)

3. 将度量的数字格式设置为金额。 单击 the 完成编辑 icon。现在可以通过调整变量对象中的时间段来修改表中显示的数据。

这是选择 month 期间选项时结果表的外观：

结果表

| loan\_id | start\_balance | =start\_balance\*(rate\*(today(1)-monthsstart($(vPeriod),today(1)))/365) |
| --- | --- | --- |
| 8188 | $10000.00 | $7.95 |
| 8189 | $15000.00 | $67.93 |
| 8190 | $17500.00 | $33.37 |
| 8191 | $21000.00 | $56.73 |
| 8192 | $90000.00 | $600.66 |

monthsstart() 函数，使用用户的输入作为其第一个参数，使用今天的日期作为其第二个参数，返回用户选择的期间的开始日期。通过从当前日期中减去该结果，表达式将返回本期间迄今为止经过的天数。

然后将该值乘以利率并除以 365，以返回该期间产生的实际利率。然后将结果乘以贷款的起始余额，以返回本期间迄今为止累计的利息。