# inyear 指令碼與圖表函數

此函數會傳回 True，前提是如果 timestamp 位於包含 base\_date 的年中。

語法：

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

傳回的資料類型： 布林值

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

`inyear()` 函數範圍的圖表

`inyear()` 函數範圍的圖表

`inyear()`inyear() 函數範圍的圖表 ![圖表顯示 inyear() 函數評估的時間範圍，並將會根據基底日期的位置傳回布林值結果。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inyear-diagram1.png)

`inyear()` 函數在比較所選日期值與 `base_date` 定義的年份時傳回布林值結果。

何時使用 inyear

`inyear()` 函數會傳回布林值結果。通常，此函數類型將作為 `if expression` 中的條件使用。這根據評估的日期是否發生於問題中的年份，傳回彙總或計算。例如，`inyear()` 函數可用來識別發生在定義年份的所有銷售。

引數

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

您可以在 `first_month_of_year` 引數中使用下列值設定一年的第一個月：

first\_month\_of\_year 值

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

## 區域設定

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

函數範例

| 範例 | 結果 |
| --- | --- |
| `inyear ('01/25/2013', '01/01/2013', 0 )` | 傳回 TRUE |
| `inyear ('01/25/2012', '01/01/2013', 0)` | 傳回 FALSE |
| `inyear ('01/25/2013', '01/01/2013', -1)` | 傳回 FALSE |
| `inyear ('01/25/2012', '01/01/2013', -1 )` | 傳回 TRUE |
| `inyear ('01/25/2013', '01/01/2013', 0, 3)` | 傳回 TRUE<br><br>`base_date` 和 `first_month_of_year` 的值指定時間戳記必須落在 01/03/2012 和 02/28/2013 內 |
| `inyear ('03/25/2013', '07/01/2013', 0, 3 )` | 傳回 TRUE |

## 範例 1 - 基本範例

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

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

概述

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

載入指令碼包含：

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

* 包含設為「`in_year`」欄位之 `inyear()` 函數的前置載入，這判定哪些交易發生在與 2021 年 7 月 26 日相同的年份。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
Transactions: 
    Load 
        *, 
        inyear(date,'07/26/2021', 0) as in_year 
        ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'01/13/2020',37.23 
8189,'02/26/2020',17.17
8190,'03/27/2020',88.27 
8191,'04/16/2020',57.42 
8192,'05/21/2020',53.80 
8193,'08/14/2020',82.06 
8194,'10/07/2020',40.39 
8195,'12/05/2020',87.21 
8196,'01/22/2021',95.93 
8197,'02/03/2021',45.89 
8198,'03/17/2021',36.23 
8199,'04/23/2021',25.66 
8200,'05/04/2021',82.77 
8201,'06/30/2021',69.98 
8202,'07/26/2021',76.11 
8203,'12/27/2021',25.12 
8204,'06/06/2022',46.23 
8205,'07/18/2022',84.21 
8206,'11/14/2022',96.24 
8207,'12/12/2022',67.67 
]; 
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `date`
* `in_year`

結果表格

| 日期 | in\_year |
| --- | --- |
| 01/13/2020 | 0 |
| 02/26/2020 | 0 |
| 03/27/2020 | 0 |
| 04/16/2020 | 0 |
| 05/21/2020 | 0 |
| 08/14/2020 | 0 |
| 10/07/2020 | 0 |
| 12/05/2020 | 0 |
| 01/22/2021 | -1 |
| 02/03/2021 | -1 |
| 03/17/2021 | -1 |
| 04/23/2021 | -1 |
| 05/04/2021 | -1 |
| 06/30/2021 | -1 |
| 07/26/2021 | -1 |
| 12/27/2021 | -1 |
| 06/06/2022 | 0 |
| 07/18/2022 | 0 |
| 11/14/2022 | 0 |
| 12/12/2022 | 0 |

「`in_year`」欄位使用 `inyear()` 函數在前置 LOAD 陳述式中建立。第一個引數識別正在評估哪個欄位。第二個引數是 2021 年 7 月 26 日的硬式編碼日期，即判定比較子年份的 `base_date`。`0` 的 `period_no` 是最終引數，表示 `inyear()` 函數沒有比較該年之前或之後的年。

以 7 月 26 日作為基底日期的 `inyear()` 函數範圍圖表

以 7 月 26 日作為基底日期的 `inyear()` 函數範圍圖表

以 7 月 26 日作為基底日期的 `inyear()`inyear() 函數範圍圖表 ![以 7 月 26 日作為基底日期的 inyear() 函數圖表以及從 2021 年 1 月 1 日到 2022 年 1 月 1 日的時間範圍會醒目提示。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inyear-examples1_and4-diagram.png)

任何發生在 2021 年的交易會傳回 TRUE 的布林值結果。

## 範例 2 - period\_no

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

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

概述

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

載入指令碼包含：

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

* 包含設為「`previous_year`」欄位之 `inyear()` 函數的前置載入，這判定哪些交易發生在包含 2021 年 7 月 26 日之年份的前一年。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
Transactions: 
    Load 
        *, 
        inyear(date,'07/26/2021', -1) as previous_year 
        ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'01/13/2020',37.23 
