用量
named_value_format: desired_name {
value_format: "excel formatting string"
strict_value_format: yes | no
}
|
階層
named_value_format |
預設值
無
接受
自訂格式的名稱,以及定義格式的 Excel 樣式格式字串,還有選用 strict_value_format 子參數的布林值
|
定義
您可以使用 named_value_format 參數建立及儲存自訂格式,並套用至多個維度和指標。
在 Looker 中建立及使用自訂格式的步驟如下:
- 首先,您可以在模型檔案中使用
named_value_format,為新的自訂格式宣告名稱。這個名稱必須是不含任何空格的字串。 - 接著,您可以使用
named_value_format下的value_format子參數,提供 Excel 樣式的格式字串,定義所需的格式。 - 最後,在欄位層級的
value_format_name參數中參照這個自訂格式,將其套用至維度和指標。
您可以在模型中定義多個自訂格式,如以下範例所示。
常見的 value_format 格式化字串
value_format參數使用的格式與視覺化「值格式」欄位使用的格式相同,但value_format參數需要以雙引號括住格式字串。如要瞭解視覺化效果中的值格式,請參閱「直條圖選項」說明文件頁面。
如要瞭解如何指定這些格式,請參閱 Excel 的完整指南說明文件。不過,Looker 目前不支援日期格式、顏色格式和十六進位轉換。
以下列舉幾個最常見的格式設定選項。部分特殊字元 (例如國際貨幣符號) 必須以雙引號括住。
value_format: "0" # Integer (123)
value_format: "*00#" # Integer zero-padded to 3 places (001)
value_format: "0 \" String\"" # Integer followed by a string (123 String)
# Please note "String" can be replaced with any other word
value_format: "0.##" # Number up to 2 decimals (1. or 1.2 or 1.23)
value_format: "0.00" # Number with exactly 2 decimals (1.23)
value_format: "*00#.00" # Number zero-padded to 3 places and exactly 2 decimals (001.23)
value_format: "#,##0" # Number with comma between thousands (1,234)
value_format: "#,##0.00" # Number with comma between thousands and 2 decimals (1,234.00)
value_format: "0.000,,\" M\"" # Number in millions with 3 decimals (1.234 M)
# Please note division by 1 million happens automatically
value_format: "0.000,\" K\"" # Number in thousands with 3 decimals (1.234 K)
# Please note division by 1 thousand happens automatically
value_format: "$0" # Dollars with 0 decimals ($123)
value_format: "$0.00" # Dollars with 2 decimals ($123.00)
value_format: "\"€\"0" # Euros with 0 decimals (€123)
value_format: "$#,##0.00" # Dollars with comma btwn thousands and 2 decimals ($1,234.00)
value_format: "$#.00;($#.00)" # Dollars with 2 decimals, positive values displayed
# normally, negative values wrapped in parenthesis
value_format: "0\%" # Display as percent with 0 decimals (1 becomes 1%)
value_format: "0.00\%" # Display as percent with 2 decimals (1 becomes 1.00%)
value_format: "0%" # Convert to percent with 0 decimals (.01 becomes 1%)
value_format: "0.00%" # Convert to percent with 2 decimals (.01 becomes 1.00%)
strict_value_format 會覆寫 number_format 使用者屬性
一般來說,系統會先套用以 named_value_format 建立的格式,再套用 number_format 使用者屬性中設定的數字格式。不過,如果將選用的 strict_number_format 子參數設為 yes,系統就不會套用 number_format 的格式,欄位會保留 named_value_format 的格式。如需範例及瞭解詳情,請參閱「本地化數字格式」說明文件頁面。
預設格式設定選項
如要套用 Looker 內建的值格式,可以從 value_format_name 參數說明頁面的「預設格式名稱」部分,選擇列出的格式。如要瞭解如何使用 value_format_name 套用內建格式,請參閱該頁面。
範例
建立名為 euro_in_thousands 的自訂格式,看起來像 €1.23K:
named_value_format: euro_in_thousands {
value_format: "\"€\"0.000,\" K\""
}
防止 number_formatting 使用者屬性影響 euro_in_thousands 格式:
named_value_format: euro_in_thousands {
value_format: "\"€\"0.000,\" K\""
strict_value_format: yes
}
如需其他範例,請參閱「本地化數字格式」說明文件頁面。
建立名為 usd_in_millions 的自訂格式 (如 $1.23M),以及名為 phone_number 的格式 (如 (123) 456-7890):
named_value_format: usd_in_millions {
value_format: "$0.000,,\" M\""
}
named_value_format: phone_number {
value_format: "(###) ###-####"
}