COMPARE TO
Use COMPARE TO to compare the query's reporting period against another baseline. A comparison can use another comparison range, a relative comparison period, or configured targets. ShopifyQL adds a generated comparison column for each compared metric. Set the reporting period with SINCE, UNTIL, or DURING, and reference the comparison columns from SHOW, ORDER BY, or HAVING.
Syntax
Anchor to Comparison rangesComparison ranges
TARGETS is available only when the query's schema supports it.
TARGETS is available only when the query's schema supports it.
Set what ShopifyQL compares the reporting period against. A date comparison reuses the same relative, absolute, named, and date-function forms as the reporting period, with an optional UNTIL to set the comparison's end. Other comparisons measure results against configured metric goals.
- Compare to a relative period with a named operator, such as
COMPARE TO previous_year. - Compare to a period starting on an absolute date, such as
COMPARE TO 2023-01-01. - Compare to a named date range, such as
COMPARE TO last_week. - Compare to a period resolved by a date function, such as
COMPARE TO startOfQuarter(-3q). - Set an explicit comparison range end with
UNTIL, such asCOMPARE TO startOfQuarter(-3q) UNTIL endOfQuarter(-3q). - Compare against configured metric targets with
COMPARE TO TARGETS.
ShopifyQL
Examples
Description
Compare daily [`sessions`](/docs/api/shopifyql/latest/schemas/sessions_and_behavior/sessions#sessionsmetric-propertydetail-sessions) across January 2026 against the same days in January 2025. This example uses an absolute `COMPARE TO` range to anchor the comparison column to fixed prior dates.
ShopifyQL
FROM sessions SHOW sessions TIMESERIES day SINCE 2026-01-01 UNTIL 2026-01-31 COMPARE TO 2025-01-01 UNTIL 2025-01-31 ORDER BY day ASCDescription
Compare daily [`total_sales`](/docs/api/shopifyql/latest/schemas/sales_revenue/sales#salesmetric-propertydetail-totalsales) for the previous quarter against the quarter two before it. This example uses date functions with `COMPARE TO ... UNTIL` (`startOfQuarter` and `endOfQuarter`) to resolve both the reporting period and a fixed comparison range.
ShopifyQL
FROM sales SHOW total_sales TIMESERIES day SINCE startOfQuarter(-1q) UNTIL endOfQuarter(-1q) COMPARE TO startOfQuarter(-3q) UNTIL endOfQuarter(-3q) ORDER BY day ASC
Anchor to Relative comparison periodsRelative comparison periods
Compare the reporting period to an earlier period of the same length, measured back from the selected period. Use a named relative operator instead of writing out the comparison date range.
- Anchor to previous_periodprevious_
periodprevious_ period previous_periodprevious_period One period before the base date range.
- Anchor to previous_yearprevious_
yearprevious_ year previous_yearprevious_year One year before the base date range.
- Anchor to previous_year_match_day_of_weekprevious_
year_ match_ day_ of_ weekprevious_ year_ match_ day_ of_ week previous_year_match_day_of_weekprevious_year_match_day_of_week 52 weeks before the base date range.
- Anchor to previous_quarterprevious_
quarterprevious_ quarter previous_quarterprevious_quarter One quarter before the base date range.
- Anchor to previous_monthprevious_
monthprevious_ month previous_monthprevious_month One month before the base date range.
- Anchor to previous_weekprevious_
weekprevious_ week previous_weekprevious_week One week before the base date range.
- Anchor to previous_dayprevious_
dayprevious_ day previous_dayprevious_day One day before the base date range.
- Anchor to previous_hourprevious_
hourprevious_ hour previous_hourprevious_hour One hour before the base date range.
- Anchor to previous_minuteprevious_
minuteprevious_ minute previous_minuteprevious_minute One minute before the base date range.
- Anchor to previous_secondprevious_
secondprevious_ second previous_secondprevious_second One second before the base date range.
ShopifyQL
Examples
Description
Compare daily [`sessions`](/docs/api/shopifyql/latest/schemas/sessions_and_behavior/sessions#sessionsmetric-propertydetail-sessions) for last week against the week immediately before. This example uses `COMPARE TO previous_period` to derive the comparison range automatically from the reporting period's length.
ShopifyQL
FROM sessions SHOW sessions TIMESERIES day DURING last_week COMPARE TO previous_period ORDER BY day ASCDescription
Compare daily [`total_sales`](/docs/api/shopifyql/latest/schemas/sales_revenue/sales#salesmetric-propertydetail-totalsales) by sales channel for last month against the month before. This example uses `COMPARE TO previous_month` to benchmark each channel against the prior month.
ShopifyQL
FROM sales SHOW total_sales GROUP BY sales_channel TIMESERIES day DURING last_month COMPARE TO previous_month ORDER BY day ASC, total_sales DESC
Anchor to Generated comparison columnsGenerated comparison columns
When a query uses COMPARE TO, ShopifyQL adds a result column for each compared metric. The generated name combines the metric and the comparison period, so the same metric can carry more than one comparison column. Reference a generated comparison column by name in ORDER BY, HAVING, or VISUALIZE. The comparison period determines the suffix:
- A relative comparison generates
comparison_net_sales__previous_year. - An absolute-date comparison generates
comparison_net_sales__20230101. - A named-date comparison generates
comparison_net_sales__last_week. - A date-function comparison generates
comparison_net_sales__startOfQuarter_sub_3q, where minus signs becomesub.
ShopifyQL
Examples
Description
Rank product types by [`gross_sales`](/docs/api/shopifyql/latest/schemas/sales_revenue/sales#salesmetric-propertydetail-grosssales) for the current year. This example uses the generated `comparison_gross_sales__previous_year` column in [`ORDER BY`](/docs/api/shopifyql/latest/syntax/order-by) to sort by the previous year.
ShopifyQL
FROM sales SHOW gross_sales GROUP BY product_type DURING this_year COMPARE TO previous_year ORDER BY comparison_gross_sales__previous_year DESC LIMIT 10Description
Sort billing countries by [`orders`](/docs/api/shopifyql/latest/schemas/sales_revenue/sales#salesmetric-propertydetail-orders) for January 2024. This example uses an absolute `COMPARE TO` range, which generates a date-stamped column like `comparison_orders__20230101` to sort by.
ShopifyQL
FROM sales SHOW orders GROUP BY billing_country SINCE 2024-01-01 UNTIL 2024-01-31 COMPARE TO 2023-01-01 UNTIL 2023-01-31 ORDER BY comparison_orders__20230101 DESC LIMIT 10