用法
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 中,自定义格式的创建和使用需要执行以下几个步骤:
- 首先,您需要在模型文件中使用
named_value_format为新的自定义格式声明一个名称。此名称必须是不含任何空格的字符串。 - 接下来,您需要在
named_value_format下使用value_format子参数,通过提供 Excel 式格式字符串 来定义所需的格式。 - 最后,在字段级
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: "(###) ###-####"
}