Only - 圖表函數
如果已彙總的資料中有一個可能的結果並且只有一個,則 Only() 會傳回一個值。例如,如果多個產品有單價 9,則僅搜尋單價 = 9 的產品將傳回 NULL。
語法:
Only([{SetExpression}] [TOTAL [<fld {,fld}>]] expr)
傳回的資料類型: 雙值
| 引數 | 描述 |
|---|---|
| expr | 包含待測量之資料的運算式或欄位。 |
| SetExpression | 依預設,彙總函數將彙總選項所定義的一組可能記錄。集合分析運算式可定義一組替代的記錄。 |
| TOTAL | 如果單字 TOTAL 位於函數引數之前,則會在提供現行選項的所有可能值上進行計算,而不僅僅是關於目前維度值的那些選項,也就是說,它會忽略圖表維度。 透過使用 TOTAL [<fld {.fld}>] (其中 TOTAL 限定詞後面是做為圖表維度變數子集之一或更多欄位名稱的清單),您可建立總可能值的子集。 |
| 範例 | 結果 |
|---|---|
| Only(Category) | 只要在脈絡中是唯一內容,就會顯示 Category。 |
範例 - Only 基礎事項
概述
開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。
載入指令碼包含:
-
載入到稱為 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 '|');結果
載入資料並開啟工作表。透過下列量值建立新的表格:
-
=Only({<UnitPrice={9}>} Product),用來計算唯一一個 UnitPrice 是 9 的 Product。
-
=Only({<Product={DD}>} Customer),用來計算唯一一個銷售稱為 DD 之 Product 的 Customer。
-
=Only({<UnitPrice={20}>} UnitSales),用來計算 UnitSales 的數量,其中 UnitPrice 是 20。
-
=Only({<UnitPrice={15}>} UnitSales),用來計算 UnitSales 的值,其中 UnitPrice = 15。
| Only({<UnitPrice={9}>} Product) | Only({<Product={DD}>} Customer) | Only({<UnitPrice={20}>} UnitSales) | Only({<UnitPrice={15}>} UnitSales) |
|---|---|---|---|
| BB | Betacab | 2 | - |
在此範例中,請觀察以下事項:
-
第一欄傳回值 BB,因為這是唯一一個 UnitPrice 是 9 的 Product。
-
第二欄傳回 Betacab,因為這是唯一銷售名為 DD 之 Product 的 Customer。
-
第三欄傳回 2,因為這是唯一的 UnitSales 值,其中 UnitPrice = 20。
-
第四欄傳回 NULL,因為沒有單一結果,其中 UnitPrice = 15。在此資料集中,有兩個 UnitSales 值,其中 UnitPrice =15。
範例 - Only 使用情境
概述
資料集包含依期間劃分的產品銷售額。該公司想要判定某種產品是在單一商店銷售或是在多家商店銷售。
開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。
載入指令碼包含:
-
載入到稱為 Example 之資料表格的資料集。
-
資料表格中的欄位如下:
-
Date
-
RecordID
-
StoreProduct
-
Sales
-
載入指令碼
Example:
Load * inline [
Date, RecordID, StoreProduct, Sales, Store
2024-01-01, 1, Apple, 100, A
2024-01-15, 2, Banana, 80, B
2024-01-30, 3, Carrot, 50, C
2024-02-05, 4, Apple, 120, A
2024-02-20, 5, Banana, 90, B
2024-02-28, 6, Carrot, 60, D
2024-03-10, 7, Apple, 110, A
2024-03-22, 8, Banana, 85, B
2024-03-31, 9, Carrot, 55, C
];結果
載入資料並開啟工作表。建立新的表格並將此欄位新增為維度:
-
StoreProduct
建立下列量值:
-
=Only(Store),用來計算銷售該產品的唯一商店。
-
=If(IsNull(Only(Store)), 'Multiple Stores', 'Single Store'),用來計算該產品是在單一商店或是在多個商店銷售。
-
=Sum(Sales),用來計算產品銷售總額。
| StoreProduct | Only(Store) | If(IsNull(Only(Store)), 'Multiple Stores', 'Single Store') | Sum(Sales) |
|---|---|---|---|
| Apple | A | Single Store | 330 |
| Banana | B | Single Store |
255 |
| Carrot | - | Multiple Stores | 165 |
結果指出,Apples 和 Bananas 僅在一家商店銷售,而 Carrots 則在多家商店銷售。
範例 - 建立單月記錄表格的進階使用情境
概述
此範例使用 Only 函數顯示單一月份的逐行記錄表格。為了做到這一點,集合分析用於確定所需記錄集的範圍。集合分析至少需要一個量值,雖然我們實際上並不需要彙總記錄集,但我們能夠有效地使用 Only 彙總函數來裝載集合分析範圍。
開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。
載入指令碼包含:
-
載入到稱為 Example 之資料表格的資料集。
-
前置 LOAD 陳述式截斷月份的日子、使用 MonthStart 函數並以日期呈現方式傳回每月 1 號,藉此從 Date 欄位衍生出 MonthYear 欄位。此結果提供給 Date 函數,以將顯示格式設為 MMM YY,例如 Jan 24。
-
資料表格中的欄位如下:
-
Date
-
RecordID
-
StoreProduct
-
Sales
-
Store
-
載入指令碼
Example:
Load
*,
date(MonthStart (Date),'MMM YY') AS MonthYear
inline [
Date, RecordID, StoreProduct, Sales, Store
2024-01-01, 1, Apple, 100, A
2024-01-15, 2, Banana, 80, B
2024-01-30, 3, Carrot, 50, C
2024-02-05, 4, Apple, 120, A
2024-02-20, 5, Banana, 90, B
2024-02-28, 6, Carrot, 60, D
2024-03-10, 7, Apple, 110, A
2024-03-22, 8, Banana, 85, B
2024-03-31, 9, Carrot, 55, C
];結果
載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度:
-
MonthYear
-
RecordID
-
Date
-
StoreProduct
-
Store
建立下列量值:
-
=Only({$<MonthYear={'$(=MaxString(MonthYear))'}>} Sales),用來根據表格中的目前選項,評估 Sales 值並僅傳回 MonthYear 是最新 (最近) 可用 MonthYear 的內容。貨幣符號 ($) 展開使用 MaxString 函數 $(=MaxString(MonthYear)) 執行線上計算,這會以文字呈現方式傳回最新的 MonthYear 值。
| MonthYear | RecordID | Date | StoreProduct | Only({$<MonthYear={‘$(=MaxString(MonthYear))’}>} Sales) |
|---|---|---|---|---|
| Mar 24 | 7 | 2024-03-10 | Apple | 110 |
| Mar 24 | 8 | 2024-03-22 |
Banana |
85 |
| Mar 24 | 9 | 2024-03-31 | Carrot | 55 |
Only 函數的輸出會產生記錄表格,預設呈現資料中可用的最新或最近的 MonthYear 銷售值。如果您從 MonthYear 欄中選取較早的月份,例如 Feb 24,則會出現該月份的最新銷售值。