# 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-CN/sense/May2026/Subsystems/Hub/Content/Sense_Hub/Scripting/inline-loads-qs.htm)

## 示例 - 使用 if 加载脚本

[](https://help.qlik.com/en-US/sense/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/sense/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/sense/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/sense/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/sense/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 |