Skip to content

Commit

Permalink
Update metricflow-commands.md (#5684)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirnawong1 authored Jun 25, 2024
1 parent 73880c9 commit cbbda9d
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions website/docs/docs/build/metricflow-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ MetricFlow allows you to define and query metrics in your dbt project in the [db

MetricFlow is compatible with Python versions 3.8, 3.9, 3.10, and 3.11.


## MetricFlow

MetricFlow is a dbt package that allows you to define and query metrics in your dbt project. You can use MetricFlow to query metrics in your dbt project in the dbt Cloud CLI, dbt Cloud IDE, or dbt Core.
Expand Down Expand Up @@ -274,12 +273,16 @@ mf query --metrics <metric_name> --group-by <dimension_name> # In dbt Core

Options:

--metrics SEQUENCE Metrics to query for: syntax is --metrics bookings
or for multiple metrics --metrics bookings, messages.
--metrics SEQUENCE Syntax to query single metrics: --metrics metric_name
For example, --metrics bookings
To query multiple metrics, use --metrics followed by the metric names, separated by commas without spaces.
For example, --metrics bookings,messages

--group-by SEQUENCE Dimensions and/or entities to group by: syntax is
--group-by ds or for multiple group bys --group-by
ds, org.
--group-by SEQUENCE Syntax to group by single dimension/entity: --group-by dimension_name
For example, --group-by ds
For multiple dimensions/entities, use --group-by followed by the dimension/entity names, separated by commas without spaces.
For example, --group-by ds,org


--end-time TEXT Optional iso8601 timestamp to constraint the end
time of the data (inclusive).
Expand All @@ -289,17 +292,20 @@ Options:
time of the data (inclusive)
*Not available in dbt Cloud yet

--where TEXT SQL-like where statement provided as a string. For
example: --where "revenue > 100". To add a dimension filter to
a where filter, you have to indicate that the filter item is part of your model.
--where TEXT SQL-like where statement provided as a string and wrapped in quotes: --where "condition_statement"
For example, to query a single statement: --where "revenue > 100"
To query multiple statements: --where "revenue > 100 and user_count < 1000"
To add a dimension filter to a where filter, ensure the filter item is part of your model.
Refer to the [FAQ](#faqs) for more info on how to do this using a template wrapper.

--limit TEXT Limit the number of rows out using an int or leave
blank for no limit. For example: --limit 100

--order-by SEQUENCE Metrics or group bys to order by ("-" prefix for
DESC). For example: --order-by -ds or --order-by
ds,-revenue
--order-by SEQUENCE Specify metrics, dimension, or group bys to order by.
Add the `-` prefix to sort query in descending (DESC) order.
Leave blank for ascending (ASC) order.
For example, to sort metric_time in DESC order: --order-by -metric_time
To sort metric_time in ASC order and revenue in DESC order: --order-by metric_time,-revenue

--csv FILENAME Provide filepath for data frame output to csv

Expand Down Expand Up @@ -328,13 +334,13 @@ The following tabs present various types of query examples that you can use to q
<TabItem value="eg1" label="Metrics">
Use the example to query metrics by dimension and return the `order_total` metric by `metric_time.`
Use the example to query multiple metrics by dimension and return the `order_total` and `users_active` metrics by `metric_time.`
**Query**
```bash
dbt sl query --metrics order_total --group-by metric_time # In dbt Cloud
dbt sl query --metrics order_total,users_active --group-by metric_time # In dbt Cloud

mf query --metrics order_total --group-by metric_time # In dbt Core
mf query --metrics order_total,users_active --group-by metric_time # In dbt Core
```
**Result**
Expand All @@ -357,9 +363,9 @@ You can include multiple dimensions in a query. For example, you can group by th
**Query**
```bash
dbt sl query --metrics order_total --group-by metric_time, is_food_order # In dbt Cloud
dbt sl query --metrics order_total --group-by metric_time,is_food_order # In dbt Cloud
mf query --metrics order_total --group-by metric_time, is_food_order # In dbt Core
mf query --metrics order_total --group-by metric_time,is_food_order # In dbt Core
```
**Result**
Expand All @@ -382,7 +388,7 @@ mf query --metrics order_total --group-by metric_time, is_food_order # In dbt Co
<TabItem value="eg3" label="Order/limit">
You can add order and limit functions to filter and present the data in a readable format. The following query limits the data set to 10 records and orders them by `metric_time`, descending.
You can add order and limit functions to filter and present the data in a readable format. The following query limits the data set to 10 records and orders them by `metric_time`, descending. Note that using the `-` prefix will sort the query in descending order. Without the `-` prefix sorts the query in ascending order.
**Query**
```bash
Expand Down Expand Up @@ -410,15 +416,15 @@ mf query --metrics order_total --group-by metric_time,is_food_order --limit 10 -
<TabItem value="eg4" label="where clause">
You can further filter the data set by adding a `where` clause to your query.
You can further filter the data set by adding a `where` clause to your query. The following example shows you how to query the `order_total` metric, grouped by `metric_time` with multiple where statements (orders that are food orders and orders from the week starting on or after Feb 1st, 2024):
**Query**
```bash
# In dbt Cloud
dbt sl query --metrics order_total --group-by metric_time --where "{{ Dimension('order_id__is_food_order') }} = True"
dbt sl query --metrics order_total --group-by metric_time --where "{{ Dimension('order_id__is_food_order') }} = True and metric_time__week >= '2024-02-01'"
# In dbt Core
mf query --metrics order_total --group-by metric_time --where "{{ Dimension('order_id__is_food_order') }} = True"
mf query --metrics order_total --group-by metric_time --where "{{ Dimension('order_id__is_food_order') }} = True and metric_time__week >= '2024-02-01'"
```
**Result**
Expand Down Expand Up @@ -485,7 +491,7 @@ mf query --saved-query <name> # In dbt Core
For example, if you use dbt Cloud and have a saved query named `new_customer_orders`, you would run `dbt sl query --saved-query new_customer_orders`.
:::info A note on querying saved queries
When querying [saved queries](/docs/build/saved-queries),you can use parameters such as `where`, `limit`, `order`, `compile`, and so on. However, keep in mind that you can't access `metric` or `group_by` parameters in this context. This is because they are predetermined and fixed parameters for saved queries, and you can't change them at query time. If you would like to query more metrics or dimensions, you can build the query using the standard format.
When querying [saved queries](/docs/build/saved-queries), you can use parameters such as `where`, `limit`, `order`, `compile`, and so on. However, keep in mind that you can't access `metric` or `group_by` parameters in this context. This is because they are predetermined and fixed parameters for saved queries, and you can't change them at query time. If you would like to query more metrics or dimensions, you can build the query using the standard format.
:::
</TabItem>
Expand Down Expand Up @@ -622,3 +628,27 @@ The default `limit` for query issues from the dbt Cloud CLI is 100 rows. We set
However, you can change this limit if needed by setting the `--limit` option in your query. For example, to return 1000 rows, you can run `dbt sl list metrics --limit 1000`.
</detailsToggle>
<detailsToggle alt_header="How can I query multiple metrics, group bys, or where statements?">
To query multiple metrics, group bys, or where statements in your command, follow this guidance:
- To query multiple metrics and group bys, use the `--metrics` or `--group-by` syntax followed by the metric or dimension/entity names, separated by commas without spaces:
- Multiple metrics example: `dbt sl query --metrics accounts_active,users_active`
- Multiple dimension/entity example: `dbt sl query --metrics accounts_active,users_active --group-by metric_time__week,accounts__plan_tier`
- To query multiple where statements, use the `--where` syntax and wrap the statement in quotes:
- Multiple where statement example: `dbt sl query --metrics accounts_active,users_active --group-by metric_time__week,accounts__plan_tier --where "metric_time__week >= '2024-02-01' and accounts__plan_tier = 'coco'"`
</detailsToggle>
<detailsToggle alt_header="How can I sort my query in ascending or descending order?">
When you query metrics, use `--order-by` to specify metrics or groupings to order by. The `order_by` option applies to metrics, dimensions, and group bys.
Add the `-` prefix to sort your query in descending (DESC) order. Leave blank for ascending (ASC) order:
- For example, to query a metric and sort `metric_time` in descending order, run `dbt sl query --metrics order_total --group-by metric_time --order-by -metric_time`. Note that the `-` prefix in `-metric_time` sorts the query in descending order.
- To query a metric and sort `metric_time` in ascending order and `revenue` in descending order, run `dbt sl query --metrics order_total --order-by metric_time,-revenue`. Note that `metric_time` without a prefix is sorted in ascending order and `-revenue` with a `-` prefix sorts the query in descending order.
</detailsToggle>

0 comments on commit cbbda9d

Please sign in to comment.