本頁面是指 評估中的
filters參數。
filters也可用於原生衍生資料表,詳情請參閱explore_source參數說明文件頁面。
filters也可做為資訊主頁的一部分,詳情請參閱「資訊主頁參數」說明文件頁面。
filters也可以做為資訊主頁元素的一部分。如需使用範例,請參閱直條圖元素的說明文件頁面。
用量
view: view_name {
measure: field_name {
filters: [dimension_name: "filter expression", dimension_name: "filter expression", ... ]
}
}
|
階層
filters |
可能的欄位類型
評估
接受
一組維度名稱和對應的 Looker 篩選運算式
|
定義
filters 是選用的篩選器運算式清單,會套用至指標計算。這項功能僅適用於執行匯總作業的下列指標類型:
type: counttype: count_distincttype: sumtype: average
篩選器的語法如下:
filters: [dimension_name_1: "Looker filter expression", dimension_name_2: "Looker filter expression", ...]
如要瞭解如何編寫有效的篩選運算式,請參閱 Looker 的篩選器標記。將整個篩選運算式放在引號中,如下列範例所示。
您也可以搭配 filters 使用 Liquid 參數和範本化篩選器,建立動態篩選的測量指標,篩選條件值可根據使用者在資訊主頁或 Look 中的輸入內容動態變更。這樣一來,使用者就能選取只適用於特定指標的篩選器值,而不必在全域 WHERE 子句中篩選整個查詢。如需參考資料,請參閱本頁稍後的「建立動態篩選的指標」一節。
範例
filters 可用於多種指標。下列範例說明一些常見用途的解決方案。
篩選多個欄位
在本例中,這項指標會計算過去七天內建立的不重複 ID,且這些 ID 不屬於帳戶已停用的使用者。這個範例也說明如何為欄位新增多個篩選條件。每個篩選運算式都需要專屬的完整 filters 參數:
measure: this_week_count {
type: count_distinct
sql: ${TABLE}.id ;;
filters: [created_date: "7 days", user.status: "-disabled"]
# Reference fields from other joined views with view_name.field_name syntax
# Minus sign means "not" in this case, but check notation docs for details
}
篩選 yesno 維度
再舉一例,假設資料包含每位人員 (一般員工或約聘人員) 各一列,有一個 is_contractor 欄位,類型為 yesno,如果該人員是約聘人員,則包含 yes。這項指標只會計算承包商的資料列:
measure: contractor_count {
type: count
filters: [is_contractor: "yes"]
}
新增多個篩選條件值
在下一個範例中,filters 運算式包含多個值。這項指標會計算 state 欄位中符合下列任一值的資料列:
measure: customers_by_state {
type: count
filters: [state: "California, Nevada, Washington, Oregon"]
}
篩選 NULL 值
在這個範例中,這項指標會計算過去 7 天內建立的相異 ID,並在 user.email_address 欄位中包含不是 NULL 的值:
measure: this_week_count_with_email {
type: count_distinct
sql: ${TABLE}.id ;;
filters: [created_date: "7 days", user.email_address: "-NULL"]
}
由於欄位 user.email_address 的類型為 string,因此使用 "-NULL" 語法。詳情請參閱「Looker 篩選器運算式」說明文件頁面。
新增數值篩選條件
在本範例中,篩選器會取得數值。這項指標只會加入price超過 100 的訂單:
measure: total_amt_large_orders {
type: sum
filters: [orders.price: ">100"]
sql: ${orders.price} ;;
}
使用 AND 合併條件
在本範例中,指標會使用 篩選運算式和 AND 運算子,計算 18 至 25 歲年齡層顧客的訂單總數:
measure: sales_18_to_25 {
type: sum
filters: [customers.age: ">=18 AND <=25"]
sql: ${orders.price} ;;
}
使用半形逗號合併條件
在本例中,這項指標會使用 篩選運算式和 , 運算子,計算年齡小於 25 歲或大於 65 歲的顧客總訂單數:
measure: sales_less_25_or_greater_65 {
type: sum
filters: [customers.age: "<25, >65"]
sql: ${orders.price} ;;
}
建立動態篩選指標
在這個範例中,篩選器可讓使用者在 Look 或資訊主頁篩選器中,查看依使用者選擇的狀態篩選的訂單數量:
首先,建立僅限篩選的欄位,讓使用者選取值:
filter: status_filter {
type: string
suggest_dimension: status
}
接著,建立隱藏維度,利用範本篩選器擷取使用者在上一個僅限篩選器的欄位中選取的項目。只要在篩選器欄位中選取值,這個維度就會傳回「是」:
dimension: status_satisfies_filter {
type: yesno
hidden: yes
sql: {% condition status_filter %} ${status} {% endcondition %} ;;
}
最後,建立以 type: yesno 維度為篩選條件的指標:
measure: count_dynamic_status {
type: count
filters: [status_satisfies_filter: "yes"]
}
如果使用者選擇不為僅限篩選器的欄位選取值,SQL 會預設為 1=1,這不會影響指標值。
常見挑戰
請勿將 filters 與 type: number 的測量指標搭配使用
許多使用者嘗試將 filters 與 type: number 的測量值搭配使用,但這無法運作:
# Will NOT work
measure: total_food_profit {
type: number
sql: ${total_revenue} - ${total_cost} ;;
filters: [segment: "food"]
}
measure: total_revenue {
type: sum
sql: ${revenue} ;;
}
measure: total_cost {
type: sum
sql: ${cost} ;;
}
請改為將 filters 參數套用至組成 type: number 評估指標的任何個別評估指標,如下所示:
# Will work
measure: total_food_profit {
type: number
sql: ${total_food_revenue} - ${total_food_cost} ;;
}
measure: total_food_revenue {
type: sum
sql: ${revenue} ;;
filters: [segment: "food"]
}
measure: total_food_cost {
type: sum
sql: ${cost} ;;
filters: [segment: "food"]
}