# inmonthstodate 指令碼與圖表函數

此函數會發現時間戳記是否在月、雙月、季度、四月期或半年的期間內，且不超過 (可包含) `base_date` 的最後一毫秒。還可以發現時間戳記是否在前一個或後一個時間期間內。

語法：

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

傳回的資料類型： 布林值

`inmonthstodate` 函數的圖表。

`inmonthstodate` 函數的圖表。

`inmonthstodate`inmonthstodate 函數的圖表。 ![圖表顯示 inmonthstodate 函數可以如何用來識別時間戳記落在某個設定時間區段之內或之外。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-diagram1.png)

引數

| 引數 | 描述 |
| --- | --- |
| 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 之間的值。 |

在 `inmonthstodate()` 函數中，`base_date` 作為其中特定年區段的結束點運作。

例如，若該年分為幾個四個月區段，而 `base_date` 為 5 月 15 日，則 1 月開始和 4 月結束之間的任何時間戳記都會傳回布林值結果 FALSE。5 月 1 日和 5 月 15 日之間的日期會傳回 TRUE。該年的其餘部分會傳回 FALSE。

`inmonthstodate` 函數的布林值結果範圍圖表。

`inmonthstodate` 函數的布林值結果範圍圖表。

`inmonthstodate`inmonthstodate 函數的布林值結果範圍圖表。 ![圖表顯示 inmonthstodate 函數的布林值結果範圍。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-diagram2.png)

一年中的下列區段可作為 `n_month` 引數用於函數。

`n_month`n\_month 引數

| 期間 | 月數 |
| --- | --- |
| 月 | 1 |
| 兩個月 | 2 |
| 季 | 3 |
| 四個月 | 4 |
| 半年 | 6 |

何時使用 inmonthstodate

`inmonthstodate()` 函數會傳回布林值結果。通常，此函數類型會作為 `if expression` 中的條件使用。透過使用 `inmonthstodate()` 函數，您可以選取想要評估的期間。例如，提供輸入變數，用來讓使用者識別在某個期間截至特定日期的月份、季度或半年製造的產品。

## 區域設定

除非另有說明，否則此主題中的範例皆使用下列日期格式：MM/DD/YYYY。日期格式是在資料載入指令碼的 `SET DateFormat` 陳述式中指定。由於地區設定和其他因素，您系統中的預設日期格式可能會不同。您可以變更以下範例中的格式，以滿足您的需求。或者，您可以在載入指令碼中變更格式，以符合這些範例。如需詳細資訊，請參閱 [修改用於應用程式和指令碼的區域設定](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/Apps/regional-settings-in-apps.htm)。

應用程式中的預設地區設定是根據使用者設定檔。這些地區格式設定與 Qlik Cloud 使用者介面中顯示的語言無關。Qlik Cloud 顯示的語言將與您正在使用的瀏覽器相同。

