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 では日付の書式設定、色の書式設定、16 進変換はサポートされていません。

もっともよく使われる書式設定オプションのいくつかを、以下に示します。国際通貨記号などの一部の特殊文字は、二重引用符で囲む必要があります。

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 の癖の 1 つは、SQL が整数演算を処理する方法です。5 を 2 で割ると、ほとんどの人は結果が 2.5 になると予想します。ただし、多くの SQL 言語では、2 つの整数を割ると結果も整数になるため、結果は 2 になります。この問題を解決するには、分子に 10 進数(1.0、100.0 など)を乗算して、SQL に 10 進数の結果を強制的に返すようにします。次に例を示します。

measure: active_users_percent {
  type: number
  sql: 100.000 * ${active_users} / ${users} ;;
  value_format: "0.000"
}

number_format ユーザー属性で value_format を使用する

モデル内のフィールドの形式を設定するために value_format を使用する場合は、number_format ユーザー属性で選択された数値の形式は、value_format で適用された形式に加えて適用されます。例と詳細については、数値書式のローカライズのドキュメント ページをご覧ください。