set

用量

view: view_name {
   set: set_name {
      fields: [field, field, ]
   }
}
階層
set
可能的欄位類型
維度、維度群組、篩選欄位、指標

接受
以方括號括住的欄位名稱清單

定義

在需要欄位清單的其他參數 (例如 fields (用於聯結)drill_fields (用於欄位)) 中參照集合時,集合會很有用。

您可以使用 set 參數定義每個集合。一組可包含目前檢視畫面中的任意數量維度、指標或篩選器欄位,包括維度群組產生的個別維度。也可以使用 view_name.field_name 語法,包含其他檢視區塊的欄位。

set 參數內,新增 fields 參數,然後列出欄位,如以下範例所示:

set: my_first_set {
  fields: [
    dimension_one,
    another_view.dimension_two,
    measure_one
  ]
}

集合也可以包含其他集合。如要區分集合名稱與維度或指標名稱,請加入 * 字元。例如:

set: my_first_set {
  fields: [
    dimension_one,
    another_view.dimension_two,
    measure_one
  ]
}

set: my_second_set {
  fields: [
    dimension_three,
    measure_two
  ]
}

set: my_third_set {
  fields: [
    my_first_set*,
    my_second_set*
  ]
}

名為 my_third_set 的集合會包含下列欄位:

  • dimension_one
  • another_view.dimension_two
  • measure_one
  • dimension_three
  • measure_two

最後,您可以使用 - 字元排除欄位。例如:

set: my_first_set {
  fields: [
    dimension_one,
    another_view.dimension_two,
    measure_one
  ]
}

set: my_second_set {
  fields: [
    dimension_three,
    measure_two
  ]
}

set: my_third_set {
  fields: [
    my_first_set*,
    my_second_set*
  ]
}

set: my_fourth_set {
  fields: [
    my_first_set*,
    -measure_one
  ]
}

名為 my_fourth_set 的集合會包含下列欄位:

  • dimension_one
  • another_view.dimension_two

範例

建立名為 financial_data 的集合

set: financial_data {
  fields: [
    subtotal,
    shipping,
    tax,
    total,
    cost,
    profit
  ]
}

在「Customers」(顧客) 檢視畫面中建立名為 basic_customer_data 的集合,並參照「Customer Order Facts」(顧客訂單事實) 檢視畫面:

set: basic_customer_data {
  fields: [
    name,
    address,
    status,
    customer_order_facts.lifetime_orders,
    customer_order_facts.lifetime_revenue
  ]
}

常見挑戰

其他檢視區塊的欄位必須彙整至使用 set 的探索

如要在 set 中參照來自其他檢視區塊的欄位,請務必將該檢視區塊彙整至使用 set探索。舉例來說,以下程式碼無法運作:

模型檔案

explore: orders { ... }

查看檔案

view: orders {
  set: customer_info {
    fields: [customer.name]
  }
}

這裡的 customers 尚未加入 orders,因此無法在 customer_info 集中參照 customers 的欄位。

dimension_group 中的欄位新增至集合

如要將 dimension_group 中的欄位新增至資料集,您必須個別新增每個時間範圍維度。舉例來說,請看以下 dimension_group

dimension_group: created {
  type: time
  timeframes: [date, week, month]
  sql: ${TABLE}.created_at ;;
}

您無法在類似這樣的設定中加入日期、週和月維度:

set: created_timeframes {
  fields: [created]
}

請改為個別新增時間範圍,如下所示:

set: created_timeframes {
  fields: [
    created_date,
    created_week,
    created_month
  ]
}