具名值格式

用量

named_value_format: desired_name {
value_format: “excel 格式字符串”
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 且类似于 $123M 的自定义格式以及一个名为 phone_number ((123) 456-7890) 的格式。

named_value_format: usd_in_millions {
  value_format: "$0.000,,\" M\""
}
named_value_format: phone_number {
  value_format: "(###) ###-####"
}