Dimensions
Non-aggregated fields for grouping and filtering metrics
A dimension is a raw, non-aggregated field — a category, date, or other attribute — used to group and filter metrics. Where a metric answers "how much?", a dimension answers "broken down by what?".
Defining a dimension
Dimensions are usually defined on a data model, close to the column they come from:
Model orders {
// type, data source and table
dimension created_at {
label: 'Created At'
type: 'datetime'
definition: @sql {{ #SOURCE.created_at }};;
}
dimension status {
label: 'Status'
type: 'text'
definition: @sql {{ #SOURCE.status }};;
}
}A dataset can also add dimensions that combine fields across its included
models. Set model to the model the dimension belongs to, and write the
definition in AQL so it can reach the other models' fields:
Dataset sales {
// models and relationships
dimension line_value {
model: order_items
label: 'Line Value'
type: 'number'
definition: @aql order_items.quantity * products.price;;
}
}Queries use it like any field of that model: order_items.line_value.
Common types
dateanddatetime— supports querying at different grains (day, week, month, …) without redefining the dimension, as inorders.created_at | month().text— a discrete attribute likestatusorplan, used for filtering and grouping.number— numeric values such as IDs, quantities or prices.truefalse— boolean flags.
Putting it together
Once a dataset has metrics and dimensions defined, anyone can query
revenue grouped by orders.created_at at a month grain, filtered to one
orders.status — without writing SQL or repeating the underlying joins:
anfra query -d sales -a 'explore {
dimensions { month: orders.created_at | month() }
measures { revenue: revenue }
filters { orders.status == "delivered" }
}'