named_value_format

用法

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 中创建和使用自定义格式需要执行以下几个步骤:

  1. 首先,您需要在模型文件中使用 named_value_format 为新的自定义格式声明一个名称。此名称必须是不含任何空格的字符串。
  2. 接下来,您可以使用 named_value_format 下的 value_format 子参数,通过提供 Excel 样式的格式字符串来定义所需的格式。
  3. 最后,在字段级通过 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 用户属性

通常,在 number_format 用户属性中设置的数字格式会应用于使用 named_value_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: "(###) ###-####"
}