# if 指令碼與圖表函數

if 函數會根據使用函數提供的條件是評估為 True 還是 False，來傳回值。

[If Function](https://play.vidyard.com/qnkaRddq32LnAGUTTDGdLA)

語法：

if(condition , then [, else])

傳回的資料類型： 雙值

引數

| 引數 | 描述 |
| --- | --- |
| condition | 邏輯上解譯的運算式。 |
| then | 運算式可以是任何類型的。如果 condition 是 True，則 if 函數會傳回 then 運算式的值。 |
| else | 運算式可以是任何類型的。如果 condition 是 False，則 if 函數會傳回 else 運算式的值。<br><br>此參數為選用。若 condition 為 False，則會在您未指定 else 的情況下傳回 NULL。 |

範例：圖表運算式

| 範例 | 結果 |
| --- | --- |
| `if( Amount>=<br>						0, 'OK', 'Alarm' )` | 此運算式會測試數量是否為正數 (0 或更大)，如果是，則會傳回 `OK`。如果數量小於 0，則會傳回 `Alarm`。 |

本主題中的某些範例使用內嵌載入。如需詳細資訊，請參閱[使用內嵌載入以載入資料](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/inline-loads-qs.htm)。

## 範例 - 使用 if 的載入指令碼

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

#### 載入指令碼

概述

If 可在載入指令碼中與其他方法和物件中使用，包括變數。此範例使用 `threshold` 變數並在資料模型中包含基於閾值的新欄位。

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

載入指令碼包含：

* 資料集載入到的兩個資料表格稱為：

  * `Transactions`：包括交易欄位

  * `Transaction_Buckets`：包括 `transaction_id` 欄位並建立新欄位 `[Compared to Threshold]`，使用 `if` 函數將 `transaction_amount` 欄位與 `threshold` 變數設定進行比較。

* `threshold` 變數陳述式設定為 `100`。

載入指令碼

```
Transactions:
Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, m, orange
3752, 20180916, 5.75, 1, 5646471, S, blue
3753, 20180922, 125.00, 7, 3036491, l, Black
3754, 20180922, 484.21, 13, 049681, xs, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
];
 
set threshold = 100;
					
/* Create new table called Transaction_Buckets 
Compare transaction_amount field from Transaction table to threshold of 100.
Output results into a new field called Compared to Threshold
 */

Transaction_Buckets:
Load
   transaction_id,
   If(transaction_amount > $(threshold),'Greater than $(threshold)','Less than $(threshold)') as [Compared to Threshold]
Resident Transactions;
```

結果

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

* `transaction_id`

* `[Compared to Threshold]`

使用載入指令碼中的 ifif 函數來顯示輸出的結果表格。

| transaction\_id | 與臨界值比較 |
| --- | --- |
| 3750 | 少於 100 |
| 3751 | 大於 100 |
| 3752 | 少於 100 |
| 3753 | 大於 100 |
| 3754 | 大於 100 |
| 3756 | 少於 100 |
| 3757 | 大於 100 |

## 範例 - 使用 if 評估產品資料

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

#### 圖表運算式

概述

資料集包含產品資料。您想根據產品是否可取用來識別和分類產品。

載入指令碼

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

載入指令碼包含：

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

* 資料表格中的欄位如下：

  * `ProductID`

  * `ProductName`

  * `ProductCategory`

```
Example:
LOAD * INLINE [
ProductID, ProductName, ProductCategory
1, "Laptop", "Electronics"
2, "Banana", "Fruits"
3, "Shampoo", "Toiletries"
4, "TV", "Electronics"
5, "Apple", "Fruits"
];
```

結果

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

* `ProductID`

* `ProductName`

* `ProductCategory`

建立下列量值：

* `=If(ProductCategory =  'Toiletries' or ProductCategory =  'Fruits', 'Consumable', 'Other')`，用來傳回所需的值。

結果表格

| ProductID | ProductName | ProductCategory | If(ProductCategory = 'Toiletries' or ProductCategory = 'Fruits', 'Consumable', 'Other') |
| --- | --- | --- | --- |
| 1 | Laptop | Electronics | Other |
| 2 | Banana | Fruits | Consumable |
| 3 | Shampoo | Toiletries | Consumable |
| 4 | TV | Electronics | Other |
| 5 | Apple | Fruits | Consumable |

從結果中可以看到，`if` 函數評估運算式並傳回所需的值。例如，類別 `Fruits` 或 `Toiletries` 中的產品會分類為 `Consumable`。

## 範例 - 使用 if 評估和分類用戶端資料

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

#### 圖表運算式

概述

資料集包含用戶端資料。您想要識別並分類符合您分類要求的用戶端區域和產業類別。

載入指令碼

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

載入指令碼包含：

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

* 資料表格中的欄位如下：

  * `ClientID`

  * `ClientName`

  * `Region`

  * `Industry`

```
Example:
LOAD * INLINE [
ClientID, ClientName, Region, Industry
1, "Acme Corp", "North", "Manufacturing"
2, "Beta Ltd", "East", "Technology"
3, "Gamma Inc", "West", "Retail"
4, "Delta LLC", "North", "Technology"
5, "Epsilon GmbH", "South", "Healthcare"
];
```

結果

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

* `ClientID`

* `ClientName`

* `Region`

* `Industry`

建立下列量值：

* `=If((Region = 'North' or Region = 'East') AND (Industry= 'Technology' or Industry = 'Manufacturing'), 'Target Segment', 'Secondary Segment')`，用來傳回所需的值。

結果表格

| ClientID | ClientName | Region | Industry | If((Region = 'North' or Region = 'East') AND (Industry= 'Technology' or Industry = 'Manufacturing'), 'Target Segment', 'Secondary Segment') |
| --- | --- | --- | --- | --- |
| 1 | Acme Corp | North | Manufacturing | Target Segment |
| 2 | Beta Ltd | East | Technology | Target Segment |
| 3 | Gamma Inc | West | Retail | Secondary Segment |
| 4 | Delta LLC | North | Technology | Target Segment |
| 5 | Epsilon GmbH | Sound | Healthcare | Secondary Segment |

從結果中可以看到，`if` 函數評估運算式並傳回所需的值。例如，`Acme Corp` 分類為 `Target Segment`，因為這滿足屬於 `North` 或 `East` 區域和 `Manufacturing` 或 `Technology` 產業的要求。

## 範例 - 使用 if 評估事件資料

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

#### 圖表運算式

概述

使用 if 函數對事件報告資料集進行分類。

載入指令碼

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

載入指令碼包含：

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

* 資料表格中的欄位如下：

  * Date

  * Location

  * Incidents

```
Example:
LOAD * inline [
Date, Location, Incidents
1/3/2016, Beijing, 0
1/3/2016, Boston, 12
1/3/2016, Stockholm, 3
1/3/2016, Toronto, 0
1/4/2016, Beijing, 0
1/4/2016, Boston, 8];
```

結果

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

* 日期

* 位置

* 事件

建立下列計算維度：

* =if(Incidents&gt;=10, 'Critical', 'Ok' )

* =if(Incidents&gt;=10, 'Critical', If( Incidents&gt;=1 and Incidents&lt;10, 'Warning', 'Ok'))

結果表格

| Date | Location | Incidents | if(Incidents&gt;=10, 'Critical', 'Ok' ) | if(Incidents&gt;=10, 'Critical', If( Incidents&gt;=1 and Incidents&lt;10, 'Warning', 'Ok')) |
| --- | --- | --- | --- | --- |
| 1/3/2016 | Beijing | 0 | Ok | Ok |
| 1/3/2016 | Boston | 12 | Critical | Critical |
| 1/3/2016 | Stockholm | 3 | Ok | Warning |
| 1/3/2016 | Toronto | 0 | Ok | Ok |
| 1/4/2016 | Beijing | 0 | Ok | Ok |
| 1/4/2016 | Boston | 8 | Ok | Warning |

## 範例 - 使用 if 對日期進行分類

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

#### 圖表運算式

概述

使用 if 函數對日期進行分類。

載入指令碼

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

```
SET FirstWeekDay=0;
Load 
Date(MakeDate(2022)+RecNo()-1) as Date
Autogenerate 14;
```

結果

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

* Date

建立下列計算維度：

* WeekDay(Date)

* =If(WeekDay(Date)&gt;=5,'WeekEnd','Normal Day')

結果表格

| Date | WeekDay(Date) | If(WeekDay(Date)&gt;=5,'WeekEnd','Normal Day') |
| --- | --- | --- |
| 1/1/2022 | Sat | WeekEnd |
| 1/2/2022 | Sun | WeekEnd |
| 1/3/2022 | Mon | Normal Day |
| 1/4/2022 | Tue | Normal Day |
| 1/5/2022 | Wed | Normal Day |
| 1/6/2022 | Thu | Normal Day |
| 1/7/2022 | Fri | Normal Day |
| 1/8/2022 | Sat | WeekEnd |
| 1/9/2022 | Sun | WeekEnd |
| 1/10/2022 | Mon | Normal Day |
| 1/11/2022 | Tue | Normal Day |
| 1/12/2022 | Wed | Normal Day |
| 1/13/2022 | Thu | Normal Day |
| 1/14/2022 | Fri | Normal Day |