value_format

用途

view: view_name {
  dimension: field_name {
    value_format:"$#.00;($#.00)"
  }
}
階層
value_format
使用可能なフィールドタイプ
ディメンション、メジャー

許可
Excel 形式の書式設定を含む文字列

定義

value_format パラメータを使用すると、Looker のデータ値を Excel 形式を使用して書式設定できます。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] フィールドの ビジュアリゼーション で使用される書式設定と同じですが、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 で適用した形式の上に適用されます。例と詳細については、数値形式のローカライズのドキュメント ページをご覧ください。