Skip to main content

Time Range Syntax

Rill provides a flexible, expressive syntax for specifying time ranges. This reference covers every aspect of the syntax with detailed examples.

Grammar Overview

expression     = interval [as_of]* [by_clause] [tz_clause] [offset_clause]

interval = shorthand | period_to_date | start_end | ordinal | iso_interval

shorthand = duration
period_to_date = grain "TD"
start_end = point_in_time "to" point_in_time
ordinal = grain number ["of" grain number]*
iso_interval = iso_timestamp ["to" iso_timestamp]

point_in_time = (offset | reference) [snap]*
offset = prefix duration
reference = "ref" | "now" | "watermark" | "latest" | "earliest"
snap = "/" grain
duration = (number grain)+
prefix = "+" | "-"

as_of = "as" "of" point_in_time
by_clause = "by" grain
tz_clause = "tz" timezone
offset_clause = "offset" (prefix number "P" | prefix duration)

grain = "s" | "m" | "h" | "D" | "W" | "M" | "Q" | "Y"

Time Grains

GrainAliasesDescriptionExamples
sSSecond30s, sTD
mMinute15m, mTD
hHHour24h, hTD, HTD
dDDay7D, 7d, DTD
wWWeek2W, WTD
MMonth3M, MTD
qQQuarter1Q, QTD
yYYear1Y, YTD
Month vs Minute

M is always month. m is always minute. This is the only case-sensitive distinction.


Reference Points

A reference point is the time that an expression is evaluated relative to. Expressions like 7D implicitly use ref, which resolves to now by default. You can override this with the as of modifier — for example, 7D as of watermark evaluates the range relative to the data completeness boundary instead. Choosing the right reference point determines whether your range follows the clock, the latest data, or a data completeness boundary.

KeywordDescriptionUse Case
refContextual reference timeInternal use, modified by as of
nowCurrent wallclock timeReal-time dashboards
watermarkData completeness markerProduction dashboards with ETL delays
latestMost recent data timestampShow data up to (but not including) latest available
earliestOldest data timestampFull historical analysis

Reference Point Examples

Given: now = 2025-05-15T10:32:36Z, watermark = 2025-05-13T06:32:36Z, latest = 2025-05-14T06:32:36Z

ExpressionStartEnd
7D2025-05-08T10:32:36Z2025-05-15T10:32:36Z
7D as of now2025-05-08T10:32:36Z2025-05-15T10:32:36Z
7D as of watermark2025-05-06T06:32:36Z2025-05-13T06:32:36Z
7D as of now/D2025-05-08T00:00:00Z2025-05-15T00:00:00Z
7D as of watermark/H+1H2025-05-06T07:00:00Z2025-05-13T07:00:00Z

Interval Types

1. Shorthand Intervals

The simplest form: a duration that counts backward from the reference point.

Syntax: <number><grain>[<number><grain>]*

Expansion: <duration>-<duration> to ref

ShorthandEquivalent Start-EndDescription
7D-7D to refLast 7 days
4h-4h to refLast 4 hours
2W-2W to refLast 2 weeks
3M-3M to refLast 3 months
1Y-1Y to refLast 1 year

Multi-grain durations:

ExpressionDescription
3W18D23h3 weeks + 18 days + 23 hours
1Y6M1 year + 6 months
2D12h30m2 days + 12 hours + 30 minutes

Examples with reference points:

Given watermark = 2025-05-13T06:32:36Z:

ExpressionStartEnd
7D as of watermark2025-05-06T06:32:36Z2025-05-13T06:32:36Z
MTD as of watermark2025-05-01T00:00:00Z2025-05-13T06:32:36Z

2. Period-to-Date Intervals

Returns from the start of the current period to the reference point.

Syntax: <grain>TD

Expansion: <grain>TDref/<grain> to ref

SyntaxExpansionDescription
sTDref/s to refCurrent second
mTDref/m to refMinute to date
hTDref/h to refHour to date
DTDref/D to refDay to date (today so far)
WTDref/W to refWeek to date
MTDref/M to refMonth to date
QTDref/Q to refQuarter to date
YTDref/Y to refYear to date

Examples:

Given now = 2025-05-15T10:32:36Z:

ExpressionStartEnd
DTD2025-05-15T00:00:00Z2025-05-15T10:32:36Z
MTD2025-05-01T00:00:00Z2025-05-15T10:32:36Z
YTD2025-01-01T00:00:00Z2025-05-15T10:32:36Z
MTD as of watermark2025-05-01T00:00:00Z2025-05-13T06:32:36Z

3. Start-End Intervals

