# FirstSortedValue - 图表函数

FirstSortedValue() 将返回来自 value 指定表达式的值，相当于 sort\_weight 参数排序的结果，例如，单价最低的产品名称。排序顺序中的第 n 个值，可在 rank 中指定。如果在指定 rank 下多个结果值共享同一 sort\_weight，则此函数返回 NULL。

语法：

FirstSortedValue([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] value, sort\_weight [,rank])

返回数据类型： 双

参数

| 参数 | 描述 |
| --- | --- |
| value | 输出字段。 此函数用于查找表达式 value 的值，相当于 sort\_weight 的排序结果。 |
| sort\_weight | 输入字段。 该表达式包含要排序的数据。找到 sort\_weight 的第一个（最低）值，由 value 表达式的对应值确定。如果在 sort\_weight 前面加一个减号，则此函数会返回最后一个（最高）排序值。 |
| rank | 通过指定一个大于 1 的 rank “n”，您会获得第 n 个排序值。 |
| SetExpression | 聚合函数会默认聚合选择项定义的可能记录集合。可选记录集合可由集合分析表达式定义。 |
| DISTINCT | 如果在函数参数前出现单词 DISTINCT，则将忽略计算该函数参数生成的副本。 |
| TOTAL | 如果在函数参数前面出现单词 TOTAL，则计算给出当前选择项的所有可能值，而不只是属于当前维度值的那些值，即它会忽略图表维度。<br><br>通过使用 TOTAL [&lt;fld {.fld}&gt;]（其中 TOTAL 限定符后跟一个或多个字段名称作为图表维度变量的子集的列表），您可以创建总可能值的子集。<br><br>[定义聚合范围](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/define-aggregation-scope.htm) |

示例：图表表达式

| 示例 | 结果 |
| --- | --- |
| `FirstSortedValue (Product, UnitPrice)` | 返回带最低 `UnitPrice` 的 `Product`。 |
| `FirstSortedValue (Product, UnitPrice, 2)` | 返回带第二低 `UnitPrice` 的 `Product`。 |
| `FirstSortedValue (Customer, -UnitPrice, 2)` | 返回具有 `Product`（带第二高的 `UnitPrice`）的 `Customer`。 |
| `FirstSortedValue (Customer, -UnitPrice*UnitSales, 2)` | 返回具有第二高销售订单值的 `Customer`，该值为 `UnitPrice` 乘以 `UnitSales`。 |

## 示例 - FirstSortedValue 基本原理

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

#### 图表表达式

概述

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

加载脚本包含：

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

* 数据表中有以下字段：

  * `Customer`

  * `Product`

  * `UnitSales`

  * `UnitPrice`

加载脚本

```
Example:
LOAD * inline [
Customer|Product|UnitSales|UnitPrice
Astrida|AA|4|16
Astrida|AA|10|15
Astrida|BB|9|9
Betacab|BB|5|10
Betacab|CC|2|20
Betacab|DD||25
Canutility|AA|8|15
Canutility|CC||19
] (delimiter is '|');
```

结果

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

* `=FirstSortedValue (Product, UnitPrice)`，计算单价最低的产品。

* `=FirstSortedValue (Product, UnitPrice, 2)`，计算单价第二低的产品。

* `=FirstSortedValue (Customer, -UnitPrice, 2)` 用于计算单价第二高的产品的客户。

* `=FirstSortedValue (Customer, UnitPrice, 3)` 用于计算产品价格第三高的客户。

* `=FirstSortedValue (Customer, -UnitPrice*UnitSales, 2)` 用于计算具有第二高销售订单价值的客户，`UnitPrice` 乘以 `UnitSales`。

Results table

| FirstSortedValue (Product, UnitPrice) | FirstSortedValue (Product, UnitPrice, 2) | FirstSortedValue (Customer, -UnitPrice, 2) | FirstSortedValue (Customer, UnitPrice, 3) | FirstSortedValue (Customer, -UnitPrice\*UnitSales, 2) |
| --- | --- | --- | --- | --- |
| BB | BB | Betacab | - | Canutility |

第一列返回值 `BB`，因为这是具有最低 `UnitPrice` (`9`) 的产品

第二列返回值 `BB`，因为这是具有第二低 `UnitPrice` (`10`) 的产品 。

第三列返回值 `Betacab` ，其为具有 `Product` 的 `Customer`，该客户具有第二高 `UnitPrice` (`20`)。

第四列返回 `NULL`，因为有两个 `Customer` 的值（`Astrida` 和 `Canutility`），具有相同的排名（第三低）`UnitPrice` (`15`)。

第五列返回 `Canutility`，其为 `Customer`，具有第二高的销售订单值，`UnitPrice` 乘以 `UnitSales` (`120`)。

## 示例 - FirstSortedValue 方案

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

#### 图表表达式

概述

数据集包含今年前三个月的销售数据。区域经理希望确定最优和最畅销的产品，以计划未来的库存。

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

加载脚本包含：

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

* 数据表中有以下字段：

  * `Date`

  * `SalesProduct`

  * `Category`

  * `Sales`

加载脚本

```
Example:
Load * inline [
Date,SalesProduct, Category, Sales
2024-01-01, Apple, Fruit, 100
2024-01-15, Banana, Fruit, 80
2024-01-30, Carrot, Vegetable, 50
2024-02-05, Apple, Fruit, 120
2024-02-20, Banana, Fruit, 90
2024-02-28, Carrot, Vegetable, 60
2024-03-10, Apple, Fruit, 110
2024-03-22, Banana, Fruit, 85
2024-03-31, Carrot, Vegetable, 55
];
```

结果

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

* `Category`

创建以下度量：

* `=FirstSortedValue(SalesProduct, -Sales)`，计算出每个类别中最畅销的产品。

* `=FirstSortedValue(TOTAL SalesProduct, -Sales)`，计算最畅销的产品。

* `=FirstSortedValue(TOTAL SalesProduct, Sales)`，计算销量最不佳的产品。

Results table

| Category | FirstSortedValue(SalesProduct, -Sales) | FirstSortedValue(TOTAL SalesProduct, -Sales) | FirstSortedValue(TOTAL SalesProduct, Sales) |
| --- | --- | --- | --- |
| Fruit | Apple | Apple | Carrot |
| Vegetable | Carrot | Apple | Carrot |

结果表明，`Apple` 和 `Carrot` 是各自类别中最畅销的产品，`Fruit` 和 `Vegetable` 。不过，在所有类别中，`Apple` 是最畅销的产品，`Carrot` 是最不畅销的产品。

## 了解详情

* [集合分析](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/SetAnalysis/set-analysis-expressions.htm)
* [定义聚合范围](https://help.qlik.com/zh-CN/sense/May2026/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/define-aggregation-scope.htm)