# inquartertodate - 指令碼與圖表函數

如果 timestamp 位於包含 base\_date 的季度部分內，且不超過 (可包含) base\_date 的最後一毫秒，則此函數會傳回 True。

語法：

InQuarterToDate (timestamp, base\_date, period\_no [, first\_month\_of\_year])

傳回的資料類型： 布林

資訊備註

在 Qlik Sense 中，布林值 true 值以 -1 代表，而 false 值以 0 代表。

inquartertodate 函數的圖表

inquartertodate 函數的圖表

inquartertodate 函數的圖表 ![inquartertodate 函數將會傳回 TRUE 值之日期範圍內的範例圖表。](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Resources/Images/scripting-functions-inquartertodate-general.png)

`inquartertodate()` 函數會在 1 月 1 日和 12 月 31 日 (或使用者定義的一年開始日期及其對應的結束日期) 之間將該年分為四個相等的季。使用 `base_date`，該函數則會分割特定的季，並透過 `base_date` 識別哪一季和該季區段的最大允許日期。最後，該函數會在比較規定的日期值與該區段時，傳回布林值結果。

引數

| 引數 | 描述 |
| --- | --- |
| timestamp | 您要與 base\_date 比較的日期。 |
| base\_date | 用來評估季度的日期。 |
| period\_no | 季度可以使用 period\_no 位移。period\_no 是一個整數，其中值 0 表示包含 base\_date 的季度。負值的 period\_no 表示之前的季度，正值表示之後的季度。 |
| first\_month\_of\_year | 如果要使用不起始於 1 月的 (會計) 年度，可在 first\_month\_of\_year 中指定介於 2 和 12 之間的值。 |

## 什麼情況下使用

`inquartertodate()` 函數會傳回布林值結果。通常，此函數類型將作為 `if` 運算式中的條件使用。`inquartertodate()` 函數會用來根據評估的日期是否發生於該季截至 (含) 問題中的日期，傳回彙總或計算。

例如，`inquartertodate()` 函數可用來識別某季截至特定日期製造的所有設備。

函數範例

| 範例 | 結果 |
| --- | --- |
| `inquartertodate('01/25/2013', '03/25/2013', 0)` | 傳回 `TRUE`，因為 `timestamp` 的值 01/25/2013 落在從 01/01/2013 到 03/25/2013 的三個月期間內，其中包含 `base_date` 的值 03/25/2013。 |
| `inquartertodate('04/26/2013', '03/25/2013', 0)` | 傳回 `FALSE`，因為 04/26/2013 處在與前一個範例相同的期間之外。 |
| `inquartertodate('02/25/2013', '06/09/2013', -1)` | 傳回 `TRUE`，因為 `period_no` 的值 -1 將搜尋期間往回移動一段三個月期間 (該年的一季)。這將搜尋期間從 01/01/2013 成為 03/09/2013。 |
| `inquartertodate('03/25/2006', '04/15/2006', 0, 2)` | 傳回 `TRUE`，因為 `first_month_of_year` 的值設為 2，這將搜尋期間從 02/01/2006 成為 04/15/2006，而非從 04/01/2006 成為 04/15/2006。 |

## 區域設定

除非另有說明，否則此主題中的範例皆使用下列日期格式：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/inquartertodate.htm)

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

概述

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

載入指令碼包含：

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

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

* 建立欄位 `  in_quarter_to_date `，這決定哪些交易發生在 2022 年該季的 5 月 15 日之前。

載入指令碼

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

結果表格

| 日期 | in\_quarter\_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_quarter_to_date`」欄位使用 `inquartertodate()` 函數在前置 LOAD 陳述式中建立。提供的第一個引數識別正在評估哪個欄位。第二個引數是 5 月 15 日的硬式編碼日期，即識別要分割哪一季並定義該區段結束邊界的 `base_date`。值為 0 的 `period_no` 是最終引數，表示該函數沒有比較分割的季之前或之後的季。

inquartertodate 函數的圖表，無其他引數

inquartertodate 函數的圖表，無其他引數

inquartertodate 函數的圖表，無其他引數![圖表顯示 inquartertodate 函數將會傳回 TRUE 值的日期範圍。](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Resources/Images/scripting-functions-inquartertodate-example-no-additional-arguments.png)

發生在 4 月 1 日和 5 月 15 日之間的任何交易都會傳回布林值結果 `TRUE`。5 月 16 日及之後的交易日期將會傳回 `FALSE`，如同 4 月 1 日之前的任何交易。

## 範例 2 – period\_no

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

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

概述

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

載入指令碼包含：

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

* 建立欄位 `previous_qtr_to_date`，這決定哪些交易發生在結束於 2022 年 5 月 15 日之季區段的完整一季之前。

載入指令碼

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

Transactions:
    Load
        *,
        inquartertodate(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 |

`period_no` 值 -1 表示 `inquartertodate ()` 函數比較輸入的季區段與上一季。5 月 15 日落在該年的第二季，因此該區段原本等於 4 月 1 日和 5 月 15 日之間。然後 `period_no` 將此區段往前移動三個月，造成日期邊界變成 1 月 1 日到 2 月 15 日。

inquartertodate 函數的圖表，period\_no 範例

inquartertodate 函數的圖表，period\_no 範例

inquartertodate 函數的圖表，period\_no 範例![圖表顯示 inquartertodate 函數將會傳回 TRUE 值的日期範圍。](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Resources/Images/scripting-functions-inquartertodate-example-period-no.png)

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

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

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

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

概述

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

載入指令碼包含：

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

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

在此範例中，我們將 3 月設為會計年度的第一個月。

載入指令碼

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

Transactions:
    Load
        *,
        inquartertodate(date,'05/15/2022', 0,3) as in_quarter_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_quarter_to_date`

