Skip to main content

dbt-metric-utils

This tool allows you to query metrics from your dbt semantic model directly from a dbt model through the dbt_metric_utils_materialize macro. One way to look at it is that it revives the metric.calculate() macro from dbt <=v1.5. By having access to this macro, the dbt semantic layer becomes more useful for dbt-core users. You still don't have all the goodness of dbt-cloud semantic layer but it does allow you to get started with connecting your users and BI tools to aggregation tables/views that are directly querying your metrics.

[!TIP] Check out some examples queries here

[!TIP] Browse the dbt docs pages for the example project here

Installation instructions

This project is a Python package that wraps around dbt in the most transparant way I could find. Try it out through the following steps:

  1. Install dbt-metric-utils from Pypi in your project (e.g. pip install dbt-metric-utils)
  2. Run dbt-metric-utils init or dbtmu init. This will install the macro into your project and will make sure that any dbt CLI calls are intercepted and processed in the correct way (check below for explanation)
  3. Introduce a dbt model that calls the dbt_metric_utils_materialize macro.
  4. Continue using dbt as you're used to.

How it works

Any dbt command that doesn't require dbt to compile your project is simply passed directly to dbt (Mode A in the diagram). A dbt invocation that does require compilation (e.g. compile, run, test , etc) is intercepted by the package.

After intercepting we run through the following sequence of steps

  1. Call dbt parse . This will build a partially filled manifest.json from which we can extract all the models, their dependencies, and the raw SQL queries.
  2. Extract all models that contain a dbt_metric_utils_materialize invocation.
  3. Run mf query --explain commands for all the dbt_metric_utils_materialize invocations.
  4. Inject the generated queries by Metricflow as dbt variables in the actual dbt command. If the user ran dbt run , we actually trigger dbt run --vars {<macro_invocation_signature>: <query>}

The passed variables will be a mapping from dbt_metric_utils_materialize invocation signature (e.g. metric=['m1'],dimensions='[dim1']... ) to the generated metric query. The dbt_metric_utils_materialize macro will find that variable at compile time and return it as the macro result.

Along this sequence of steps, we also ensure that the dependency graph in manifest.json is updated correctly. Dbt itself only detects dependencies based on ref and source , not on macros that are external to it.

Release files for dbt-metric-utils 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbt-metric-utils 0.1.1
File Size Uploaded
dbt_metric_utils-0.1.1.tar.gz 12.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for dbt-metric-utils 0.1.1
File Interpreter ABI Platform
dbt_metric_utils-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 MB

Release files / dbt_metric_utils-0.1.1.tar.gz

Download URL dbt_metric_utils-0.1.1.tar.gz
Size 12.5 MB
Tags Source
SHA-256 checksum
How to use checksums
3f2d18d4f7f6eb27653509e6ccecb7ee164e1e75e36eddd51ed64c053a551fda
BLAKE2b-256 checksum
How to use checksums
ca19cebe756cfed888fc630879478bfcc6149eca3cc49ac33df0de66b8b75cca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.2

Release files / dbt_metric_utils-0.1.1-py3-none-any.whl

Download URL dbt_metric_utils-0.1.1-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e29701164cc47d038d2999a4359161509806495138ec1682b548abb80ecd46e2
BLAKE2b-256 checksum
How to use checksums
2ffe6e1098f07bf6309c2073b20b8e4c23ba0fbeabad74e4a85b5b9c93db40cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page