AMQL Docs
AQL ReferenceFunctionsTime-based functions

trailing_period

A function to calculate a metric over a specific number of date periods up to the current period.

Definition

Calculates a metric over a specific number of date periods up to the current period (in the context of the current row, not the current month/period on your calendar). For example, you can apply this computation to determine the total orders you have in the last 3 months up until the current month, also known as Trailing 3 Months Metric.

Syntax

trailing_period(metric, date_dimension, period)
Examples
// Return total orders in the last 3 months using interval
trailing_period(count(orders.id), orders.created_at, interval(3 months))

// Return total orders in the last 3 periods using number (requires time grain)
trailing_period(count(orders.id), orders.created_at, 3)

// with pipe
count(orders.id) | trailing_period(orders.created_at, interval(3 months))
count(orders.id) | trailing_period(orders.created_at, 3)

Input

  • metric (required): The metric on which you want to apply the trailing_period() function
  • date_dimension (required): The date dimension that is used to determine the periods
  • period (required): Either:
    • An interval literal that specifies the number of periods to calculate (includes the current period). E.g. interval(3 months), interval(1 year).
    • A number that specifies how many periods to include based on the current time grain. E.g. 3 for 3 months when grouped by month, or 3 days when grouped by day. Note: When using a number, the date dimension must have a time grain applied (e.g., | month(), | day()).

Output

A metric that calculates input metric over a specific number of date periods up to the current period.

Number-based Period Example

When using a number instead of an interval, the period is determined by the time grain applied to the date dimension:

explore {
  dimensions {
    order_items.created_at | month()  // Time grain is month
  }
  measures {
    this_month_count: count(order_items.order_id),
    last_3_months_count: count(order_items.order_id) | trailing_period(order_items.created_at, 3)  // Last 3 months
  }
}

In this example:

  • Since the dimension is grouped by month (| month()), the number 3 means "last 3 months"
  • If the dimension was grouped by day (| day()), the number 3 would mean "last 3 days"
  • If no time grain is specified, an error will be raised

Sample Usages

Trailing periods offer a valuable tool for analyzing how metrics perform over a rolling or moving time frame. Imagine that 3 months is a good time frame to see how a certain metric performs in your business. By using trailing_period you can see how a metric historically performed for the past 3 months at any given point in time. This is especially useful for metrics that represent a ratio or percentage, such as the Percentage of Cancelled Orders, Conversion Rate, etc.

For example, here's how you can use trailing_period to calculate how the Past 3 Months Percentage of Cancelled Orders changes over time:

Past 3 Months Percentage of Cancelled Orders
metric cancelled_orders_pct = 
  count_if(orders, orders.status == 'cancelled') * 100.0 / count(orders.id);
explore {
  dimensions {
    orders.created_at | month()
  }
  measures {
    cancelled_orders_pct: cancelled_orders_pct,
    _past_3_months_cancelled_rate: 
      cancelled_orders_pct | trailing_period(orders.created_at, interval(3 months))
  }
}
orders->created_at->monthcancelled_orders_pct_past_3_months_cancelled_rate
2020-02-01 00:00:00.00000050.000000000000000050.0000000000000000
2020-03-01 00:00:00.00000033.333333333333333341.6666666666666667
2020-04-01 00:00:00.00000014.285714285714285731.5789473684210526
2020-05-01 00:00:00.0000000.0000000000000000000018.7500000000000000
2020-06-01 00:00:00.00000040.000000000000000020.0000000000000000
2020-07-01 00:00:00.00000018.181818181818181821.0526315789473684
2020-08-01 00:00:00.00000025.000000000000000025.0000000000000000
2020-09-01 00:00:00.00000014.285714285714285718.1818181818181818
2020-10-01 00:00:00.0000009.090909090909090913.6363636363636364
2020-11-01 00:00:00.00000024.000000000000000018.6046511627906977
2020-12-01 00:00:00.00000025.000000000000000021.4285714285714286
2021-01-01 00:00:00.00000032.142857142857142927.3972602739726027
2021-02-01 00:00:00.00000020.833333333333333326.3888888888888889
2021-03-01 00:00:00.0000009.523809523809523819.1489361702127660
2021-04-01 00:00:00.00000030.769230769230769220.0000000000000000
2021-05-01 00:00:00.00000012.500000000000000016.7883211678832117
2021-06-01 00:00:00.00000031.111111111111111123.5714285714285714
2021-07-01 00:00:00.00000014.285714285714285718.4713375796178344
2021-08-01 00:00:00.00000035.593220338983050826.8750000000000000
2021-09-01 00:00:00.00000016.000000000000000023.5714285714285714
2021-10-01 00:00:00.0000006.666666666666666723.6842105263157895
2021-11-01 00:00:00.00000036.666666666666666720.0000000000000000
2021-12-01 00:00:00.00000030.000000000000000025.4545454545454545
2022-01-01 00:00:00.00000024.137931034482758628.9855072463768116
2022-02-01 00:00:00.00000017.307692307692307723.7500000000000000
2022-03-01 00:00:00.00000022.222222222222222221.3872832369942197
2022-04-01 00:00:00.00000023.913043478260869621.7391304347826087
2022-05-01 00:00:00.00000034.883720930232558127.3858921161825726
2022-06-01 00:00:00.00000030.434782608695652229.6296296296296296
2022-07-01 00:00:00.00000021.428571428571428628.6231884057971014
2022-08-01 00:00:00.00000027.272727272727272726.2172284644194757
2022-09-01 00:00:00.00000024.271844660194174824.1007194244604317
2022-10-01 00:00:00.00000020.388349514563106823.6749116607773852
2022-11-01 00:00:00.00000019.708029197080292021.2827988338192420
2022-12-01 00:00:00.00000026.241134751773049622.3097112860892388
2023-01-01 00:00:00.00000024.561403508771929823.6080178173719376
2023-02-01 00:00:00.00000018.633540372670807523.0443974630021142
2023-03-01 00:00:00.00000023.684210526315789522.0588235294117647
Open in playground ↗

Note that, this is not the same as the average of the percentage of cancelled orders over the last 3 months, but rather the same percentage metric calculated on a different time frame. In this case, when Percentage of Cancelled Orders is defined as:

Cancelled % = Count Cancelled Orders / Count Orders

Then the Past 3 Months Percentage of Cancelled Orders is calculated as:

Past 3 Months Cancelled % = Count Cancelled Orders in the last 3 months / Count Orders in the last 3 months

Frequently Asked Questions

What happens if the visualization date grain is finer than the period specified in trailing_period?

In that case, trailing period will calculate the metric over this time frame:

Current Period - N Periods < Date <= Current Period

Examples:

  • Trailing 3 Months Metric displayed daily.
  • Trailing 1 Year Metric displayed monthly.

What happens if the visualization date grain is coarser than the period specified in trailing_period or if the visualization has no date dimension at all?

In that case, trailing_period will calculate the metric over the last N periods, where N is the number of periods specified in trailing_period.

For example:

  • For a Trailing 3 Months Metric displayed yearly, the metric for 2022 will be calculated from October 1st, 2019, to December 31st, 2022 (assuming the last record in 2022 is in December 2022).

  • For a Trailing 3 Months Metric displayed as a KPI Metric (a single number), the metric will be calculated from October 1st, 2024, to December 31st, 2024 (assuming the last date in the dataset is in December 2022).

See also

On this page