用法
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 定义可重复使用的自定义格式
如需了解如何将 value_format 与 named_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"
}
将 value_format 与 number_format 用户属性搭配使用
如果您使用 value_format 设置模型中字段的格式,则在 number_format 用户属性中选择的数字格式将应用于使用
value_format 应用的格式之上。如需查看示例并了解详情,请访问本地化数字格式文档页面。