Skip to main content

WITH

Use WITH to apply optional modifiers, which are keywords that add result columns or change how ShopifyQL calculates results. Modifiers can add generated result columns, apply attribution models, change currency and timezone calculations, or calculate cumulative values.

Use one WITH clause for each query. To apply more than one modifier, separate modifiers with commas in the same WITH clause.


Choose optional query modifiers for generated columns, currency and timezone calculations, attribution, or cumulative values. Modifier-specific sections describe output columns and constraints.

Anchor to TOTALS
TOTALS
WITH TOTALS

Adds top-level metric total columns named {metric_name}__totals.

Anchor to GROUP_TOTALS
GROUP_TOTALS
WITH GROUP_TOTALS

Adds totals for grouped dimensions named {metric_name}__{dimension_names}_totals.

Anchor to PERCENT_CHANGE
PERCENT_CHANGE
WITH PERCENT_CHANGE

Adds percent-change columns for each comparison metric.

Anchor to CUMULATIVE_VALUES
CUMULATIVE_VALUES
WITH CUMULATIVE_VALUES

Adds running-total columns for additive metrics.

Anchor to CURRENCY
CURRENCY
WITH CURRENCY '<currency_code>'

Runs currency calculations in the specified currency.

Anchor to TIMEZONE
TIMEZONE
WITH TIMEZONE '<iana_timezone>'

Runs time-based calculations in the specified IANA timezone.

Anchor to attribution model
attribution model
WITH FIRST_CLICK_ATTRIBUTION | LAST_CLICK_ATTRIBUTION | LAST_NON_DIRECT_CLICK_ATTRIBUTION | ANY_CLICK_ATTRIBUTION | LINEAR_ATTRIBUTION

Applies marketing attribution models to eligible sales metrics.


Anchor to Generated result columnsGenerated result columns

Some WITH modifiers add generated result columns in addition to the columns that SHOW returns. ShopifyQL bases each generated column name on the metric name that the query returns. If a field in SHOW uses AS, then ShopifyQL uses the alias in the generated column name.

You can use generated result columns in clauses that operate on returned values, such as ORDER BY and VISUALIZE, when the columns are valid for the query.

Note

When a WITH modifier adds generated result columns, not all of them can be used in HAVING. For more information about filtering aggregated results, refer to HAVING.

WITH TOTALS adds a total column for each metric, aggregated across the entire result set, when a query groups results with GROUP BY or TIMESERIES. ShopifyQL sums additive metrics like net_sales, and recomputes ratio and percentage metrics, such as average_order_value, over the full result set instead of summing the grouped rows. ShopifyQL adds the __totals suffix to each metric name:

  • net_sales__totals is the totals column generated for net_sales.

WITH GROUP_TOTALS adds subtotal columns when a query groups by at least two dimensions. ShopifyQL generates a subtotal column for each prefix of the grouped dimensions except the full set, so grouping by three dimensions adds two subtotal columns. ShopifyQL names each column after the metric and the dimensions in its prefix:

  • Grouping by two dimensions adds one column, {metric_name}__{first_dimension}_totals, such as total_sales__country_totals for the country subtotal of total_sales.
  • Grouping by three dimensions adds two columns, {metric_name}__{first_dimension}_totals and {metric_name}__{first_dimension}_{second_dimension}_totals, in the order of the grouped dimensions.

Anchor to Percent-change columnsPercent-change columns

When the reporting period runs up to now or today, ShopifyQL compares each period only through the same point in its grain, so a partial current period is measured against an equal period-to-date instead of against a full prior period. For example, on the 17th of a month, each comparison month is measured through its own 17th.

WITH PERCENT_CHANGE adds columns for date comparison metrics created by COMPARE TO. Without a date comparison, this modifier doesn't add percent-change result columns. ShopifyQL names each column after the compared metric and the comparison period:

  • A relative comparison, such as COMPARE TO previous_year, generates percent_change_net_sales__previous_year.
  • A date-function comparison generates a column such as percent_change_total_sales__startOfQuarter_sub_3q, where minus signs in the date function become sub.

Attribution models reallocate conversion credit for eligible sales metrics across the marketing touchpoints in a customer's journey, so you can compare how different crediting rules change a metric like net_sales. Group by a marketing dimension, such as utm_campaign or referring_channel, so ShopifyQL can attribute results to touchpoints, and reference the generated attribution columns by their suffixed names. ANY_CLICK_ATTRIBUTION gives full credit to every touchpoint, so its attributed totals can add up to more than the metric's overall value.

Use attribution model modifiers to add marketing-attribution variants for eligible sales metrics. You can include multiple attribution models in the same WITH clause to compare models side by side.

Attribution modelCredit assignment
FIRST_CLICK_ATTRIBUTIONAssigns all conversion credit to the first clicked touchpoint. Column suffix: __first_click.
LAST_CLICK_ATTRIBUTIONAssigns all conversion credit to the final clicked touchpoint. Column suffix: __last_click.
LAST_NON_DIRECT_CLICK_ATTRIBUTIONAssigns credit to the last non-direct marketing touchpoint. Column suffix: __last_non_direct_click.
ANY_CLICK_ATTRIBUTIONAssigns full credit to every clicked touchpoint. Column suffix: __any_click.
LINEAR_ATTRIBUTIONDivides credit equally across touchpoints. Column suffix: __linear.
multiple modelsMultiple models can be combined to compare attribution strategies side by side.

Anchor to Attribution result columnsAttribution result columns

Attribution result columns use the returned metric name with a suffix for each attribution model. If the metric is aliased in SHOW, then ShopifyQL uses the alias in the generated column name.

Anchor to FIRST_CLICK_ATTRIBUTION
FIRST_CLICK_ATTRIBUTION
net_sales__first_click

First-click attribution column.

Anchor to LAST_CLICK_ATTRIBUTION
LAST_CLICK_ATTRIBUTION
net_sales__last_click

Last-click attribution column.

Anchor to LAST_NON_DIRECT_CLICK_ATTRIBUTION
LAST_NON_DIRECT_CLICK_ATTRIBUTION
net_sales__last_non_direct_click

Last-non-direct-click attribution column.

Anchor to ANY_CLICK_ATTRIBUTION
ANY_CLICK_ATTRIBUTION
net_sales__any_click

Any-click attribution column.

Anchor to LINEAR_ATTRIBUTION
LINEAR_ATTRIBUTION
net_sales__linear

Linear attribution column.


WITH CUMULATIVE_VALUES adds a running-total column for each additive metric, where summing values across time is meaningful, such as sums and counts like net_sales, orders, and customers. Ratio and average metrics, such as average_order_value or conversion_rate, don't generate cumulative columns.

Cumulative values require a time-based order, so combine CUMULATIVE_VALUES with TIMESERIES or ORDER BY on a time dimension. Reference a generated running total by the metric's __cumulative column, or by its alias if the metric is renamed with AS.


Anchor to Currency and timezoneCurrency and timezone

Use WITH CURRENCY '<currency_code>' to run currency calculations using a valid currency code. Use WITH TIMEZONE '<iana_timezone>' to run time-based calculations using a valid IANA timezone. Both values are strings, so wrap them in single quotes.


Was this page helpful?