結果表格

| 日期 | in\_quarter\_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 作為 `inquartertodate()` 函數中的 `first_month_of_year` 引數，該函數會讓該年從 3 月 1 日開始，然後將該年分為幾個季。因此，季區段為：

* 3 月至 5 月

* 6 月至 8 月

* 9 月至 11 月

* 12 月至 2 月

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

inquartertodate 函數的圖表，first\_month\_of\_year 範例

inquartertodate 函數的圖表，first\_month\_of\_year 範例

inquartertodate 函數的圖表，first\_month\_of\_year 範例![圖表顯示 inquartertodate 函數將會傳回 TRUE 值的日期範圍。](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Resources/Images/scripting-functions-inquartertodate-example-first-mth-yr.png)

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

## 範例 4 – 圖表物件範例

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/inquartertodate.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。

建立下列量值：

=inquartertodate(date,'05/15/2022', 0)

結果表格

| 日期 | =inquartertodate(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 |

會使用 inquartertodate() 函數在圖表物件中建立 in\_quarter\_to\_date 量值。第一個引數是正在評估的日期欄位。第二個引數是 5 月 15 日的硬式編碼日期，即識別要分割哪一季並定義該區段結束邊界的 base\_date。值為 0 的 period\_no 是最終引數，表示該函數沒有比較分割的季之前或之後的季。

inquartertodate 函數的圖表，圖表物件範例

inquartertodate 函數的圖表，圖表物件範例

inquartertodate 函數的圖表，圖表物件範例![圖表顯示 inquartertodate 函數將會傳回 TRUE 值的日期範圍。](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Resources/Images/scripting-functions-inquartertodate-example-chart-object.png)

發生在 4 月 1 日和 5 月 15 日之間的任何交易都會傳回布林值結果 TRUE。5 月 16 日及之後的交易將會傳回 FALSE，如同 4 月 1 日之前的任何交易。

## 範例 5 – 情境

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

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

概述

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

載入指令碼包含：

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

* 關於產品 ID、製造日期和成本價格的資訊。

在 2022 年 5 月 15 日，識別並解決了製造流程中的一項設備錯誤。在該季截至此日期製造的產品將會有瑕疵。最終使用者希望圖表物件依季名稱顯示產品的狀態為「瑕疵」或「故障」，以及在該季至今製造的產品成本。

載入指令碼

```
Products:
Load
*
Inline 
[
product_id,manufacture_date,cost_price
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
];
```

結果

請執行下列動作：

1. 載入資料並開啟工作表。建立新的表格。建立維度以顯示季名稱：

   =quartername(manufacture\_date)

2. 接下來，建立維度以識別哪些產品為瑕疵，那些為故障：

   =if(inquartertodate(manufacture\_date,makedate(2022,05,15),0),'Defective','Faultless')

3. 建立量值以加總產品的 cost\_price：

   =sum(cost\_price)

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

結果表格

| quartername(manufacture\_date) | if(inquartertodate(manufacture\_date,makedate(2022,05,15),0),'Defective','Faultless') | Sum(cost\_price) |
| --- | --- | --- |
| 2022 年 1 月至 3 月 | 無缺失 | $253.89 |
| 2022 年 4 月至 6 月 | 無缺失 | $229.03 |
| 2022 年 4 月至 6 月 | 瑕疵 | $122.45 |
| 2022 年 7 月至 9 月 | 無缺失 | $446.31 |
| 2022 年 10 月至 12 月 | 無缺失 | $163.91 |

inquartertodate() 函數會在評估每個產品的製造日期時傳回布林值。對於傳回布林值 TRUE 的項目，會將產品標記為 ‘Defective’。對於傳回值 FALSE 的任何產品，以及因此不是在該季截至 (含) 5 月 15 日製作的產品，會將產品標記為 ‘Faultless’。