Anfra docs
Concepts

Data Models

Map a table or SQL query into dimensions and measures

A data model maps a database table (or a SQL query) into a structure Anfra can reason about — its dimensions (raw, non-aggregated fields) and measures (aggregations scoped to this one model).

Defining a data model

Model orders {
  type: 'table'
  data_source_name: 'warehouse'
  table_name: 'public.orders'

  dimension id {
    definition: @sql {{ #SOURCE.id }};;
    type: 'number'
  }

  dimension status {
    definition: @sql {{ #SOURCE.status }};;
    type: 'text'
  }

  measure order_count {
    definition: @aql count(orders.id) ;;
  }
}

Dimensions vs. measures

  • Dimensions — raw values used for filtering and grouping, e.g. status, created_at, customer_id.
  • Measures — aggregations (count, sum, avg, …) computed within this single model.

Measures work well for aggregations that only need one model. Once you need to aggregate across multiple joined models, define a metric on a dataset instead.

Relationships

A model can declare relationships to other models, so datasets know how to join them:

relationship: many_to_one(customer_id) references customers(id)

When to create a new data model

Create one data model per table or view you want to expose to the semantic layer. Keep models close to the underlying schema — leave cross-model logic to datasets.

Next steps

On this page