dimension_group: created {
type: time
timeframes: [
raw,
time,
date,
]
sql: ${TABLE}.created_at ;;
}
用于比较基于 created_raw 按两个动态时间范围(时间范围 A 和 时间范围 B)过滤的计数衡量标准(订单数 A 和 订单数 B)的 LookML 如下所示:
## 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"]],["最后更新时间 (UTC):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."]]