8189,'02/26/2020',17.17
8190,'03/27/2020',88.27 
8191,'04/16/2020',57.42 
8192,'05/21/2020',53.80 
8193,'08/14/2020',82.06 
8194,'10/07/2020',40.39 
8195,'12/05/2020',87.21 
8196,'01/22/2021',95.93 
8197,'02/03/2021',45.89 
8198,'03/17/2021',36.23 
8199,'04/23/2021',25.66 
8200,'05/04/2021',82.77 
8201,'06/30/2021',69.98 
8202,'07/26/2021',76.11 
8203,'12/27/2021',25.12 
8204,'06/06/2022',46.23 
8205,'07/18/2022',84.21 
8206,'11/14/2022',96.24 
8207,'12/12/2022',67.67 
]; 
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `date`
* `previous_year`

結果表格

| 日期 | previous\_year |
| --- | --- |
| 01/13/2020 | -1 |
| 02/26/2020 | -1 |
| 03/27/2020 | -1 |
| 04/16/2020 | -1 |
| 05/21/2020 | -1 |
| 08/14/2020 | -1 |
| 10/07/2020 | -1 |
| 12/05/2020 | -1 |
| 01/22/2021 | 0 |
| 02/03/2021 | 0 |
| 03/17/2021 | 0 |
| 04/23/2021 | 0 |
| 05/04/2021 | 0 |
| 06/30/2021 | 0 |
| 07/26/2021 | 0 |
| 12/27/2021 | 0 |
| 06/06/2022 | 0 |
| 07/18/2022 | 0 |
| 11/14/2022 | 0 |
| 12/12/2022 | 0 |

使用 `-1` 作為 `inyear()` 函數中的 `period_no` 引數，將比較子年份邊界往回偏移一整年。2021 年原本識別為比較子年份。`period_no` 將比較子年份偏移一年，讓 2020 成為比較子年份。

period\_no 引數設定為 `-1` 的 `inyear()` 函數範圍圖表

period\_no 引數設定為 `-1` 的 `inyear()` 函數範圍圖表

period\_no 引數設定為 `-1`-1 的 `inyear()`inyear() 函數範圍圖表 ![以 7 月 26 日作為基底日期的 inyear() 函數圖表，且 period_no 引數設為負值。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inyear-example2-diagram.png)

因此，任何發生在 2020 年的交易會傳回 TRUE 的布林值結果。

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

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

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

概述

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

載入指令碼包含：

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

* 包含設為「`in_year`」欄位之 `inyear()` 函數的前置載入，這判定哪些交易發生在與 2021 年 7 月 26 日相同的年份。

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

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
Transactions: 
    Load 
        *, 
        inyear(date,'07/26/2021', 0, 3) as in_year 
        ; 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'01/13/2020',37.23 
8189,'02/26/2020',17.17
8190,'03/27/2020',88.27 
8191,'04/16/2020',57.42 
8192,'05/21/2020',53.80 
8193,'08/14/2020',82.06 
8194,'10/07/2020',40.39 
8195,'12/05/2020',87.21 
8196,'01/22/2021',95.93 
8197,'02/03/2021',45.89 
8198,'03/17/2021',36.23 
8199,'04/23/2021',25.66 
8200,'05/04/2021',82.77 
8201,'06/30/2021',69.98 
8202,'07/26/2021',76.11 
8203,'12/27/2021',25.12 
8204,'06/06/2022',46.23 
8205,'07/18/2022',84.21 
8206,'11/14/2022',96.24 
8207,'12/12/2022',67.67 
]; 
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `date`
* `in_year`

結果表格

| 日期 | in\_year |
| --- | --- |
| 01/13/2020 | 0 |
| 02/26/2020 | 0 |
| 03/27/2020 | 0 |
| 04/16/2020 | 0 |
| 05/21/2020 | 0 |
| 08/14/2020 | 0 |
| 10/07/2020 | 0 |
| 12/05/2020 | 0 |
| 01/22/2021 | 0 |
| 02/03/2021 | 0 |
| 03/17/2021 | -1 |
| 04/23/2021 | -1 |
| 05/04/2021 | -1 |
| 06/30/2021 | -1 |
| 07/26/2021 | -1 |
| 12/27/2021 | -1 |
| 06/06/2022 | 0 |
| 07/18/2022 | 0 |
| 11/14/2022 | 0 |
| 12/12/2022 | 0 |

使用 `3` 作為 `inyear()` 函數中的 `first_month_of_year` 引數會讓該年於 3 月 1 日開始，並於 2 月底結束。

將 3 月設定為該年第一個月的 `inyear()` 函數範圍圖表

將 3 月設定為該年第一個月的 `inyear()` 函數範圍圖表

將 3 月設定為該年第一個月的 `inyear()`inyear() 函數範圍圖表 ![以 7 月 26 日作為基底日期且該年的第一個月設為 3 月的 inyear() 函數圖表。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inyear-example3-diagram.png)

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

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

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

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

概述

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

不過，在此範例中，資料集保持不變並且會載入到應用程式中。會建立決定交易是否發生在與 2021 年 7 月 26 日相同的一年，作為應用程式圖表物件中的量值。

