命名規則のベストプラクティス
はじめに
ディメンション名とメジャー名は、特にモデルが大きくなるにつれて混乱を招く可能性があります。
LookML の作成時に適切なフィールド参照を作成し、フィールド ピッカーで正しいフィールドを選択するには、慎重に名前を付ける必要があります。
注:日本ではフィールド名をそのまま使わず、label: パラメーターで日本語名を付けるのが自然Yoshitake.icon
これに役立つ 1 つの方法は、一貫した命名方法です。次の規則は、Looker 社内でよく使用される規則です。
カウントのメジャー:Count
行数を数える(カウントする)メジャーには「Count」を使う
type: count
「[フィルター名]_[エンティティ名]_Count」とする
code: count
- view: users
fields:
- measure: count
type: count
- measure: active_count
type: count
filters:
status: 'active'
LookerのUI上では次のような表示になる
USERS Count
USERS Active Count
これらのフィールド名は自然に読み取れますが、ビジネス ユーザーは最初は「number of」や「total」ではなく「count」を検索することがわからないかもしれません。
ただし、簡単なトレーニングを行えば、これが最適な選択肢であることがわかります。
counts には「number of」や「total」を使用しません。これらの単語は他のケース(後述)使用するためです。
一意な値の数(ユニーク数)のメジャー:Unique Count
一意な値(ユニーク数)を数えるメジャーには「Unique Count」を使う
type: count_distinct
「Unique_[フィルター名]_[エンティティ名]_Count」とする
code: Unique Count
- view: users
fields:
- measure: unique_user_count
type: count_distinct
sql: ${id}
- measure: unique_active_user_count
type: count_distinct
sql: ${id}
filters:
status: 'active'
- measure: unique_name_count
type: count_distinct
sql: ${name}
LookerのUI上では次のような表示になる
USERS Unique User Count
USERS Unique Active User Count
USERS Unique Name Count
合計値のメジャー:Total
合計値のメジャーには「Total」を使う
type: total
「Total_[フィルター名]_[エンティティ名]」とする
code: Total
- view: orders
fields:
- measure: total_revenue
type: sum
sql: ${TABLE}.revenue
- measure: total_chicago_revenue
type: sum
sql: ${TABLE}.revenue
filters:
city: 'Chicago'
LookerのUI上では次のような表示になる
ORDERS Total Revenue
ORDERS Total Chicago Revenue
数量のディメンション:Numbers of
数量のディメンションには「Numbers of」を使う
type: number
「Number_of_[エンティティ名]」とする
code: number of
- view: orders
fields:
- dimension: number_of_items
type: number
sql: ${TABLE}.items
LookerのUI上では次のような表示になる
ORDERS Number of Items
ここで「Count(カウント)」や「Total(合計)」という単語を使用すると、ディメンションを操作しているのかメジャーを操作しているのかがわからなくなるため、混乱しやすくなります
平均のメジャー:Avg
平均のメジャーには「Avg」を使う
type: average
「Avg_[フィルター名]_[エンティティ名]_」とする
code: Average
- view: orders
fields:
- measure: avg_revenue
type: average
sql: ${TABLE}.revenue
- measure: avg_chicago_revenue
type: average
sql: ${TABLE}.revenue
filters:
city: 'Chicago'
LookerのUI上では次のような表示になる
ORDERS Avg Revenue
ORDERS Avg Chicago Revenue
比率:Over または Per
比率には「Over」「Per」を使う
他の二つのフィールド間の比率に基づくメジャーやディメンションは[分子]/[分母]で記述される
code: per
- view: orders
fields:
- dimension: revenue_per_number_of_items
type: number
sql: ${revenue} / ${number_of_items}
- measure: total_revenue_per_unique_customer
type: number
sql: ${total_revenue} / ${unique_customer_count}
LookerのUI上では次のような表示になる
ORDERS Revenue Per Number Of Items
ORDERS Total Revenue Per Unique Customer
最初のディメンションを「Avg Revenue Per Item」のような名前にすると、「type: average」メジャーではなく「type: number」ディメンションを使用していることがわかりにくくなります。
その他
yes/no フィールドに明確な名前を付けます
良い例:「Is Returned」
悪い例:「Returned」
ディメンション グループでは date や time という単語を避けます
Looker は各タイムフレームをディメンション名の末尾に追加します
created_date は
created_date_date
created_date_month
などになります。
created を使用すると created_date、created_month などになります。