Explicit specification of start and end points.

Syntax: <point_in_time> to <point_in_time>

Point-in-Time Components

A point in time can be:

  • Reference: ref, now, watermark, latest, earliest
  • Offset: -4D, +1W, -2M3D
  • Snapped: now/D, watermark/W, -4D/D
  • Combined: -4D/D+2h, watermark/M-1D

Detailed Examples

Given now = 2025-05-15T10:32:36Z, watermark = 2025-05-13T06:32:36Z:

ExpressionStartEndNotes
-4d to now2025-05-11T10:32:36Z2025-05-15T10:32:36ZExact 4 days ago
-4d/d to now/d2025-05-11T00:00:00Z2025-05-15T00:00:00ZSnapped to day boundaries
-4d to now/d2025-05-11T10:32:36Z2025-05-15T00:00:00ZMixed: exact start, snapped end
watermark/D to watermark2025-05-13T00:00:00Z2025-05-13T06:32:36ZStart of watermark day to watermark
watermark/Y to watermark2025-01-01T00:00:00Z2025-05-13T06:32:36ZYear to watermark
watermark to latest2025-05-13T06:32:36Z2025-05-14T06:32:36ZBetween watermark and latest
earliest to latest(earliest data)(latest data)All data

First/Last N of a Period

ExpressionDescriptionExample Result
-2m/m-2s to -2m/mLast 2 seconds of 2 mins ago06:29:58Z to 06:30:00Z
-2m/m to -2m/m+2sFirst 2 seconds of 2 mins ago06:30:00Z to 06:30:02Z
-2h/h-2m to -2h/hLast 2 minutes of 2 hours ago03:58:00Z to 04:00:00Z
-2h/h to -2h/h+2mFirst 2 minutes of 2 hours ago04:00:00Z to 04:02:00Z
-2D/D-2h to -2D/DLast 2 hours of 2 days ago22:00:00Z to 00:00:00Z
-2D/D to -2D/D+2hFirst 2 hours of 2 days ago00:00:00Z to 02:00:00Z
-2W/W-2D to -2W/WLast 2 days of 2 weeks ago2 days before week boundary
-2W/W to -2W/W+2DFirst 2 days of 2 weeks agoFirst 2 days of that week
-2M/M-2D to -2M/MLast 2 days of 2 months agoLast 2 days of that month
-2M/M to -2M/M+2DFirst 2 days of 2 months agoFirst 2 days of that month
-2Q/Q-2M to -2Q/QLast 2 months of 2 quarters agoLast 2 months of that quarter
-2Y/Y-2M to -2Y/YLast 2 months of 2 years agoNov-Dec of that year
-2Y/Y-2Q to -2Y/YLast 2 quarters of 2 years agoQ3-Q4 of that year

4. Ordinal Intervals

Selects a specific numbered occurrence within a period.

Syntax: <grain><number> [of <grain><number>]*

Basic Ordinals

ExpressionDescription
D11st day of reference period
D1515th day of reference period
W11st week of reference period
W22nd week of reference period
M11st month (January)
M1111th month (November)
Q22nd quarter (Apr-Jun)
H1212th hour (noon)
m3030th minute
s4545th second

Chained Ordinals

ExpressionDescription
D3 of M23rd day of February
W2 of Q12nd week of Q1
D15 of M6June 15th
H2 of D42nd hour of 4th day
m15 of H215th minute of 2nd hour
s30 of m15 of H230th second of 15th minute of 2nd hour
s57 of m4 of H2 of D4Very specific moment in 4th day

Ordinals with Reference Points

Given watermark = 2025-05-13T06:32:36Z:

ExpressionStartEnd
W12025-04-28T00:00:00Z2025-05-05T00:00:00Z
W1 as of -2M2025-03-03T00:00:00Z2025-03-10T00:00:00Z
D2 as of watermark/W2025-05-13T00:00:00Z2025-05-14T00:00:00Z
D2 as of -1W as of watermark/W2025-05-06T00:00:00Z2025-05-07T00:00:00Z
W2 as of -1M as of latest/M2025-06-09T00:00:00Z2025-06-16T00:00:00Z
W2 as of -1Q as of latest/Q2025-04-07T00:00:00Z2025-04-14T00:00:00Z
W2 as of -1Y as of 20242023-01-09T00:00:00Z2023-01-16T00:00:00Z
M2 as of -2Q/Q as of watermark/Q2024-11-01T00:00:00Z2024-12-01T00:00:00Z
Q2 as of -2Y/Y as of watermark/Y2023-04-01T00:00:00Z2023-07-01T00:00:00Z

