value_format

用法

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_formatnamed_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_formatnumber_format 用户属性搭配使用

如果您使用 value_format 设置模型中字段的格式,则在 value_format 应用的格式之上,系统还会应用在 number_format 用户属性中选择的数字格式。如需查看示例并了解详情,请访问本地化数字格式文档页面。