建立經過篩選的度量,以便使用 value = "yes" 條件參照 yesno 維度。這可確保指標只會匯總符合步驟 1 中定義篩選器所指定時間範圍條件的記錄。
這項邏輯可讓使用者建立分析和視覺化資料,比較不同時間範圍的值,例如下列探索:
使用者可以變更「時間範圍 A」和「時間範圍 B」篩選器中的值,但只會影響「訂單數量 A」和「訂單數量 B」的值。訂單數量 A和訂單數量 B 是含有篩選器的指標,可參照時間範圍篩選器中的時間範圍條件。時間範圍 A 會影響訂單數量 A 的值,而時間範圍 B 會影響訂單數量 B 的值。
以下提供本範例的 LookML。
LookML
以下 LookML 假設您有一個維度群組,其中包含名為 created_raw 的 raw 時間範圍:
dimension_group: created {
type: time
timeframes: [
raw,
time,
date,
]
sql: ${TABLE}.created_at ;;
}
比較計數指標 (訂單計數 A 和 訂單計數 B) 的 LookML 會根據 created_raw 篩選兩個動態時間範圍 (時間範圍 A 和 時間範圍 B),如下所示:
## filter determining time range for all "A" measures
filter: timeframe_a {
type: date_time
}
## flag for "A" measures to only include appropriate time range
dimension: group_a_yesno {
hidden: yes
type: yesno
sql: {% condition timeframe_a %} ${created_raw} {% endcondition %} ;;
}
## filtered measure A
measure: count_a {
type: count
filters: [group_a_yesno: "yes"]
}
## filter determining time range for all "B" measures
filter: timeframe_b {
type: date_time
}
## flag for "B" measures to only include appropriate time range
dimension: group_b_yesno {
hidden: yes
type: yesno
sql: {% condition timeframe_b %} ${created_raw} {% endcondition %} ;;
}
measure: count_b {
type: count
filters: [group_b_yesno: "yes"]
}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-31 (世界標準時間)。"],[],[],null,["# Timeframe versus timeframe analysis using templated filters\n\n\u003cbr /\u003e\n\n\nTo compare a metric filtered by several timeframes in the same Look or dashboard, you can use [filtered measures](/looker/docs/reference/param-field-filters). Filtered measures let you apply a hardcoded filter directly to a measure, rather than applying a filter to an entire query.\n\n\u003cbr /\u003e\n\n| **Note:** For [dialects that support period-over-period measures](/looker/docs/period-over-period#supported_dialects_for_period_over_period), you can create a LookML measure of `type: period_over_period` to create a period-over-period measure. See the [Period-over-period measures in Looker](/looker/docs/period-over-period) documentation page for more information.\n\n\u003cbr /\u003e\n\n\nIf you had a limited number of timeframes that you were interested in comparing, you could define a few measures with hardcoded timeframe filters (for example, \"this year\" and \"last year\") and display them in an Explore, a Look, or a dashboard. You can also take this approach a step further to make timeframe comparisons dynamic --- or measures that change with timeframes specified by users in [Explores](/looker/docs/creating-and-editing-explores), [Looks](/looker/docs/viewing-looks), or [dashboards](/looker/docs/viewing-dashboards) --- by using [templated filters](/looker/docs/templated-filters) in filtered measures.\n\nPattern overview\n----------------\n\n\nOn a high level, this approach involves three components:\n\n1. Define a templated filter of `type: date` for each timeframe that will appear as a [filter-only field](/looker/docs/reference/param-field-filter) on your Explore, Look, or dashboard.\n2. Create a [`yesno`](/looker/docs/reference/param-dimension-filter-parameter-types#yesno) type dimension to tie to the templated filter so that when a user selects a value for the filter-only field, the `yesno` dimension returns \"yes\" for records that meet the filter's conditions.\n3. Create a filtered measure that references the `yesno` dimension with the condition `value = \"yes\"`. This ensures that the measure only aggregates records that meet the timeframe condition that is specified in the filter that is defined in step 1.\n\n\u003cbr /\u003e\n\n\nThis logic lets users create analyses and visualizations that compare values from different timeframes, like the following Explore:\n\n\nUsers can change the values in the **Timeframe A** and **Timeframe B** filters and only impact the values for **Orders Count A** and **Orders Count B** . **Orders Count A** and **Orders Count B** are measures with filters that reference the timeframe conditions in the timeframe filters. **Timeframe A** impacts the values for **Orders Count A** , and **Timeframe B** impacts the values for **Orders Count B**.\n\n\nThe following section provides the LookML for this example.\n\nThe LookML\n----------\n\n\nThe following LookML assumes that you have a dimension group with a [`raw`](/looker/docs/reference/param-field-dimension-group#timeframe_options) timeframe named `created_raw`: \n\n```\n dimension_group: created {\n type: time\n timeframes: [\n raw,\n time,\n date,\n ]\n sql: ${TABLE}.created_at ;;\n }\n \n```\n\n\u003cbr /\u003e\n\n\nThe LookML for comparing count measures --- **Orders Count A** and **Orders Count B** --- filtered by two dynamic timeframes --- **Timeframe A** and **Timeframe B** --- based on `created_raw` looks like this:\n\n\u003cbr /\u003e\n\n```\n ## filter determining time range for all \"A\" measures\n filter: timeframe_a {\n type: date_time\n }\n ## flag for \"A\" measures to only include appropriate time range\n dimension: group_a_yesno {\n hidden: yes\n type: yesno\n sql: {% condition timeframe_a %} ${created_raw} {% endcondition %} ;;\n }\n ## filtered measure A\n measure: count_a {\n type: count\n filters: [group_a_yesno: \"yes\"]\n }\n ## filter determining time range for all \"B\" measures\n filter: timeframe_b {\n type: date_time\n }\n ## flag for \"B\" measures to only include appropriate time range\n dimension: group_b_yesno {\n hidden: yes\n type: yesno\n sql: {% condition timeframe_b %} ${created_raw} {% endcondition %} ;;\n }\n measure: count_b {\n type: count\n filters: [group_b_yesno: \"yes\"]\n }\n```\n\n\nYou can use this logic to create as many timeframe comparisons as you need.\n\nFiltering an entire query with dynamic timeframes\n-------------------------------------------------\n\n\nFiltered measures do not apply a filter condition to overall query results. If you want to limit the overall query results within the specified timeframes, you can:\n\n1. Create the following `yesno` dimension\n2. Filter the dimension values by \"yes\" in Explores, Looks, or dashboards:\n\n\u003cbr /\u003e\n\n```\n dimension: is_in_time_a_or_b {\n group_label: \"Time Comparison Filters\"\n type: yesno\n sql:\n {% condition timeframe_a %} ${created_raw} {% endcondition %} OR\n {% condition timeframe_b %} ${created_raw} {% endcondition %} ;;\n }\n```\n\n\nThis prevents the database from scanning more data than needed for the query --- and may help with performance and query cost."]]