用量
view: view_name {
dimension: field_name {
value_format:"$#.00;($#.00)"
}
}
|
階層
value_format |
可能的欄位類型
維度、評估指標
接受
包含 Excel 樣式格式的字串
|
定義
value_format 參數可讓您使用 Excel 樣式的格式,在 Looker 中設定資料值的格式。你可以透過下列方式使用 value_format:
- 在欄位層級,直接對特定維度或指標套用 Excel 樣式格式
- 做為
named_value_format參數的子參數,在模型層級建立可重複使用的自訂格式,並套用至多個欄位
直接將 value_format 套用至維度和測量指標
value_format 參數可用於維度和指標。如要透過 value_format 將 Excel 樣式的格式套用至維度,維度類型必須為 number。如要透過 value_format 為指標套用 Excel 樣式的格式,請確認指標為數值,而非 type: string。例如:
dimension: order_amount {
type: number
sql: ${TABLE}.order_amount ;;
value_format: "$#.00;($#.00)"
}
measure: total_order_amount {
type: sum
sql: ${order_amount} ;;
value_format: "$#.00;($#.00)"
}
使用 value_format 定義可重複使用的自訂格式
如要瞭解如何搭配 named_value_format 使用 value_format 定義可重複使用的格式,請參閱 named_value_format 參數的說明文件頁面。
預設格式設定選項
如要套用 Looker 內建的值格式,可以從 value_format_name 參數說明頁面的「預設格式名稱」部分中選擇格式。如要瞭解如何使用 value_format_name 套用內建格式,請參閱該頁面。
常見格式化字串
value_format 接受 Excel 樣式的格式設定字串。
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)
# 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)
# Note division by 1 million happens automatically
value_format: "0.000,\" K\"" # Number in thousands with 3 decimals (1.234 K)
# 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%)
如需使用 value_format 參數的進階條件式格式設定範例,請參閱「使用 value_format 的條件式格式設定」最佳做法頁面。
常見挑戰
除法運算時遺失小數位數
使用 value_format 時,SQL 處理整數運算的方式是常見的 SQL 奇怪之處。如果將 5 除以 2,大多數人會預期結果為 2.5。不過,許多 SQL 方言只會傳回 2,因為當兩個整數相除時,結果也會是整數。為解決這個問題,您可以將分子乘以十進位數字 (例如 1.0 或 100.0),強制 SQL 傳回十進位結果。例如:
measure: active_users_percent {
type: number
sql: 100.000 * ${active_users} / ${users} ;;
value_format: "0.000"
}
搭配 number_format 使用者屬性使用 value_format
如果您使用 value_format 格式化模型中的欄位,系統會將 number_format 使用者屬性中選取的數字格式,套用至以 value_format 套用的格式。如需範例及瞭解詳情,請參閱「本地化數字格式」說明文件頁面。