# 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-TW/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 | 如果要使用不起始於 1 月的 (會計) 年度，可在 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-months 的值)，這讓搜尋期間從 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`」欄位使用`inmonths()`函數在前置 LOAD 陳述式中建立。提供的第一個引數為 `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-TW/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-examples1_and4-diagram.png)

5 月落在該年的第二季。因此，發生在 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`」的前置載入，這判定交易是否發生在 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`

結果表格

| 日期 | 上一季 |
| --- | --- |
| 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-TW/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 日相同的季度。

在此範例中，組織政策適用於 3 月作為會計年度第一個月份的情況。

載入指令碼

```
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 月)。

將 3 月設定為該年第一個月的 `inmonths()` 函數圖表。

將 3 月設定為該年第一個月的 `inmonths()` 函數圖表。

將 3 月設定為該年第一個月的 `inmonths()`inmonths() 函數圖表。 ![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-TW/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-TW/sense/May2026/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonths-examples1_and4-diagram.png)

5 月落在該年的第二季。因此，發生在 4 月 1 日和 6 月 30 日之間的任何交易將會傳回 TRUE 的布林值結果 。這在結果表格中驗證。

## 範例 5 - 情境

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

#### 載入指令碼和結果

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為「Products」之表格的資料集。

* 該表格含有下列欄位：

  * 產品 ID

  * 產品類型

  * 製造日期

  * 成本價格

最終使用者希望圖表依產品類型顯示在 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. 將量值的數字格式設定為金錢。

結果表格

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

inmonths() 函數使用使用者輸入作為其引數，以定義該年起始區段的大小。該函數傳遞每個產品的製造日期作為 inmonths() 函數的第二個引數。透過使用 1 月 1 日作為 inmonths() 函數的第三個引數，製造日期落在該年起始區段的產品將會傳回布林值 TRUE，因此 sum 函數將會加上這些產品的成本。