0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Looker】dimensionを任意の表示順序でソートする

Last updated at Posted at 2020-09-25

2020/9/28追記
caseパラメータを用いた場合、記述した順序でexploreでもソートしてくれます。
公式リファレンス
ただし、
・ラベルのデータ型はstringで固定
・ラベルに変数を使えない
等の制約があるため、今回紹介するやり方を使うべきかはケースバイケースです。

例題

下記のデータが入ったSampleテーブルがあるとします。

id name
1 aaa
2 bbb
3 ccc
4 ddd
5 eee

Exploreでnameを基準にソートさせたとき

id name
3 ccc
1 aaa
2 bbb
5 eee
4 ddd

という順序で表示したい場合を考えてみましょう。

結論

ソート用のdimensionを定義し、order_by_fieldパラメーターに作成したソート用のdimensionを指定することで、任意の表示順序でソートさせることができます。

sample.view

view: sample {
︙
︙
  dimension: id {
    primary_key: yes
    type: number
    sql: ${TABLE}."id"
  }

  dimension: name_sort {
    type: number
    sql:
      CASE ${id}
        WHEN 1 THEN 2
        WHEN 2 THEN 3
        WHEN 3 THEN 1
        WHEN 4 THEN 5
        WHEN 5 THEN 4
        ELSE NULL
      END;;
  }

  dimension: name {
    type: string
    sql: ${TABLE}."name"
    order_by_field: name_sort
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?