# if - 脚本和图表函数

if 函数用于返回一个值，具体取决于函数提供的条件的计算结果是否为 True 或 False。

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

语法：

if(condition , then [, else])

本主题中的一些示例使用内联加载。[使用内联加载来加载数据](https://help.qlik.com/zh-CN/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;
```

结果

Qlik SenseQlik Sense 表显示了在加载脚本中使用 ifif 函数的输出。

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

Qlik SenseQlik Sense 表以图表表达式显示了 ifif 函数的示例。

| 日期 | 位置 | 事件 | 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 |

图表表达式 2

在新应用程序中，在数据加载编辑器的新选项卡中添加以下脚本，然后加载数据。然后可以使用下面的图表表达式创建表格。

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

Qlik SenseQlik Sense 表以图表表达式显示了 ifif 函数的示例。

| 日期 | 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 |