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