Skip to content

Commit

Permalink
clariy multihop depth (#4973)
Browse files Browse the repository at this point in the history
this pr clarifies multi-hop depth and whether more than 3 joins are
supported in mf.

stemmed from users asking and this slack thread:
https://dbt-labs.slack.com/archives/C03KHQRQUBX/p1708571327378159?thread_ts=1708570486.187929&cid=C03KHQRQUBX
  • Loading branch information
mirnawong1 authored Mar 13, 2024
2 parents 0d1d5ee + 745a9fe commit f7de7ab
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions website/docs/docs/build/join-logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ MetricFlow will use `user_id` as the join key to join two semantic models, `tran

Note that the `average_purchase_price` measure is defined in the `transactions` semantic model, where `user_id` is a foreign entity. However, the `user_signup` semantic model has `user_id` as a primary entity.

Since this is a foreign-to-primary relationship, a left join is implemented where the `transactions` semantic model joins the `user_signup` semantic model, since the `average_purchase_price` measure is defined in the `transactions` semantic model.
Since this is a foreign-to-primary relationship, a left join is implemented where the `transactions` semantic model joins the `user_signup` semantic model since the `average_purchase_price` measure is defined in the `transactions` semantic model.

When querying dimensions from different semantic models using the CLI, a double underscore (or dunder) is added to the dimension name after the joining entity. In the CLI query shown below, `user_id__type` is included as a `dimension`.

Expand All @@ -84,17 +84,19 @@ mf query --metrics average_purchase_price --dimensions metric_time,user_id__type

## Multi-hop joins

:::info
MetricFlow can join three tables at most, supporting multi-hop joins with a limit of two hops.
:::
MetricFlow allows users to join measures and dimensions across a graph of entities by moving from one table to another within a graph. This is referred to as "multi-hop join".

MetricFlow can join up to three tables, supporting multi-hop joins with a limit of two hops. This does the following:
- Enables complex data analysis without ambiguous paths.
- Supports navigating through data models, like moving from `orders` to `customers` to `country` tables.

MetricFlow allows users to join measures and dimensions across a graph of entities, which we refer to as a 'multi-hop join.' This is because users can move from one table to another like a 'hop' within a graph.
While direct three-hop paths are limited to prevent confusion from multiple routes to the same data, MetricFlow does allow joining more than three tables if the joins don’t exceed two hops to reach a dimension.

Here's an example schema for reference:
For example, if you have two models, `country` and `region`, where customers are linked to countries, which in turn are linked to regions, you can join all of them in a single SQL query and can dissect `orders` by `customer__country_country_name` but not by `customer__country__region_name`.

![Multi-Hop-Join](/img/docs/building-a-dbt-project/multihop-diagram.png)
![Multi-Hop-Join](/img/docs/building-a-dbt-project/multihop-diagram.png "Example schema for reference")

Notice how this schema can be translated into the three MetricFlow semantic models below to create the metric 'Average purchase price by country' using the `purchase_price` measure from the sales table and the `country_name` dimension from the `country_dim` table.
Notice how the schema can be translated into the following three MetricFlow semantic models to create the metric 'Average purchase price by country' using the `purchase_price` measure from the sales table and the `country_name` dimension from the `country_dim` table.

```yaml
semantic_models:
Expand Down Expand Up @@ -124,6 +126,8 @@ semantic_models:
- name: signup_date
type: time
- name: country_dim
- name: country
entities:
- name: country_id
type: primary
Expand Down

0 comments on commit f7de7ab

Please sign in to comment.