anfra docs
Semantic layer

Metrics

Reusable, named calculations agents and reports can query

A metric is a named calculation defined once on a dataset, in AQL, so every agent and report that asks for "revenue" resolves against the same definition.

Defining a metric

datasets/sales.dataset.aml
Dataset sales {
  // models and relationships

  metric revenue {
    label: 'Revenue'
    type: 'number'
    description: 'Sum of quantity × list price across all order items.'
    format: '[$$]#,###0.00'
    definition: @aql sum(order_items.quantity * products.price);;
  }

  metric order_count {
    label: 'Orders'
    type: 'number'
    definition: @aql count(orders.id);;
  }

  metric average_order_value {
    label: 'Average Order Value'
    type: 'number'
    format: '[$$]#,###0.00'
    definition: @aql revenue / order_count;;
  }
}
  • definition is an AQL expression. It can aggregate fields from any model in the dataset, or combine other metrics by name, as average_order_value does.
  • description tells readers and agents what the number includes.
  • format sets how values are displayed.

Metrics vs. measures

Measures live on a single data model and can only aggregate that model's own fields. Metrics live on a dataset and can combine fields across every model the dataset includes — use a measure for single-model aggregations, and a metric once you need to span models or build on other metrics.

Querying by dimensions

A metric has no grouping until it's combined with one or more dimensions — for example, revenue grouped by orders.created_at at a monthly grain gives you revenue by month.

anfra query -d sales -a 'explore {
  dimensions { month: orders.created_at | month() }
  measures { revenue: revenue }
}'

Add -g to print the generated SQL instead of running it. See anfra query.

Next steps

On this page