Complex Ordinal Examples

ExpressionDescription
s57 of m4 of H2 of D4 as of -1M57th sec of 4th min of 2nd hour of 4th day of last month
D3 of M11 as of 2024November 3rd, 2024
W2 as of -2M/M as of watermark/M2nd week of 2 months ago

5. ISO 8601 Intervals

Standard date/time format for absolute time specifications.

Single Timestamps (Implicit Range)

A single timestamp expands to cover its entire grain:

ExpressionStartEndGrain
20252025-01-01T00:00:00Z2026-01-01T00:00:00ZYear
2025-022025-02-01T00:00:00Z2025-03-01T00:00:00ZMonth
2025-02-202025-02-20T00:00:00Z2025-02-21T00:00:00ZDay
2025-02-20T012025-02-20T01:00:00Z2025-02-20T02:00:00ZHour
2025-02-20T01:232025-02-20T01:23:00Z2025-02-20T01:24:00ZMinute
2025-02-20T01:23:45Z2025-02-20T01:23:45Z2025-02-20T01:23:46ZSecond

Explicit Ranges

Multiple separators are supported: to, /, ,

ExpressionStartEnd
2025-02-20T01:23:45Z to 2025-07-15T02:34:50Z2025-02-20T01:23:45Z2025-07-15T02:34:50Z
2025-02-20T01:23:45Z / 2025-07-15T02:34:50Z2025-02-20T01:23:45Z2025-07-15T02:34:50Z
2025-02-20T01:23:45Z,2025-07-15T02:34:50Z2025-02-20T01:23:45Z2025-07-15T02:34:50Z

Sub-second Precision

ExpressionPrecision
2025-02-20T01:23:45.123ZMilliseconds
2025-02-20T01:23:45.123456ZMicroseconds
2025-02-20T01:23:45.123456789ZNanoseconds

Snapping (Boundary Alignment)

Snapping truncates a time to a grain boundary using the / operator.

Syntax: <point_in_time>/<grain>

Snap Behavior

ExpressionGiven now = 2025-05-15T10:32:36ZResult
now/sSnap to second2025-05-15T10:32:36Z
now/mSnap to minute2025-05-15T10:32:00Z
now/hSnap to hour2025-05-15T10:00:00Z
now/DSnap to day2025-05-15T00:00:00Z
now/WSnap to week (Monday)2025-05-12T00:00:00Z
now/MSnap to month2025-05-01T00:00:00Z
now/QSnap to quarter2025-04-01T00:00:00Z
now/YSnap to year2025-01-01T00:00:00Z

Double Snapping for ISO Week Boundaries

Weeks can span year boundaries (ISO 8601 week rules). Double snapping handles this:

ExpressionDescription
-2Y/Y/W to -1Y/Y/WFirst week of 2 years ago to first week of last year
-0Y/Y/W to ref/WFirst week of this year to current week

The second /W applies ISO week correction to align properly with week boundaries that may fall in the previous/next year.

Example:

-2Y/Y/W to -1Y/Y/W as of watermark
→ 2023-01-02T00:00:00Z to 2024-01-01T00:00:00Z

Modifiers

as of — Reference Point Override

Changes the reference point for time calculations. Multiple as of clauses are evaluated right-to-left.

Syntax: <expression> as of <point_in_time>

ExpressionDescription
7D as of now7 days ending at current time
7D as of now/D7 days ending at start of today
7D as of watermark7 days ending at watermark
7D as of watermark/D7 days ending at start of watermark day
7D as of watermark/D+1D7 days ending at start of tomorrow (includes today)
7D as of latest/D+1D7 days ending at day after latest data

Chaining as of:

ExpressionEvaluation Order
D3 as of -1M as of watermark/Mwatermark/M-1MD3
W2 as of -1Y as of 20242024-1YW2
-2D/D to ref/D as of -2D as of watermark/D+1DComplex comparison setup

by — Display Grain

Specifies the aggregation grain for display purposes.

Syntax: <expression> by <grain>

ExpressionDescription
7D by DLast 7 days, aggregate by day
MTD by hMonth to date, aggregate by hour
YTD by MYear to date, aggregate by month
1Q by WLast quarter, aggregate by week

tz — Timezone

Specifies the timezone for evaluation. Uses IANA timezone names.

Syntax: <expression> tz <timezone>

ExpressionDescription
7D tz America/New_York7 days in Eastern time
MTD tz Europe/LondonMonth to date in UK time
DTD tz Asia/TokyoToday in Japan time
W1 as of watermark tz Asia/KathmanduFirst week in Nepal time

