Skip to main content

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. A date comparison adds a generated comparison column for each compared metric. Set the reporting period with SINCE, UNTIL, or DURING, and reference the comparison columns from ORDER BY or VISUALIZE.

Syntax

COMPARE TO comparison[, ...]

comparison:
date [UNTIL date]
TARGETS

Note

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 as COMPARE TO startOfQuarter(-3q) UNTIL endOfQuarter(-3q).

  • Compare against configured metric targets with COMPARE TO TARGETS.

    This comparison has stricter requirements than a date comparison. The query must use TIMESERIES, must set a reporting period with SINCE, UNTIL, or DURING, and can't combine TARGETS with another comparison period. Unlike a date comparison, it doesn't add a generated comparison column or target values to the result set.

Examples

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 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_period
previous_period
previous_period

One period before the base date range.

Anchor to previous_year
previous_year
previous_year

One year before the base date range.

Anchor to previous_year_match_day_of_week
previous_year_match_day_of_week
previous_year_match_day_of_week

52 weeks before the base date range.

Anchor to previous_quarter
previous_quarter
previous_quarter

One quarter before the base date range.

Anchor to previous_month
previous_month
previous_month

One month before the base date range.

Anchor to previous_week
previous_week
previous_week

One week before the base date range.

Anchor to previous_day
previous_day
previous_day

One day before the base date range.

Anchor to previous_hour
previous_hour
previous_hour

One hour before the base date range.

Anchor to previous_minute
previous_minute
previous_minute

One minute before the base date range.

Anchor to previous_second
previous_second
previous_second

One second before the base date range.

Examples

ShopifyQL

FROM sessions
SHOW sessions
TIMESERIES day
DURING last_week
COMPARE TO previous_period
ORDER BY day ASC

Anchor to Generated comparison columnsGenerated comparison columns

When a query uses a date comparison, 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 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 become sub.
Examples

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 10

Was this page helpful?