anfra SDKControls
Date drill
Switch the date grain of a query without rewriting it
A date drill is a control that changes the grain of a date dimension, for example from month to week. Like a filter, it affects only the queries it is mapped to.
const grain = app.createDateDrill('grain', { default: 'month' }) // label is optional
app.mapControl(grain, trend, { field: 'orders.created_at' })
grain.setGrain('week')
app.execute()setGrain accepts 'year', 'quarter', 'month', 'week', 'day', 'hour' and 'minute'.
How it finds the dimension
anfra finds every dimension of the query that is built directly on the mapped field and redraws it at the chosen grain. Three rules follow:
- Map to the field reference, such as
orders.created_at, never to the dimension's alias. A field that no dimension is built on fails the query with aQueryError. - Alias the dimension. It can be the raw field (
period: orders.created_at) or already truncated (period: date_trunc(orders.created_at, "month")orperiod: orders.created_at | month()). The drill overrides any of them. A computed dimension that merely uses the field is left alone. - Name the alias for any grain. The row key stays the alias whatever the grain, so use a grain-neutral name such as
period, notmonth.
const trend = app.createQuery('trend', {
dataset: 'sales',
aql: `explore {
dimensions { period: orders.created_at }
measures { revenue: revenue }
}`,
})A cross-filter from a drilled query uses the drilled grain: selecting a week filters the target by that week.
Related
- Controls: mapping a control to queries.
- Queries: aliasing dimensions.
- Example app: a grain switch on a trend line.