值格式

用量

视图: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 设置模型中的字段格式,则在 number_format 用户属性中选择的数字格式将应用于使用 value_format 的格式。如需查看示例和了解详情,请访问本地化数字格式文档页面。