# inmonths 脚本和图表功能

此函数用于查找时间戳是否在作为基准日期的同一个月、两个月、季度、四个月期间或半年内。另外，它也可以用于判断时间戳是否位于上一个或下一个时间周期以内。

InMonths(n\_months, timestamp, base\_date, period\_no [, first\_month\_of\_year])

返回数据类型： 布尔值

在 Qlik Sense 中，布尔 true 值由 -1 表示，false 值由 0 表示。

`inmonths()` 函数的图表

`inmonths()` 函数的图表

`inmonths()`inmonths() 函数的图表 ![Diagram that shows the range of time in which the inmonths() function will evaluate a base date as true or false.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-diagram1.png)

`inmonths()` 函数根据提供的 `n_months` 参数将一年划分为若干段。然后，它确定评估的每个时间戳是否与 `base_date`参数属于同一段。但是，如果提供了 `period_no` 参数，该函数将确定时间戳是属于 `base_date` 的上一个时段还是下一个时段。

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

`n_month`n\_month 参数

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

适合使用它的情况

`inmonths()` 函数返回布尔结果。通常，这种类型的函数将用作 `if expression` 中的条件。通过使用 `inmonths()` 函数，您可以选择要评估的期间。例如，让用户识别在某一期间的月份、季度或半年内生产的产品。

返回数据类型： 布尔值

在 Qlik Sense 中，布尔 true 值由 -1 表示，false 值由 0 表示。

参数

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

可以使用以下值在 `first_month_of_year` 参数中设置一年中的第一个月：

first\_month\_of\_year 值

| 月 | 值 |
| --- | --- |
| 二月 | 2 |
| 三月 | 3 |
| 四月 | 4 |
| 五月 | 5 |
| 六月 | 6 |
| 七月 | 7 |
| 八月 | 8 |
| 九月 | 9 |
| 十月 | 10 |
| 十一月 | 11 |
| 十二月 | 12 |

## 区域设置

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

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

函数示例

| 示例 | 结果 |
| --- | --- |
| `inmonths(4, '01/25/2013', '04/25/2013', 0)` | 返回 TRUE。因为 时间戳，01/25/2013，在四个月内 01/01/2013 到 04/30/2013，其中 `base_date` 的值 04/25/2013 就位于其中。 |
| `inmonths(4, '05/25/2013', '04/25/2013', 0)` | 返回 FALSE。因为 05/25/2013 与前一个示例不在同一时间段内。 |
| `inmonths(4, '11/25/2012', '02/01/2013', -1 )` | 返回 TRUE。因为 `period_no` 的值 `-1` , 将搜索周期向后移动四个月（值为 n 个月），这表示搜索期间 09/01/2012 到 12/31/2012. |
| `inmonths(4, '05/25/2006', '03/01/2006', 0, 3)` | 返回 TRUE。因为 `first_month_of_year` 设置为 `3`, 这就是搜索期间 03/01/2006 到 07/30/2006 而不是 01/01/2006 到 04/30/2006. |

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

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 包含 2022 年交易集的 数据集 加载到名为 ‘`Transactions`’ 的表中。

* 带有附加 变量 ‘`in_months`’ 的 前一个加载，用于确定哪些交易发生在 2022 年 5 月 15 日的同一季度。

