Skip to main content
Clause ordering and optionality
  • Required: FROM · SHOW · (SINCE or DURING)
  • Optional: GROUP BY · WHERE · UNTIL · TIMESERIES · SEGMENT BY · COMPARE TO · WITH · VISUALIZE
  • Canonical order: FROM → SHOW → (GROUP BY / WHERE / SEGMENT BY) → (SINCE [UNTIL] | DURING) → [TIMESERIES] → [SEGMENT BY] → [COMPARE TO] → [WITH …] → [VISUALIZE]
The parser accepts flexible ordering of WHERE, GROUP BY, and SEGMENT BY clauses before the time bound. SEGMENT BY can also appear after the time bound or TIMESERIES for backwards compatibility. Use either SINCE (with an optional UNTIL) or DURING to bound a query, but not both. DURING is a single-token shortcut for common ranges; SINCE/UNTIL gives you full control over the start and end. Minimal example
Full example

Clauses

FROM

  • Purpose: Selects the dataset.
  • Example:

SHOW

  • Purpose: Lists expressions to compute. Typically uses aggregation functions over metrics or fields.
  • Examples:

GROUP BY

  • Purpose: Defines the grouping keys for aggregation.
  • Keys: Can be identifiers, dimensions, or product_attributes.* paths.
  • TOP N Modifier: Limit results to the top N values for a dimension.
  • Examples:

TOP N modifier

The TOP N modifier limits grouped results to show only the highest-ranking values. Three variants are available:
  • TOP N: Shows the top N values plus an “Other” category for the remainder.
  • ONLY TOP N: Shows only the top N values, hiding the remainder entirely.
  • TOP N OVERALL: Ranks values across the full time range rather than per-period. Useful with TIMESERIES to maintain consistent rankings.

WHERE (optional)

  • Purpose: Filters rows before aggregation using boolean expressions over dimensions, fields, or attributes.
  • Comparison operators: =, !=, <, <=, >, >=
  • Logical operators: AND, OR, NOT
  • Null handling: IS NULL, IS NOT NULL
  • Precedence: Parentheses are supported for grouping conditions.
  • Examples:

SINCE

  • Purpose: Defines the start of the time window for the query.
  • Accepted forms:
    • Relative: -7d, -14d, -30d, -60d, -90d, -180d, -365d, -24h, -48h, -12m, -1y
    • Keywords: today, yesterday, this_week, last_week, this_month, last_month, this_quarter, last_quarter, this_year, last_year
    • Rolling windows: past_7_days, past_14_days, past_30_days, past_60_days, past_90_days, past_180_days, past_365_days
    • Custom rolling window: past_N_days where N is any positive integer (for example, past_45_days, past_120_days)
    • Year-to-date: year_to_date
    • Black Friday / Cyber Monday by year: bfcm2024, bfcm2025 (the four days from Black Friday through Cyber Monday)
    • ISO: 2025-03-15 or 2025-03-15T10:30:00Z (timezone offsets supported, ISO 8601)
  • Examples:

UNTIL (optional)

  • Purpose: Defines the end of the time window for the query. When omitted, the query runs up to the current time.
  • Accepted forms: Same as SINCE (relative offsets, keywords, rolling windows, ISO timestamps).
  • Examples:
The UNTIL clause is currently parsed but not yet applied during metric execution. End date filtering will be supported in a future release.

DURING

  • Purpose: A single-token shortcut for common time ranges. Use DURING instead of SINCE/UNTIL when one of the named ranges fits your query.
  • Accepted forms:
    • Calendar keywords: today, yesterday, this_week, last_week, this_month, last_month, this_quarter, last_quarter, this_year, last_year
    • Rolling windows: past_7_days, past_30_days, past_90_days, past_N_days (any positive integer N)
    • Year-to-date: year_to_date
    • Black Friday / Cyber Monday by year: bfcm2024, bfcm2025
  • Mutually exclusive: A query uses either SINCE (with optional UNTIL) or DURING — never both.
  • Examples:

TIMESERIES (optional)

  • Purpose: Groups results by time intervals with automatic backfilling for missing periods.
  • Granularities:
    • Standard: day, week, month, quarter, year, hour, minute
    • Cyclical: day_of_week, hour_of_day, week_of_year, month_of_year
  • Examples:
