# if - 指令碼與圖表函數

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

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

語法：

if(condition , then [, else])

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

引數

| 引數 | 描述 |
| --- | --- |
| 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'。 |

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

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

#### 範例：載入指令碼

載入指令碼

If 可在載入指令碼中與其他方法和物件中使用，包括變數。例如，如果設定變數 threshold，並且想要根據該臨界值在資料模型中包括欄位，您可以執行下列項目。

在資料載入編輯器中建立新的索引標籤，然後載入下列資料作為內嵌載入。在 Qlik Sense 中建立以下表格以查看結果。

```
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;
```

結果

使用在載入指令碼中的 ifif 函數來顯示輸出的 Qlik SenseQlik Sense 表格。

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

## 範例 - 使用 if 的圖表運算式

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

#### 範例：圖表運算式

圖表運算式 1

載入指令碼

在資料載入編輯器中建立新的索引標籤，然後載入下列資料作為內嵌載入。載入資料後，在 Qlik Sense 表格中建立以下的圖表運算式範例。

```
MyTable:
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];
```

在圖表運算式中顯示 ifif 函數範例的 Qlik SenseQlik Sense 表格。

| 日期 | 位置 | 事件 | if(Incidents&gt;=10, 'Critical', 'Ok' ) | if(Incidents&gt;=10, 'Critical', If( Incidents&gt;=1 and Incidents&lt;10, 'Warning', 'Ok')) |
| --- | --- | --- | --- | --- |
| 1/3/2016 | 北京 | 0 | 確定 | 確定 |
| 1/3/2016 | 波士頓 | 12 | 重大 | 重大 |
| 1/3/2016 | 斯德哥爾摩 | 3 | 確定 | 警告 |
| 1/3/2016 | 多倫多 | 0 | 確定 | 確定 |
| 1/4/2016 | 北京 | 0 | 確定 | 確定 |
| 1/4/2016 | 波士頓 | 8 | 確定 | 警告 |

圖表運算式 2

在新的應用程式中，在資料載入編輯器的新索引標籤中新增下列指令碼，然後載入資料。然後您可以使用以下圖表運算式建立表格。

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

在圖表運算式中顯示 ifif 函數範例的 Qlik SenseQlik Sense 表格。

| 日期 | WeekDay(Date) | If(WeekDay(Date)&gt;=5,'WeekEnd','Normal Day') |
| --- | --- | --- |
| 1/1/2022 | 星期六 | WeekEnd |
| 1/2/2022 | 星期日 | WeekEnd |
| 1/3/2022 | 星期一 | 平日 |
| 1/4/2022 | 星期二 | 平日 |
| 1/5/2022 | 星期三 | 平日 |
| 1/6/2022 | 星期四 | 平日 |
| 1/7/2022 | 星期五 | 平日 |
| 1/8/2022 | 星期六 | WeekEnd |
| 1/9/2022 | 星期日 | WeekEnd |
| 1/10/2022 | 星期一 | 平日 |
| 1/11/2022 | 星期二 | 平日 |
| 1/12/2022 | 星期三 | 平日 |
| 1/13/2022 | 星期四 | 平日 |
| 1/14/2022 | 星期五 | 平日 |