加载脚本

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
       *, 
       inmonths(3,date,'05/15/2022', 0) as in_months 
       ; 
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`
* `in_months`

结果表

| 日期 | in\_months |
| --- | --- |
| 2/19/2022 | 0 |
| 3/7/2022 | 0 |
| 3/30/2022 | 0 |
| 4/5/2022 | -1 |
| 4/16/2022 | -1 |
| 5/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/22/2022 | -1 |
| 6/15/2022 | -1 |
| 6/26/2022 | -1 |
| 7/9/2022 | 0 |
| 7/22/2022 | 0 |
| 7/23/2022 | 0 |
| 7/27/2022 | 0 |
| 8/2/2022 | 0 |
| 8/8/2022 | 0 |
| 8/19/2022 | 0 |
| 9/26/2022 | 0 |
| 10/14/2022 | 0 |
| 10/29/2022 | 0 |

‘`in_months`’ 字段是在前置 Load 语句中使用 `inmonths()` 函数创建的。提供的第一个参数是 `3` 其将一年划分为季度分段。第二个参数标识要计算的字段，本例中为日期字段。第三个参数是 5 月 15 日的硬编码日期，其为 `base_date` 并且为 `period_no` 的 `0` 是最后一个参数。

带季度段的 `inmonths()` 函数的图表。

带季度段的 `inmonths()` 函数的图表。

带季度段的 `inmonths()`inmonths() 函数的图表。 ![Diagram that shows the range of time the inmonths() function evaluates with May 15 as the base date, and the year divided into quarter segments.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-examples1_and4-diagram.png)

五月是一年的第二季度。因此，4 月 1 日至 6 月 30 日之间发生的任何交易都将返回布尔值结果 TRUE。这在结果表中得到验证。

## 示例 2 – period\_no

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 包含 2022 年交易集的数据集加载到名为 ‘`Transactions`’ 的表中。

* 带有附加变量 ‘`previous_quarter`’ 的前置 Load，用于确定交易是否发生在 2022 年 5 月 15 日之前的季度。

加载脚本

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
       *, 
       inmonths(3,date,'05/15/2022', -1) as previous_quarter 
       ; 
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`
* `previous_quarter`

结果表

| 日期 | previous quarter |
| --- | --- |
| 2/19/2022 | -1 |
| 3/7/2022 | -1 |
| 3/30/2022 | -1 |
| 4/5/2022 | 0 |
| 4/16/2022 | 0 |
| 5/1/2022 | 0 |
| 5/7/2022 | 0 |
| 5/22/2022 | 0 |
| 6/15/2022 | 0 |
| 6/26/2022 | 0 |
| 7/9/2022 | 0 |
| 7/22/2022 | 0 |
| 7/23/2022 | 0 |
| 7/27/2022 | 0 |
| 8/2/2022 | 0 |
| 8/8/2022 | 0 |
| 8/19/2022 | 0 |
| 9/26/2022 | 0 |
| 10/14/2022 | 0 |
| 10/29/2022 | 0 |

该函数将 `-1` 用作 `period_no` 函数中的 `inmonths()` 参数，以评估交易是否发生在一年的第一季度。5 月 15 日是 `base_date`，属于一年的第二季度（4 月至 6 月）。

带季度段的 `inmonths()` 函数的图表，以及 `period_no` 设置为 `-1`

带季度段的 `inmonths()` 函数的图表，以及 `period_no` 设置为 `-1`

带季度段的 `inmonths()`inmonths() 函数的图表，以及 `period_no`period\_no 设置为 `-1`-1 ![Diagram that shows the range of time the inmonths() function evaluates with May 15 as the base date, the year divided into quarter segments, and the period_no set -1 which sets the range of time back by one quarter.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-example2-diagram.png)

因此，1 月和 3 月之间发生的任何交易都将返回布尔值结果 TRUE。

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

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 包含 2022 年交易集的数据集加载到名为 ‘`Transactions`’ 的表中。

* 带有附加变量 ‘`in_months`’ 的前一个加载，用于确定哪些交易发生在 2022 年 5 月 15 日的同一季度。

在本例中，组织策略是将三月作为财政年度的第一个月。