When using TIMESERIES, the query results include a time dimension that shows values for each period within the SINCE range. Missing periods are automatically backfilled with zero values, making it ideal for charting and trend analysis.

SEGMENT BY (optional)

  • Purpose: Adds an additional segmentation dimension applied to each group.
  • Examples:

COMPARE TO (optional)

  • Purpose: Repeats the query over a shifted time window and returns both result sets so you can compare two periods (for example, this month vs. last month). The shifted window has the same length as the primary window.
  • Syntax: COMPARE TO <target>
  • Targets:
    • previous_period — shifts back by the exact length of the primary window (a 7-day window compares against the prior 7 days).
    • previous_month — shifts back by one calendar month.
    • previous_year — shifts back by one calendar year.
    • previous_year_match_day_of_week — shifts back 364 days so the same days of the week align (useful for retail where day-of-week effects matter).
  • Combine with WITH PERCENT_CHANGE to surface the percentage delta between the two periods.
  • Examples:

WITH (optional)

  • Purpose: Adds calculated rollups and derived series to the result without changing the base aggregation. Combine modifiers with commas in any order; each modifier may appear at most once.
  • Syntax: WITH <modifier> [, <modifier>]*
  • Modifiers:
    • TOTALS — adds a grand total row across every group.
    • GROUP_TOTALS — adds subtotal rows when a query has both GROUP BY and SEGMENT BY (or TIMESERIES). One subtotal is emitted per primary group.
    • PERCENT_CHANGE — adds a percent-change column. Requires COMPARE TO.
    • CUMULATIVE_VALUES — adds a running total series. Only valid for additive expressions (SUM, COUNT, and add/subtract combinations of them). Non-additive expressions such as AVG, MIN, MAX, COUNT_DISTINCT, and ratios are rejected.
  • Examples:

VISUALIZE (optional)

  • Purpose: Specifies how to render the query results graphically.
  • Syntax: VISUALIZE <target> [TYPE <type>] [BY <field>] [SERIES <field>] [BUCKETS <n>, <n>, ...] [LIMIT <n>] [MAX <number>]
  • Target: The metric or expression to visualize (e.g., SUM(total_sales) or a field name).
  • Options: Each option may appear at most once and they can be written in any order.
    • TYPE <type> — chart type. One of table, bar, line, donut, horizontal_bar, stacked_bar, area, card, geo_bar. Use geo_bar for ranked geographic distributions; country codes in geo_country are rendered as full country names, and bars are sorted in descending order of the metric.
    • BY <field> — primary axis or grouping field for the chart (for example, BY position).
    • SERIES <field> — splits the data into separate series by the given field (for example, SERIES geo_country).
    • BUCKETS <n>, <n>, ... — numeric bucket boundaries for histogram-style charts.
    • LIMIT <n> — caps the number of categories or series rendered.
    • MAX <number> — caps the number of data points displayed.
  • Examples:
The VISUALIZE clause is particularly useful when combined with TIMESERIES for creating time-series charts, or with GROUP BY for creating bar charts and donut charts showing distribution across dimensions. Identifiers and paths
  • Identifiers: letters, digits, underscores; start with a letter or underscore.
  • Paths: dot notation (for example, product_attributes.color).
Literals
  • Strings: ‘value’ or “value”
  • Numbers: 10, 10.5
  • Booleans: TRUE, FALSE

Arithmetic expressions

LayersQL supports arithmetic operations in both SHOW and WHERE clauses, enabling you to create calculated metrics and complex filtering conditions.

Supported operators

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulo: %
  • Unary minus: - (negation)

Operator precedence

Arithmetic expressions follow standard mathematical precedence: multiplication, division, and modulo are evaluated before addition and subtraction. Use parentheses to override the default precedence.

Examples

Combine multiple aggregations:
Calculate a weighted score:
Use parentheses to control precedence:
Calculate profit per view:
Arithmetic in WHERE clause:

Aliasing arithmetic expressions

Use the AS keyword to give your calculated expressions meaningful names:

Examples

Group by attributes with a rolling window
Filter by geography and marketing channel
Calculate weighted metrics with segmentation