Metrics¶
A metric is a reusable, executable business calculation such as revenue, active customers, or failed-bank assets. It gives a calculation one stable name and definition so users can ask business questions without rewriting its aggregation logic.
Metrics are part of a semantic model. Their definitions live in the model's OSI YAML; Datus projects them into a separate Knowledge Base store for discovery, and the built-in Dosi engine compiles and executes metric queries.
What a metric defines¶
A metric normally includes:
- a datasource-wide unique
name; - a business
descriptionandai_context.instructions; - an aggregate, ratio, or arithmetic
expression; - the dataset and business time it uses;
- a three-level
subject_pathfor discovery; - optional display metadata such as
unitandformat; - optional reusable window behavior, such as rolling, cumulative, or period comparison.
The dataset, fields, and relationships required by a metric are defined in the same semantic-model file. This keeps the calculation and the model needed to execute it in one authoritative artifact.
Definition, discovery, and execution¶
Metric data has three distinct roles:
- OSI YAML is the definition. It is the source of truth for expressions, time behavior, descriptions, and extensions.
- The
metricsKnowledge Base store is the discovery projection. It indexes the metric name, description, subject path, model, dimensions, source YAML, and other retrieval metadata for the active datasource. - Dosi executes queries.
query_metricssends metric names, dimensions, time bounds, filters, and ordering to Dosi, which compiles and runs the query.
Moving a metric to another subject_path changes its classification, not its identity. Metric names must therefore remain unique within one datasource.
Quick start with natural language¶
The recommended user flow is to ask a business question. The main agent recognizes metric questions and automatically delegates them to ask_metrics:
The user does not need to know the metric names. AskMetrics uses the subject tree and metric descriptions to infer that this question needs bank_failure_count and failed_assets_million, discovers the available time dimension, and queries them by year.
This flow assumes the metrics already exist on the active datasource. If no existing metric can answer the question, AskMetrics reports that limitation instead of switching to an unrelated raw-SQL answer. See AskMetrics for routing, configuration, and output behavior.
Current metric YAML¶
The following excerpt is the metrics section from the bank_failures.yml file generated against Datus's sample DuckDB database. It uses the dataset and fields shown in Semantic Models. The leading indentation is intentional: this block belongs inside a semantic model and is not a standalone YAML document.
metrics:
- name: bank_failure_count
expression:
dialects:
- dialect: DUCKDB
expression: COUNT(*)
description: Number of failed banks, measured as bank failure event rows.
ai_context:
instructions: Use date as business time; group by state or a time grain when requested.
custom_extensions:
- vendor_name: DATUS
data: '{"v":"1.4","dataset":"bank_failures","time_dimension":"bank_failures.date","subject_path":["banking","bank_failures","count"],"unit":"banks"}'
- name: failed_assets_million
expression:
dialects:
- dialect: DUCKDB
expression: SUM(bank_failures.assets_million)
description: Total assets of failed banks, in millions of US dollars.
ai_context:
instructions: Use date as business time and sum assets at failure.
custom_extensions:
- vendor_name: DATUS
data: '{"v":"1.4","time_dimension":"bank_failures.date","subject_path":["banking","bank_failures","assets"],"unit":"USD million"}'
Metric expressions use the same expression.dialects[] structure as field expressions. Qualify fields with their dataset name, as in SUM(bank_failures.assets_million). An aggregate such as COUNT(*) cannot reveal its dataset from a qualified field, so its DATUS extension supplies dataset explicitly.
Do not place dataset, time_dimension, subject_path, unit, or window settings at the metric's top level. They are Datus behavior and belong in the metric's DATUS custom_extensions entry.
Metric extensions¶
The DATUS extension is an OSI custom_extensions entry whose data value is a JSON string:
custom_extensions:
- vendor_name: DATUS
data: '{"v":"1.4","time_dimension":"orders.order_date","subject_path":["sales","revenue","total"],"unit":"USD"}'
Common keys are:
| Key | Purpose |
|---|---|
dataset |
Anchors an expression when its dataset cannot be inferred, for example COUNT(*). |
time_dimension |
Selects the metric's business time field. Qualify it when names may collide. |
subject_path |
Places the metric in a three-level discovery hierarchy. |
unit, format |
Describe how results should be labeled or displayed. |
fill_nulls_with |
Defines explicit null-filling behavior. |
window |
Defines a reusable rolling, cumulative, comparison, ranking, or value window. |
semantic_modeling writes the extension version required by the installed engine. Preserve generated version values and keep only one DATUS entry on each object.
Querying metrics¶
AskMetrics is the normal user-facing query path. For integrations or advanced agent workflows, the underlying tools separate discovery from execution:
# Discover candidate definitions from the Knowledge Base.
search_metrics(
query_text="annual failed-bank count and assets",
top_n=5,
)
# How each metric is computed, what it reads, and its time grains.
get_metric(name="bank_failure_count")
# The fields of a dataset a metric reads, with their definitions.
get_dataset(name="bank_failures")
# Execute both metrics by year.
query_metrics(
metrics=["bank_failure_count", "failed_assets_million"],
dimensions=["metric_time"],
time_granularity="year",
order_by=["metric_time__year"],
)
What the discovery tools return¶
Each discovery tool answers with text, one fact per line, and leaves out what an object does not have.
list_metricslists metrics grouped under their subject path, one- name -- descriptionline each. When the page is not the whole catalog, a lastmore:line gives theoffsetto call again with.get_metricdescribes one metric:expr: how it is computed from other metrics or fields. An aggregate (COUNT(DISTINCT orders.customer_id)), a filter (base FILTER (WHERE condition)), a compose's arithmetic, a level-of-detail declaration, or a window's base.datasets: the datasets it reads.dimension_datasets: the datasets whose fields it can be grouped and filtered by, writtenname(f1, f2)when only some fields qualify.windowandcompares_across: a window metric's rule, and the dimensions a ranking or share compares across.params: each parameter asname type = default [domain] -- meaning, with[declared_on metric]when a member or a fixed value it reads declares it.required: the dimensions every query of it must group by.requires_time_axis: true: the query must also group bymetric_timeat a grain.time:metric_timeand its grains, finest first;nonefor a metric whose table has no time field.
get_datasetdescribes one dataset: a header line with its description,source,timefield and primary key, then one line per field. A column is- name; a computed field is- expression AS name; a fixed field is- fixed(base, keys) AS name, a value computed per key over rows of its own. Marks in brackets give a time field's format, a field that is not a dimension, and the dataset a fixed field reads. A lastrelationships:block lists each relationship ascardinality dataset on column=column.
query_metrics accepts these current parameters:
| Parameter | Meaning |
|---|---|
metrics |
One or more exact metric names. |
dimensions |
Fields as dataset.field, from the datasets get_metric lists under dimension_datasets (get_dataset shows their fields). metric_time selects the metric's time axis, with time_granularity for its grain. |
time_start, time_end |
Optional half-open time range: start is inclusive and end is exclusive. Use ISO dates (YYYY-MM-DD). Every metric and every fixed field applies it on its own table's time field. |
time_granularity |
day, week, month, quarter, or year. |
where |
Optional filter expression without the WHERE keyword. A condition on a selected metric (revenue > 1000) filters the result rows after aggregation, like HAVING; in a query with a window metric, a condition on group-by fields only also filters the result, so the kept rows keep the ranks and partition counts of the whole population. Every other condition filters rows before aggregation. |
context_filter |
Optional scope every number obeys, applied before aggregation in every part of the query that can read its columns; a part that cannot is listed in the result's warnings. It scopes the population a window metric ranks, counts or weighs: where="market = 'North'" shows North's rank among all markets, context_filter="market = 'North'" ranks North's rows against each other. It also scopes the rows a fixed level-of-detail value is computed over, which where does not. |
order_by |
Result columns to sort; prefix a name with - for descending order. |
limit |
Maximum rows, used only when the user asks for Top N, a preview, or another explicit row limit. |
params |
Bindings for the parameters get_metric lists under params, by name: the metric's own, its members', and those of the fixed values it reads. A list binds several values in one query and returns one column per value: {"k": [1, 7]} yields <metric>__k_1 and <metric>__k_7. Omitted parameters take their defaults. |
dry_run |
Return compiled SQL without executing the metric query; a configured warehouse dry-run provider also validates the SQL. The result carries the query's warnings, as an executed query does. |
For example, to include all of January 2024, use time_start="2024-01-01" and time_end="2024-02-01".
An executed query returns text: a result_id line, a rows line with the row count, one line for each thing to report (a warning or info from the engine, a note on IN (...) values that matched no row, a column line mapping a column named after a parameter binding to its metric), then a blank line and the rows as CSV. A long result shows its first and last rows around a ... N rows omitted line; the complete result is cached, get_query_metrics_result pages it, and it is what the final answer uses. The text leaves out the compiled SQL; a dry_run returns it.
Where a condition goes¶
Each condition of a question goes in one place:
time_start/time_end: the period. Every metric and every fixed field (a per-entity value such as a customer's order count) applies it on its own table's time field. A metric or fixed field whose table has no time field is read whole, and the result'swarningssay so; a metric whoseget_metricshowstime: nonereads such a table, so query it withouttime_start/time_end.context_filter: a scope every number obeys, such as one platform or one campaign. Every part of the query applies it where it can read the columns; a part that cannot is listed inwarnings.where: the result rows. A condition on a field never changes a fixed field's value; a condition on a selected metric filters after aggregation. A snapshot table's version day goes here.
A fixed value that must not follow the period, such as a first purchase day or a lifetime total, is read by putting the question's dates in where, on the date field of the metric's own table, instead of time_start/time_end: the dates pick the rows the metric counts and leave every fixed value over all history.
query_metrics(
metrics=["active_customers"],
dimensions=["visits.customer_first_order_day"],
where="visits.visit_date >= '2024-01-01' AND visits.visit_date < '2024-02-01'",
)
The positions differ only where a value depends on rows outside its own group: a fixed level-of-detail value, an include/exclude grain, or a window. get_metric and get_dataset show each of them. get_dataset writes a fixed field as fixed(order_count, customers.customer_id) AS name (one value per customer; fixed(revenue) is one value for the whole table); a level-of-detail metric states its declaration in expr; a window metric has a window line. query_metrics returns warnings for a condition a part could not read, so a dry run shows a misplaced condition before the query runs.
| Part | How the positions act on it |
|---|---|
| Fixed value | The period lands on its own table's time field, and context_filter narrows it where it can read the columns; where does not. A condition on the fixed field itself goes in where. A fixed field whose base takes window parameters reads the window bound in params. |
include, exclude |
Every position narrows it. |
| Period-over-period and running windows | Dates go in time_start/time_end; in where they cut the lookback and the compared value comes back empty. |
| Moving windows | time_start/time_end, context_filter and where give the same numbers. |
| Ranks | where on a grouped field keeps the value among all rows; context_filter computes it within the subset. |
For "each region's share of the whole company in January", the company total should change with January but not with the region, so January goes in time_start/time_end and the region in where.
Creating and updating metrics¶
Use semantic_modeling for normal authoring. It can create required datasets and fields, add or update metrics in the same YAML, validate the complete model, dry-run representative queries when requested, and synchronize both Knowledge Base projections.
Add a monthly revenue growth metric to the sales model, use order_date as business time, validate it, and dry-run a yearly query.
For batch authoring from a CSV containing question and sql, use the unified semantic-modeling component:
datus-agent bootstrap-kb \
--datasource <your_datasource> \
--components semantic_modeling \
--success_story path/to/success_story.csv \
--kb_update_strategy incremental \
--metrics-batch-size 5
--metrics-batch-size controls how many history rows are processed in one authoring batch. --subject_tree may provide allowed comma-separated categories; when omitted, authoring reuses existing categories or creates suitable ones.
The default check strategy does not author metrics. It only reports the current semantic-dataset and metric projection counts, so use incremental or overwrite when generation is intended.
Synchronizing the Knowledge Base¶
A successful semantic_modeling run synchronizes its target file automatically. If YAML was edited manually, reconcile all semantic-object and metric projections with:
datus-agent bootstrap-kb \
--datasource <your_datasource> \
--components semantic_model \
--kb_update_strategy sync-yaml
sync-yaml reads every model under the active datasource by default, replaces each artifact's projected rows, removes metrics no longer declared in that artifact, and prunes rows for deleted files during a directory-wide sync. It does not call an LLM or query the warehouse.
To check the number of indexed metrics without changing them:
datus-agent bootstrap-kb \
--datasource <your_datasource> \
--components metrics \
--kb_update_strategy check
Related guides¶
- AskMetrics: natural-language metric questions and capability boundaries
- Semantic Modeling: end-to-end authoring and complete generated YAML
- Semantic Models: datasets, fields, relationships, and Knowledge Base projection
- Dosi engine: model location and query behavior