加载脚本

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
       *, 
       inmonths(3,date,'05/15/2022', 0, 3) as in_months 
       ; 
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`
* `in_months`

结果表

| 日期 | in\_months |
| --- | --- |
| 2/19/2022 | 0 |
| 3/7/2022 | -1 |
| 3/30/2022 | -1 |
| 4/5/2022 | -1 |
| 4/16/2022 | -1 |
| 5/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/22/2022 | -1 |
| 6/15/2022 | 0 |
| 6/26/2022 | 0 |
| 7/9/2022 | 0 |
| 7/22/2022 | 0 |
| 7/23/2022 | 0 |
| 7/27/2022 | 0 |
| 8/2/2022 | 0 |
| 8/8/2022 | 0 |
| 8/19/2022 | 0 |
| 9/26/2022 | 0 |
| 10/14/2022 | 0 |
| 10/29/2022 | 0 |

在 `inmonths()` 函数中，通过使用 `3` 作为 `first_month_of_year` 参数，该函数将年份从 3 月 1 日开始。`inmonths()` 函数接着将年份划分为季度：3-5 月、6-8 月、9-11 月、12-2 月。因此，5 月 15 日属于一年的第一季度（3 月 - 5 月）。

`inmonths()` 函数的图表，3 月为一年中的第一个月。

`inmonths()` 函数的图表，3 月为一年中的第一个月。

`inmonths()`inmonths() 函数的图表，3 月为一年中的第一个月。 ![Diagram that shows the range of time the inmonths() function evaluates with May 15 as the base date, the year divided into quarter segments, and March set as the first month of the year.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-example3-diagram.png)

这几个月内发生的任何交易都将返回布尔值结果 TRUE。

## 示例 4 – 图表对象示例

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

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

概述

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

然而，在本例中，未更改的数据集被加载到应用程序中。确定交易是否发生在 2022 年 5 月 15 日的同一季度的计算是在应用程序的图表中创建的一个度量。

加载脚本

```
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`

要计算交易是否与 5 月 15 日发生在同一季度，请创建以下度量：

`=inmonths(3,date,'05/15/2022', 0)`

结果表

| 日期 | =inmonths(3,date,'05/15/2022', 0) |
| --- | --- |
| 2/19/2022 | 0 |
| 3/7/2022 | 0 |
| 3/30/2022 | 0 |
| 4/5/2022 | -1 |
| 4/16/2022 | -1 |
| 5/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/22/2022 | -1 |
| 6/15/2022 | -1 |
| 6/26/2022 | -1 |
| 7/9/2022 | 0 |
| 7/22/2022 | 0 |
| 7/23/2022 | 0 |
| 7/27/2022 | 0 |
| 8/2/2022 | 0 |
| 8/8/2022 | 0 |
| 8/19/2022 | 0 |
| 9/26/2022 | 0 |
| 10/14/2022 | 0 |
| 10/29/2022 | 0 |

使用 `in_months` 函数在图表中创建 ‘`inmonths()`’ 字段。提供的第一个参数是 `3` 其将一年划分为季度分段。第二个参数标识要计算的字段，本例中为日期字段。第三个参数是 5 月 15 日的硬编码日期，其为 `base_date` 并且为 `period_no` 的 `0` 是最后一个参数。

带季度段的 `inmonths()` 函数的图表。

带季度段的 `inmonths()` 函数的图表。

带季度段的 `inmonths()`inmonths() 函数的图表。 ![Diagram that shows the range of time the inmonths() function evaluates with May 15 as the base date, and the year divided into quarter segments.](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-examples1_and4-diagram.png)

五月是一年的第二季度。因此，4 月 1 日至 6 月 30 日之间发生的任何交易都将返回布尔值结果 TRUE。这在结果表中得到验证。

## 示例 5 – 场景

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 加载到名为 ‘Products’ 的表中的数据集。

* 表格包含以下字段：

  * product ID

  * 产品类型

  * manufacture date

  * cost price

最终用户想要一个图表，按产品类型显示 2021 年第一阶段制造的产品的成本。用户希望能够定义此段的长度。

加载脚本

```
SET vPeriod = 1; 

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

结果

加载数据并打开工作表。

在加载脚本开始时，创建了一个变量 vPeriod，该变量将绑定到变量输入控件。

进行以下操作：

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

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

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

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

5. 在值下，单击动态值。输入以下内容：

   ```
   ='1~month|2~bi-month|3~quarter|4~tertial|6~half-year'.
   ```

6. 将新表添加到工作表中。

7. 在属性面板中的数据下，将product\_type添加为维度。

8. 添加以下表达式作为度量：

   =sum(if(inmonths($(vPeriod),manufacture\_date,makedate(2022,01,01),0),cost\_price,0))

9. 将度量的数字格式设置为金额。

结果表

| product\_type | =sum(if(inmonths($(vPeriod),manufacture\_date,makedate(2022,01,01),0),cost\_price,0)) |
| --- | --- |
| 产品 A | $88.27 |
| product B | $37.23 |
| product C | $17.17 |
| product D | $0.00 |

inmonths() 函数使用用户输入作为参数来定义一年起始段的大小。函数将每个产品的生产日期作为 inmonths() 函数的第二个参数传递。通过使用 1 月 1 日作为 inmonths() 函数中的第三个参数，生产日期位于年初段的产品将返回布尔值 TRUE，因此总和函数将添加这些产品的成本。