# NullCount - 图表函数

NullCount() 用于聚合每个图表维度中 null 值的数量。

语法：

NullCount({[SetExpression][DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]]} expr)

返回数据类型： 整数

参数

| 参数 | 描述 |
| --- | --- |
| expr | 表达式或字段包含要度量的数据。 |
| SetExpression | 聚合函数会默认聚合选择项定义的可能记录集合。可选记录集合可由集合分析表达式定义。 |
| DISTINCT | 如果在函数参数前出现单词 DISTINCT，则将忽略计算该函数参数生成的副本。 |
| TOTAL | 如果在函数参数前面出现单词 TOTAL，则计算给出当前选择项的所有可能值，而不只是属于当前维度值的那些值，即它会忽略图表维度。<br><br>通过使用 TOTAL [&lt;fld {.fld}&gt;]（其中 TOTAL 限定符后跟一个或多个字段名称作为图表维度变量的子集的列表），您可以创建总可能值的子集。<br><br>[定义聚合范围](https://help.qlik.com/zh-CN/sense/November2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/define-aggregation-scope.htm) |

示例：图表表达式

| 示例 | 结果 |
| --- | --- |
| `NullCount(OrderNumber)` | 返回 `OrderNumber` 字段中空值的计数。 |
| ` NullCount (OrderNumber)/NullCount ({1} Total OrderNumber)  ` | 表达式返回来自所选客户的订单数量（以 `NULL` 作为 `OrderNumber`），作为来自客户的所有订单的一部分（以 `NULL` 作为 `OrderNumber`）。 |

## 示例 - NullCount 基本原理

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/CounterAggregationFunctions/nullcount.htm)

#### 图表表达式

概览

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 加载到名为 `Example` 的数据表中的数据集。

* 设置为 `NULL` 的处理变量的 `NullInterpret` 值。该变量将所有文本 `NULL` 为空值的记录解释为空值。

* 数据表中有以下字段：

  * `Customer`

  * `Product`

  * `OrderNumber`

加载脚本

```
Set NULLINTERPRET = NULL;
Example:
LOAD * inline [
Customer|Product|OrderNumber
Astrida|AA|1
Astrida|AA|7
Astrida|BB|4
Astrida|CC|6
Betacab|AA|5
Betacab|BB|2
Betacab|DD|NULL
Canutility|AA|3
Canutility|AA|NULL
Canutility|CC|NULL
] (delimiter is '|');
```

提示注释要将 `NullInterpret` 变量设置回默认值，请在加载脚本末尾包含以下变量语句：`Set NULLINTERPRET =;`

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

* `Customer`

创建以下度量：

* `=NullCount(OrderNumber)`，计算具有 `NULL` 作为 `OrderNumber` 的订单数。

* `=NullCount (OrderNumber)/NullCount ({1} Total OrderNumber)`，返回（来自所选客户的）订单数量，其中以 `NULL` 作为 `OrderNumber`，成为来自以 `NULL` 作为 `OrderNumber` 的客户的所有订单的一部分。 要将此值显示为百分比，可在数字格式设定下选择数字 &gt; 简单格式 &gt; 12.34%。

Results table

| Customer | NullCount(OrderNumber) | NullCount (OrderNumber)/NullCount ({1} Total OrderNumber) |
| --- | --- | --- |
| Totals | 3 | 100.00% |
| Astrida | 0 | 0.00% |
| Betacab | 1 | 33.33% |
| Canutility | 2 | 66.67% |

第一个度量值返回 `3` 因为存在具有 `NULL` 值的 `3` 总 `OrderNumber` 记录（包含客户 `Betacab` 的 `1` 记录和包含 `Canutility` 的 `2` 记录）。

第二个度量返回 `NULL` 订单数量，作为所有客户 `NULL` 订单的一部分。例如，具有 `NULL` 值的所有订单号中的 `66.67%` 都归属于客户 `Canutility` 。

## 示例 - 使用 NullCount 识别 KPI 数据中的空值

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/CounterAggregationFunctions/nullcount.htm)

#### 图表表达式

概览

数据集包含每月关键产品指标 (KPI) 数据。产品经理希望确定数据中哪些 KPI 具有空值。

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 加载到名为 `Example` 的数据表中的数据集。

* 设置为 `NULL` 的处理变量的 `NullInterpret` 值。该变量将所有文本 `NULL` 为空值的记录解释为空值。

* 数据表中有以下字段：

  * `Month`

  * `KPI`

  * `Value`

加载脚本

```
Set NULLINTERPRET = NULL;
Example:
LOAD * inline [
Month, KPI, Value
2024-01-01, Revenue, 5000
2024-02-01, Revenue, NULL
2024-03-01, Revenue, 5500
2024-04-01, Revenue, NULL
2024-01-01, Expenses, 3000
2024-02-01, Expenses, 3100
2024-03-01, Expenses, NULL
2024-04-01, Expenses, 3200
2024-01-01,Customer Count, 200
2024-02-01,Customer Count, 210
2024-03-01,Customer Count, NULL
2024-04-01,Customer Count, NULL
];
```

提示注释要将 `NullInterpret` 变量设置回默认值，请在加载脚本末尾包含以下变量语句：`Set NULLINTERPRET =;`

结果

加载数据并打开工作表。创建一个新的透视表，并将此字段添加为行维度：

* `KPI`

创建以下度量：

* `=NullCount(Value)`，计算缺失值的数量。

Results table

| KPI | NullCount(Value) |
| --- | --- |
| Customer Count | 2 |
| Expenses | 1 |
| Revenue | 2 |

结果显示，数据集中的 `5` 记录具有 `NULL` 值以及与这些记录相关的 KPI 类别。