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. 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.


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.

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.


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 become sub.

Was this page helpful?