Daylight Saving Time Examples (America/New_York):

ExpressionNotes
D3 of M11 as of 2024Nov 3, 2024 (DST ends)
D10 of M3 as of 2024Mar 10, 2024 (DST begins)
3D as of 2024-11-04Crosses DST boundary
2M as of 2024-12Includes DST transition

offset — Time Shift for Comparisons

Shifts the entire time range for comparison purposes.

Syntax: <expression> offset <shift>

Grain-based Offset

ExpressionDescription
7D offset -1WPrevious week's 7-day period
7D offset -1MSame 7 days, one month earlier
MTD offset -1YSame MTD, one year ago
7D as of latest/D+1D offset -1MLast 7 days shifted back 1 month

Previous Period Offset

ExpressionDescription
7D offset -1PThe 7 days before the current 7 days
MTD offset -1PPrevious month's MTD equivalent
2025-02-20 offset -1PThe day before (Feb 19)
2025-02 offset -1PPrevious month (January)
2025 offset -1PPrevious year (2024)

ISO Range with Previous Period:

ExpressionStartEnd
2025-02-20T01:23:45Z,2025-07-15T02:34:50Z offset -1P2024-09-28T00:12:40Z2025-02-20T01:23:45Z

Special Keywords

inf — All Time

Returns all available data from earliest to latest.

inf → earliest to latest/s+1s

Example: Given earliest = 2020-01-01T00:32:36Z, latest = 2025-05-14T06:32:36Z:

inf → 2020-01-01T00:32:36Z to 2025-05-14T06:32:37Z

Complete and Incomplete Periods

Excluding Current (Incomplete) Period

GoalExpression
Last 7 complete days7D as of watermark/D
Previous complete week1W as of watermark/W
Previous complete month1M as of watermark/M
Previous complete quarter1Q as of watermark/Q
Previous complete year1Y as of watermark/Y

Including Current (Incomplete) Period

GoalExpression
Last 7 days including today7D as of now/D+1D
Current week including today1W as of now/W+1W
Current month including today1M as of now/M+1M
Current quarter including today1Q as of now/Q+1Q
Current year including today1Y as of now/Y+1Y

Watermark Boundary Examples

Given watermark = 2025-05-12T00:00:00Z (exactly on day/week boundary):

ExpressionStartEnd
1h as of watermark/h2025-05-11T23:00:00Z2025-05-12T00:00:00Z
1h as of watermark/h+1h2025-05-12T00:00:00Z2025-05-12T01:00:00Z
1D as of watermark/D2025-05-11T00:00:00Z2025-05-12T00:00:00Z
1D as of watermark/D+1D2025-05-12T00:00:00Z2025-05-13T00:00:00Z
2D as of watermark/D2025-05-10T00:00:00Z2025-05-12T00:00:00Z
2D as of watermark/D+1D2025-05-11T00:00:00Z2025-05-13T00:00:00Z
2W as of watermark/W2025-04-28T00:00:00Z2025-05-12T00:00:00Z

Duration vs Fixed Range

A critical distinction that often causes confusion:

The Problem

Given now = 2025-09-03T14:30:00Z:

ExpressionStartEndDuration
-4d to now/d2025-08-30T14:30:00Z2025-09-03T00:00:00Z~3.4 days
4d as of now/d2025-08-30T00:00:00Z2025-09-03T00:00:00ZExactly 4 days

Why They Differ

  • -4d to now/d: Start is calculated from wallclock (2:30 PM), end is snapped to midnight
  • 4d as of now/d: Reference is snapped first, then 4 days calculated backward

Best Practices

GoalRecommendedAvoid
Exact N daysNd as of ref/D-Nd to ref/D
Exact N weeksNw as of ref/W-Nw to ref/W
Partial periods OK-Nd to ref

Week Correction (ISO 8601)

Rill follows ISO 8601 week rules where:

  • Weeks start on Monday
  • Week 1 contains the first Thursday of the year
  • Weeks can span year boundaries

First Week Variations by Boundary Day

Boundary DayWeek Starts MondayWeek Starts Sunday
MondaySame dayPrevious day
TuesdayPrevious day2 days before
Wednesday2 days before3 days before
Thursday3 days beforeNext week
FridayNext weekNext week
SaturdayNext weekNext week
SundayNext weekSame day

Example: W1 as of 2025-01-01T00:00:00Z (Wednesday)

  • Week starts Monday: 2024-12-30T00:00:00Z to 2025-01-06T00:00:00Z
  • Week starts Sunday: 2024-12-29T00:00:00Z to 2025-01-05T00:00:00Z