# FirstSortedValue - 圖表函數

FirstSortedValue() 從運算式中傳回 value 中指定的值，該值對應於排序 sort\_weight 引數的結果，例如，具有最低單價之產品的名稱。可以在 rank 中指定排序順序中的第 n 個值。如果對於指定的 rank，有多個產生的值共用同一 sort\_weight，則該函數會傳回 NULL。

語法：

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

傳回的資料類型： 雙值

引數

| 引數 | 描述 |
| --- | --- |
| value | 輸出欄位。 函數找到對應於 sort\_weight 之排序結果的運算式 value 的值。 |
| sort\_weight | 輸入欄位。 包含待排序之資料的運算式。會找到 sort\_weight 的第一個 (最低) 值，從中判定 value 運算式的對應值。如果您在 sort\_weight 前面放置一個負號，則該函數會改為傳回最後一個 (最高) 的已排序值。 |
| rank | 透過指明 rank "n" 大於 1，可取得第 n 個已排序的值。 |
| SetExpression | 依預設，彙總函數將彙總選項所定義的一組可能記錄。集合分析運算式可定義一組替代的記錄。 |
| DISTINCT | 如果 DISTINCT 這個字出現在函數引數之前，會忽略評估函數引數所產生的重複項目。 |
| TOTAL | 如果單字 TOTAL 位於函數引數之前，則會在提供現行選項的所有可能值上進行計算，而不僅僅是關於目前維度值的那些選項，也就是說，它會忽略圖表維度。<br><br>透過使用 TOTAL [&lt;fld {.fld}&gt;] (其中 TOTAL 限定詞後面是做為圖表維度變數子集之一或更多欄位名稱的清單)，您可建立總可能值的子集。<br><br>[定義彙總範圍](https://help.qlik.com/zh-TW/cloud-services/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)` | 傳回具有 `UnitPrice` 次高之 `Product` 的 `Customer`。 |
| `FirstSortedValue (Customer, -UnitPrice*UnitSales, 2)` | 傳回具有次高銷售訂單值的 `Customer`，即 `UnitPrice` 乘以 `UnitSales`。 |

## 範例 - FirstSortedValue 基礎事項

[](https://help.qlik.com/en-US/cloud-services/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`，即具有次高 `UnitPrice` (`20`) 之 `Product` 的 `Customer`。

第四欄傳回 `NULL`，因為 `Customer` 的兩個值 (`Astrida` 和 `Canutility`) 具有相同的 (第三低) 排名 `UnitPrice` (`15`)。

第五欄傳回 `Canutility`，這是具有次高銷售訂單值的 `Customer`，即 `UnitPrice` 乘以 `UnitSales` (`120`)。

## 範例 - FirstSortedValue 使用情境

[](https://help.qlik.com/en-US/cloud-services/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-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/SetAnalysis/set-analysis-expressions.htm)
* [定義彙總範圍](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/define-aggregation-scope.htm)