Comparison
>, <, >=, <=, ==, !=.
orders.created_at > @2024
AQLearn 1.5 / Concept
Source lesson: AQLearn 1.5: Filters. Tangible win: narrow your measure to a specific segment of data using explore filters {}.
Have you ever tried to answer "How many users from a target segment signed up in the last 2 years, by year"?
You hit these pain points:
'2024-01-01' rot. The dashboard silently drifts as time passes, and you have to remember to edit the query each quarter.This lesson solves it: write explore filters {} with the right operator and datetime literal so the segment is narrowed correctly, stays correct over time, and reads like the business question.
An explore filter is a boolean expression that picks which rows feed the measures. AQL evaluates the filter for every row before aggregating.
explore {
measures {
count_of_users: users | count(users.id),
}
filters {
users.sign_up_at > @2024,
}
dimensions {
time: users.sign_up_at | year(),
}
}
Read: "Count users whose sign_up_at is after January 1, 2024, broken down by sign-up year."
@AQL uses @ to express datetime literals naturally.
| Literal | Means |
|---|---|
@2024 | January 1, 2024 00:00:00 |
@2024-03 | March 1, 2024 00:00:00 |
@2024-03-12 | March 12, 2024 00:00:00 |
@(3 years ago) | Relative datetime, computed at query time |
@(1 month ago) | Same idea, rolling window |
>, <, >=, <=, ==, !=.
orders.created_at > @2024
in for "one of these values".
users.last_name in ['Kasey', 'Stacey']
like with SQL wildcards %.
users.email like '%.com'
between for inclusive bounds.
orders.discount between 0.1 and 0.3
Multiple filters separated by commas are combined with logical AND.
This lesson queries users as the source model. The filter users.sign_up_at > @2024 narrows the row set before any aggregate runs.
users.sign_up_at. Filters in this lesson narrow user rows by this datetime, by last name, and by their combination.
explore {
measures {
count_of_users: users | count(users.id),
}
filters {
users.sign_up_at < @2022,
}
dimensions {
time: users.sign_up_at | year(),
}
}
If "today" is during 2024, "before 2022" is the same as "more than 2 years ago".
filters {
users.sign_up_at < @(2 years ago),
}
Relative literals are useful for rolling dashboards that should stay correct as time passes.
filters {
users.sign_up_at < @2022,
users.last_name in ['Kasey', 'Stacey'],
}
Two filters, AND-combined. Both must be true for a row to count.
Add users.last_name as a second dimension to see the count per (year, last name) cell.
dimensions {
time: users.sign_up_at | year(),
last_name: users.last_name,
}
users.sign_up_at < @2022 and run.users.sign_up_at < @(2 years ago) and confirm the same row count.users.last_name in ['Kasey', 'Stacey'].last_name: users.last_name and run. Read result as count per year and last name.Next: relationships. How AQL traverses model-to-model paths without explicit SQL joins.