載入指令碼

```
SET DateFormat='MM/DD/YYYY'; 
Transactions: 
Load 
* 
Inline  
[ 
id,date,amount 
8188,'01/13/2020',37.23 
8189,'02/26/2020',17.17
8190,'03/27/2020',88.27 
8191,'04/16/2020',57.42 
8192,'05/21/2020',53.80 
8193,'08/14/2020',82.06 
8194,'10/07/2020',40.39 
8195,'12/05/2020',87.21 
8196,'01/22/2021',95.93 
8197,'02/03/2021',45.89 
8198,'03/17/2021',36.23 
8199,'04/23/2021',25.66 
8200,'05/04/2021',82.77 
8201,'06/30/2021',69.98 
8202,'07/26/2021',76.11 
8203,'12/27/2021',25.12 
8204,'06/06/2022',46.23 
8205,'07/18/2022',84.21 
8206,'11/14/2022',96.24 
8207,'12/12/2022',67.67 
]; 
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* `date`

若要計算交易是否發生在與 2021 年 7 月 26 日相同的一年，建立下列量值：

* `=inyear(date,'07/26/2021', 0)`

結果表格

| 日期 | =inyear(date,'07/26/2021',0) |
| --- | --- |
| 01/13/2020 | 0 |
| 02/26/2020 | 0 |
| 03/27/2020 | 0 |
| 04/16/2020 | 0 |
| 05/21/2020 | 0 |
| 08/14/2020 | 0 |
| 10/07/2020 | 0 |
| 12/05/2020 | 0 |
| 01/22/2021 | -1 |
| 02/03/2021 | -1 |
| 03/17/2021 | -1 |
| 04/23/2021 | -1 |
| 05/04/2021 | -1 |
| 06/30/2021 | -1 |
| 07/26/2021 | -1 |
| 12/27/2021 | -1 |
| 06/06/2022 | 0 |
| 07/18/2022 | 0 |
| 11/14/2022 | 0 |
| 12/12/2022 | 0 |

會使用 `inyear()` 函數在圖表中建立「`in_year`」欄位。第一個引數識別正在評估哪個欄位。第二個引數是 2021 年 7 月 26 日的硬式編碼日期，即判定比較子年份的 `base_date`。`0` 的 `period_no` 是最終引數，表示 `inyear()` 函數沒有比較該年之前或之後的年。

以 7 月 27 日作為基底日期的 `inyear()` 函數範圍圖表

以 7 月 27 日作為基底日期的 `inyear()` 函數範圍圖表

以 7 月 27 日作為基底日期的 `inyear()`inyear() 函數範圍圖表 ![以 7 月 27 日作為基底日期而且從 2021 年 1 月 1 日到 2022 年 1 月 1 日的時間範圍醒目提示的 inyear() 函數圖表。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inyear-examples1_and4-diagram.png)

任何發生在 2021 年的交易會傳回 TRUE 的布林值結果。

## 範例 5 - 情境

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

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

概述

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

載入指令碼包含：

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

* 該表格含有下列欄位：

  * 產品 ID

  * 產品類型

  * 製造日期

  * 成本價格

最終使用者希望圖表物件依產品類型顯示在 2021 年製造的產品成本。

載入指令碼

```
Products: 
Load 
* 
Inline  
[ 
product_id,product_type,manufacture_date,cost_price 
8188,product A,'01/13/2020',37.23 
8189,product B,'02/26/2020',17.17 
8190,product B,'03/27/2020',88.27 
8191,product C,'04/16/2020',57.42 
8192,product D,'05/21/2020',53.80 
8193,product D,'08/14/2020',82.06 
8194,product C,'10/07/2020',40.39 
8195,product B,'12/05/2020',87.21
8196,product A,'01/22/2021',95.93 
8197,product B,'02/03/2021',45.89 
8198,product C,'03/17/2021',36.23 
8199,product C,'04/23/2021',25.66
8200,product B,'05/04/2021',82.77 
8201,product D,'06/30/2021',69.98 
8202,product D,'07/26/2021',76.11 
8203,product D,'12/27/2021',25.12 
8204,product C,'06/06/2022',46.23 
8205,product C,'07/18/2022',84.21 
8206,product A,'11/14/2022',96.24 
8207,product B,'12/12/2022',67.67 
]; 
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* product\_type

若要計算在 2021 年製造的每個產品總和，建立下列量值：

* =sum(if(InYear(manufacture\_date,makedate(2021,01,01),0),cost\_price,0))

請執行下列動作：

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

2. 在外觀之下，關閉總計。

結果表格

| product\_type | =sum(if(InYear(manufacture\_date,makedate(2021,01,01),0),cost\_price,0)) |
| --- | --- |
| 產品 A | $95.93 |
| 產品 B | $128.66 |
| 產品 C | $61.89 |
| 產品 D | $171.21 |

inyear() 函數會在評估每個產品的製造日期時傳回布林值。對於在 2021 年製造的任何產品，inyear() 函數會傳回布林值 TRUE 並顯示 cost\_price 的總和。