若您是應用程式建立者，可以為您建立的應用程式設定預設地區。如需詳細資訊，請參閱[Qlik Cloud 分析 中設定用於建立應用程式和指令碼的偏好區域設定](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/UserInterface/Languages.htm#Setting)。

函數範例

| 範例 | 結果 |
| --- | --- |
| `inmonthstodate(4, '01/25/2013', '04/25/2013',<br>0)` | 傳回 True，因為 timestamp 的值 01/25/2013 在 01/01/2013 至 04/25/2013 結束的四個月期間內，base\_date 的值 04/25/2013 也在該期間內。 |
| `inmonthstodate(4, '04/26/2013', '04/25/2006',<br>0)` | 傳回 False，因為 04/26/2013 處在與前一個範例相同的期間之外。 |
| `inmonthstodate(4, '09/25/2005', '02/01/2006',<br>-1)` | 傳回 True，因為 period\_no 的值 -1 將搜尋期間往回偏移四個月期間 (n-months 的值)，這讓搜尋期間從 01/09/2005 變更為 02/01/2006。 |
| `inmonthstodate(4, '04/25/2006', '06/01/2006',<br>0, 3)` | 傳回 True，因為 first\_month\_of\_year 的值設為 3，這讓搜尋期間從 03/01/2006 變更為 06/01/2006，而不是 05/01/2006 變更為 06/01/2006。 |

## 範例 1 – 無其他引數

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

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

概述

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

載入指令碼包含：

* 包含一組 2022 年交易的資料集，這載入到稱為「`Transactions`」的表格中。

* 使用 `DateFormat` 系統變數 `(MM/DD/YYYY)` 格式的日期欄位。

* 前置 LOAD 陳述式包含：

  * 設為欄位「`in_months_to_date`」的 `inmonthstodate()` 函數。這決定哪些交易發生在 2022 年該季的 5 月 15 日之前。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
    *, 
    inmonthstodate(3,date,'05/15/2022', 0) as in_months_to_date 
    ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'1/19/2022',37.23 
8189,'1/7/2022',17.17 
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`
* `in_months_to_date`

結果表格

| 日期 | in\_months\_to\_date |
| --- | --- |
| 1/7/2022 | 0 |
| 1/19/2022 | 0 |
| 2/5/2022 | 0 |
| 2/28/2022 | 0 |
| 3/16/2022 | 0 |
| 4/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/16/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 |

「`in_months_to_date`」欄位使用 `inmonthstodate()` 函數在前置 LOAD 陳述式中建立。

提供的第一個引數為 `3`，將該年分為幾個季區段。第二個引數識別正在評估哪個欄位。第三個引數是 5 月 15 日的硬式編碼日期，即定義該區段結束邊界的 `base_date`。`0` 的 `period_no` 是最終引數。

`inmonthstodate` 函數的圖表，無其他引數。

`inmonthstodate` 函數的圖表，無其他引數。

`inmonthstodate`inmonthstodate 函數的圖表，無其他引數。 ![圖表顯示透過 inmonthstodate 函數識別為 TRUE 的交易範圍。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-diagram2.png)

發生在 4 月 1 日和 5 月 15 日之間的任何交易都會傳回布林值結果 TRUE。該期間之外的交易日期會傳回 FALSE。

## 範例 2 – period\_no

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

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

概述

使用與第一個範例相同的資料集和情境。

不過，在此範例中，任務是建立欄位「`previous_qtr_to_date`」，這判定交易是否發生在 5 月 15 日前一季。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
    *, 
    inmonthstodate(3,date,'05/15/2022', -1) as previous_qtr_to_date 
    ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'1/19/2022',37.23 
8189,'1/7/2022',17.17 
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`

* `previous_qtr_to_date`

結果表格

| 日期 | previous\_qtr\_to\_date |
| --- | --- |
| 1/7/2022 | -1 |
| 1/19/2022 | -1 |
| 2/5/2022 | -1 |
| 2/28/2022 | 0 |
| 3/16/2022 | 0 |
| 4/1/2022 | 0 |
| 5/7/2022 | 0 |
| 5/16/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` 作為 `inmonthstodate()` 函數中的 `period_no` 引數，函數會使用一季偏移比較子年區段的界限。

5 月 15 日落在該年的第二季，因此該區段原本等於 4 月 1 日和 5 月 15 日之間。`period_no` 引數將此區段偏移負三個月。日期邊界變成 1 月 1 日至 2 月 15 日。

period\_no 值設為 -1 的 `inmonthstodate` 函數圖表。

period\_no 值設為 -1 的 `inmonthstodate` 函數圖表。

period\_no 值設為 -1 的 `inmonthstodate`inmonthstodate 函數圖表。 ![圖表顯示 period_no 引數設為 -1 的交易範圍。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-example2-diagram.png)

因此，發生在 1 月 1 日和 2 月 15 日之間的任何交易將會傳回布林值結果 TRUE。

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

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

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

概述

使用與第一個範例相同的資料集和情境。

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

建立欄位「`in_months_to_date`」，這決定哪些交易發生在 2022 年同一季的 5 月 15 日之前。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
						
Transactions: 
    Load 
    *, 
    inmonthstodate(3,date,'05/15/2022', 0,3) as in_months_to_date 
    ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'1/19/2022',37.23 
8189,'1/7/2022',17.17 
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`

* `in_months_to_date`

結果表格

| 日期 | previous\_qtr\_to\_date |
| --- | --- |
| 1/7/2022 | 0 |
| 1/19/2022 | 0 |
| 2/5/2022 | 0 |
| 2/28/2022 | 0 |
| 3/16/2022 | -1 |
| 4/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/16/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 |

若使用 `3` 作為 `inmonthstodate()` 函數中的 `first_month_of_year`引數，該函數會讓該年從 3 月 1 日開始，然後根據提供的第一個引數將該年分為幾個季。因此，季區段為：

* 3 月-5 月

* 6 月-8 月

* 9 月-11 月

* 12 月-2 月

然後值為 5 月 15 日的 `base_date` 會分割出 3 月 - 5 月的季，並將結束邊界設定為 5 月 15 日。

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

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

將 3 月設定為該年第一個月的 `inmonthstodate`inmonthstodate 函數圖表。 ![圖表顯示該年第一個月設定為 3 月的交易範圍。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-example3-diagram.png)

因此，發生在 3 月 1 日和 5 月 15 日之間的任何交易將會傳回布林值結果 TRUE，而日期落在這些邊界之外的交易將會傳回 FALSE 的值。

## 範例 4 – 圖表範例

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

#### 載入指令碼和圖表運算式

概述

使用與第一個範例相同的資料集和情境。

在此範例中，資料集保持不變並且會載入到應用程式中。任務是要建立決定交易是否發生在與 5 月 15 日同一季的計算，作為應用程式圖表中的量值。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
						 
Transactions: 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'1/19/2022',37.23 
8189,'1/7/2022',17.17 
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`

若要計算交易是否發生在與 5 月 15 日同一季，建立下列量值：

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

結果表格

| 日期 | =inmonths(3,date,'05/15/2022', 0) |
| --- | --- |
| 1/7/2022 | 0 |
| 1/19/2022 | 0 |
| 2/5/2022 | 0 |
| 2/28/2022 | 0 |
| 3/16/2022 | 0 |
| 4/1/2022 | -1 |
| 5/7/2022 | -1 |
| 5/16/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 |

會使用 `inmonthstodate()` 函數在圖表中建立 `‘in_months_to_date` 量值。

提供的第一個引數為 `3`，將該年分為幾個季區段。第二個引數識別正在評估哪個欄位。第三個引數是 5 月 15 日的硬式編碼日期，即定義該區段結束邊界的 `base_date`。`0` 的 `period_no` 是最終引數。

具有季區段的 `inmonthstodate` 函數圖表。

具有季區段的 `inmonthstodate` 函數圖表。

具有季區段的 `inmonthstodate`inmonthstodate 函數圖表。 ![圖表顯示傳回 TRUE 布林值的交易範圍。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inmonthstodate-example4-diagram.png)

發生在 4 月 1 日和 5 月 15 日之間的任何交易將會傳回布林值結果 TRUE。該區段之外的交易日期將會傳回 FALSE。

## 範例 5 – 情境

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

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

概述

在此範例中，資料集會載入到稱為「Sales」的表格中。該表格含有下列欄位：

* 產品 ID

* 產品類型

* 銷售日期

* 銷售價格

最終使用者希望圖表依產品類型顯示截至 2022 年 12 月 24 日期間的產品銷售額。使用者希望能夠定義此期間的長度。

載入指令碼

```
SET vPeriod = 1; 

Products: 
Load 
* 
Inline  
[ 
product_id,product_type,sales_date,sales_price 
8188,product A,'9/19/2022',37.23 
8189,product D,'10/27/2022',17.17 
8190,product C,'10/30/2022',88.27 
8191,product B,'10/31/2022',57.42 
8192,product D,'11/16/2022',53.80 
8193,product D,'11/28/2022',82.06 
8194,product A,'12/2/2022',40.39 
8195,product B,'12/5/2022',87.21 
8196,product C,'12/15/2022',95.93 
8197,product B,'12/16/2022',45.89 
8198,product C,'12/19/2022',36.23 
8199,product D,'12/22/2022',25.66 
8200,product D,'12/23/2022',82.77 
8201,product A,'12/24/2022',69.98 
8202,product A,'12/24/2022',76.11 
8203,product B,'12/26/2022',25.12 
8204,product B,'12/27/2022',46.23 
8205,product B,'12/27/2022',84.21 
8206,product C,'12/28/2022',96.24 
8207,product D,'12/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(inmonthstodate($(vPeriod),sales\_date,makedate(2022,12,24),0),sales\_price,0))

9. 將量值的數字格式設定為金錢。

結果表格

| product\_type | =sum(if(inmonthstodate($(vPeriod),sales\_date,makedate(2022,12,24),0),sales\_price,0)) |
| --- | --- |
| 產品 A | $186.48 |
| 產品 B | $190.52 |
| 產品 C | $220.43 |
| 產品 D | $261.46 |

inmonthstodate() 函數使用使用者輸入作為其引數，以定義該年起始區段的大小。

該函數傳遞每個產品的銷售日期作為 inmonthstodate() 函數的第二個引數。透過使用 12 月 24 日作為 inmonthstodate() 函數中的第三個引數，銷售日期發生在截至 (含) 12 月 24 日之定義期間的產品會傳回布林值 TRUE。sum 函數會加上這些產品的銷售額。