用量
层次结构
conditionally_filter |
默认值
无接受
字段名称的一个或多个过滤条件规范以及 Looker 过滤条件表达式,以及 unless 部分中的一个或多个字段名称的列表
|
定义
借助 conditionally_filter
参数,您可以定义一组默认过滤器,如果用户应用至少一个您定义的第二个列表中的过滤器,则可以替换该过滤器。
此参数通常用于防止用户不小心创建大型查询,这些查询在数据库上运行费用过高。例如,您可以强制用户将查询范围限制到上一周,除非用户明确要求设置更大的日期范围。
conditionally_filter
中应用的过滤条件会在用户运行查询后显示。虽然用户可以更改您设置的默认 value
,但除非他们应用您在 unless
子参数中指定的至少一个过滤条件,否则他们无法完全移除该过滤条件。
您使用的字段名称可以是 dimension
或 measure
的名称。
如需引用属于合并视图(而非此探索)的一部分的维度或衡量,请使用 view_name.field_name
。
示例
请思考以下示例:
explore: order {
conditionally_filter: {
filters: [id: "123", customer.id: "678,789"]
unless: [date]
}
join: customer {
sql_on: ${order.customer_id} = ${customer.id} ;;
}
}
在本例中,id
过滤条件引用了“探索”中名为 order
的 id
字段。customer.id
过滤条件引用了名为 customer
的视图中的 id
字段。除非用户在“探索”界面中设置订单日期,否则系统会应用这两个过滤条件。此示例还演示了您可能需要多个过滤条件。
您指定的默认值可以接受这些类型的表达式。
您也可以强制用户使用订单 ID 过滤条件(默认值为“123”,但可以对此进行更改),除非他们应用订单日期过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123"]
unless: [date]
}
}
或者,强制用户使用订单 ID 过滤条件(默认值为“123”或“234”,他们可以更改这些值),除非用户应用订单日期或订单时间过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123,234"]
unless: [date, time]
}
}
或者,强制用户使用订单 ID 过滤条件(默认值为“123”)和客户城市过滤条件(默认为“芝加哥”),除非用户应用订单日期或客户日期过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123", customer.city: "Chicago"]
unless: [date, customer.date]
}
join: customer {
sql_on: ${order.customer_id} = ${customer.id} ;;
}
}
常见挑战
使用 conditionally_filter
时,用户无法移除所有过滤条件
使用 conditionally_filter
时,如果没有任何过滤条件,则无法运行查询。用户必须使用您指定的条件过滤器,也可以使用 unless
列表中的过滤器。
群组中的某个维度为 type: time
的 conditionally_filter
会将该群组的其他维度放入 unless
子参数
如果您在 conditionally_filter
中指定的 field
是基于时间的维度,并且是维度组的一部分,那么 Looker 会将该组的所有其他维度都视为受该条件过滤条件的 unless
子参数的约束,即使您没有添加 unless
子参数也是如此。
LookML 对以下两个块进行相同解释。此处,conditionally_filter
应用于基于时间的维度 event_date
,该维度是 event
维度组的一部分。未指定 unless
条件,但 Looker 会处理 event
组中的其他维度,就好像这些维度是通过 unless
子参数指定的一样。
LookML 块 1:
explore: logs {
# Make sure there is always a filter on event_date, event_week, event_month or event_year
# Default to the last complete day of data
conditionally_filter: {
filters: [logs.event_date: "1 days ago for 1 day"]
}
view: logs {
# Combine the partition date filters and the time filters into a single field group.
dimension_group: event {
type: time
timeframes: [date,week,month,year]
sql: _PARTITIONTIME ;;
}
}
LookML 代码块 2:
explore: logs {
# Make sure there is always a filter on event_date, event_week, event_month or event_year
# Default to the last complete day of data
conditionally_filter: {
filters: [logs.event_date: "1 days ago for 1 day"]
unless: [event_week, event_month, event_year]
}
view: logs {
# Combine the partition date filters and the time filters into a single field group.
dimension_group: event {
type: time
timeframes: [date,week,month,year]
sql: _PARTITIONTIME ;;
}
}
即使只有第二个 LookML 代码块将 unless
子参数明确应用于 event
组的其他维度,Looker 也会以相同的方式解读这两个 LookML 代码块。
注意事项
有一种方法是将 conditionally_filter
应用于部分用户
如需对部分用户应用条件过滤器,而不对其他用户应用,您可以使用模型权限。您需要创建两个模型:一个使用 conditionally_filter
,另一个不使用。然后,您可以针对特定用户授予对适当模型的访问权限。
如果您想在没有 unless
的情况下使用 conditionally_filter
,只需使用 always_filter
即可
如需强制用户无论使用哪组特定过滤条件,但允许用户更改默认值,请改用 always_filter
。
如果您需要完全无法更改的过滤条件,不妨考虑使用 sql_always_where
如果您希望“探索”页面中的过滤条件对所有人都相同,且不允许用户更改过滤条件值,请使用 sql_always_where
。
如果您需要更改无法更改的针对具体用户的过滤条件,请考虑使用access_filter
如果您想让“探索”专区具有特定于每个用户的过滤条件,但无法移除或更改,请使用 access_filter
。