-
Notifications
You must be signed in to change notification settings - Fork 977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add cumulative metrics granularity info #5688
Merged
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
acead33
add cumulative metrics info
mirnawong1 f0ec4cc
dbt sql
mirnawong1 7e76ad3
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 4ba8dfd
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 ee34960
Update website/docs/docs/build/cumulative-metrics.md
mirnawong1 f2d5ced
Update cumulative-metrics.md
mirnawong1 a34778e
updated to cumulative metrics docs
Jstein77 cb2986e
updated to cumulative metrics docs
Jstein77 5dc290d
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 f6a6b83
add updates
mirnawong1 befd1ae
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 9eec57b
add expandable and more context to example
mirnawong1 c0272c6
add context to window exmaples
mirnawong1 4313c7e
typos
mirnawong1 573403f
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 f04d77b
Update website/docs/docs/build/cumulative-metrics.md
mirnawong1 72aac6b
Update cumulative-metrics.md
mirnawong1 9b922f2
update table
mirnawong1 68be176
Update release-notes.md
mirnawong1 4ccf1f4
Update cumulative-metrics.md
mirnawong1 4227693
Update release-notes.md
mirnawong1 4aee683
Update cumulative-metrics.md
mirnawong1 37723c3
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 674e412
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 0ab1f22
Update website/docs/docs/build/cumulative-metrics.md
mirnawong1 cf6b231
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 fac61b0
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 89526aa
Merge branch 'current' into mwong-add-granularity-cumulative-metrics
mirnawong1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,21 @@ Cumulative metrics aggregate a measure over a given accumulation window. If no w | |
|
||
This metric is common for calculating things like weekly active users, or month-to-date revenue. You can use `fill_nulls_with` to [set null metric values to zero](/docs/build/fill-nulls-advanced), ensuring numeric values for every data row. The parameters, description, and type for cumulative metrics are: | ||
|
||
:::tip | ||
Note that we use the double colon (::) to indicate whether a parameter is nested within another parameter. So for example, `query_params::metrics` means the `metrics` parameter is nested under `query_params`. | ||
::: | ||
|
||
| Parameter | Description | Type | | ||
| --------- | ----------- | ---- | | ||
| `name` | The name of the metric. | Required | | ||
| `description` | The description of the metric. | Optional | | ||
| `type` | The type of the metric (cumulative, derived, ratio, or simple). | Required | | ||
| `label` | Required string that defines the display value in downstream tools. Accepts plain text, spaces, and quotes (such as `orders_total` or `"orders_total"`). | Required | | ||
| `type_params` | The type parameters of the metric. | Required | | ||
| `window` | The accumulation window, such as 1 month, 7 days, 1 year. This can't be used with `grain_to_date`. | Optional | | ||
| `grain_to_date` | Sets the accumulation grain, such as month will accumulate data for one month. Then restart at the beginning of the next. This can't be used with `window`. | Optional | | ||
| `type_param:window` | The accumulation window, such as 1 month, 7 days, 1 year. This can't be used with `grain_to_date`. | Optional | | ||
| `type_param:grain_to_date` | Sets the accumulation grain, such as `month`, which will accumulate data for one month. Then restart at the beginning of the next. This can't be used with `window`. | Optional | | ||
| `type_param:period_agg` | Specifies how to roll up the cumulative metric to another granularity. Options are `start`, `end`, `average`. Defaults to `start` if no `window` is specified. | Optional | | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `type_param:window_choice` | Specifies whether to take the cumulative metric value from the beginning (min) or end (max) of the window. Defaults to `min`. | Optional | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Jstein77 is this right? and can window_choice and period_agg be used with window and grain_to_date? |
||
| `measure` | A list of measure inputs | Required | | ||
| `measure:name` | TThe measure you are referencing. | Optional | | ||
| `measure:fill_nulls_with` | Set the value in your metric definition instead of null (such as zero).| Optional | | ||
|
@@ -33,6 +39,8 @@ metrics: | |
type: cumulative # Required | ||
label: The value that will be displayed in downstream tools # Required | ||
type_params: # Required | ||
period_agg: start # Optional. Defaults to start. Accepted values: start|end|average | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
window_choice: max # Optional. Defaults to min. Choose between min|max | ||
measure: | ||
name: The measure you are referencing. # Required | ||
fill_nulls_with: Set the value in your metric definition instead of null (such as zero). # Optional | ||
|
@@ -85,7 +93,7 @@ This section details examples of when you specify and don't specify window optio | |
|
||
<TabItem value="specified" label="Example of window specified"> | ||
|
||
If a window option is specified, the MetricFlow framework applies a sliding window to the underlying measure. | ||
If a window option is specified, the MetricFlow framework applies a sliding window to the underlying measure. | ||
|
||
Suppose the underlying measure `customers` is configured to count the unique customers making orders at the Jaffle shop. | ||
|
||
|
@@ -97,25 +105,27 @@ measures: | |
|
||
``` | ||
|
||
We can write a cumulative metric `weekly_customers` as such: | ||
We can write a cumulative metric `weekly_customers` as such: | ||
|
||
``` yaml | ||
metrics: | ||
- name: weekly_customers # Define the measure and the window. | ||
type: cumulative | ||
type_params: | ||
measure: customers | ||
window: 7 days # Setting the window to 7 days since we want to track weekly active | ||
window: 7 days # Setting the window to 7 days since we want to track weekly active | ||
window_choice: min # Value at the beginning of the window | ||
period_agg: first # ADD CONTEXT | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
From the sample YAML above, note the following: | ||
From the sample YAML above, note the following: | ||
|
||
* `type`: Specify cumulative to indicate the type of metric. | ||
* `type_params`: Specify the measure you want to aggregate as a cumulative metric. You have the option of specifying a `window`, or a `grain to date`. | ||
* `type_params`: Specify the measure you want to aggregate as a cumulative metric. You have the option of specifying a `window`, `grain to date`, `period_agg`, `window_choice`. | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
For example, in the `weekly_customers` cumulative metric, MetricFlow takes a sliding 7-day window of relevant customers and applies a count distinct function. | ||
|
||
If you omit the `window`, the measure will accumulate over all time. Otherwise, you can choose from granularities like day, week, quarter, or month, and describe the window using phrases like "7 days" or "1 month." | ||
If you remove `window`, `window_choice`, `period_agg`, the measure will accumulate over all time. Otherwise, you can choose from granularities like day, week, quarter, or month, and describe the window using phrases like "7 days" or "1 month." | ||
|
||
</TabItem> | ||
|
||
|
@@ -164,7 +174,7 @@ metrics: | |
|
||
</Tabs> | ||
|
||
### Grain to date | ||
### Grain to date | ||
|
||
You can choose to specify a grain to date in your cumulative metric configuration to accumulate a metric from the start of a grain (such as week, month, or year). When using a window, such as a month, MetricFlow will go back one full calendar month. However, grain to date will always start accumulating from the beginning of the grain, regardless of the latest date of data. | ||
|
||
|
@@ -176,29 +186,33 @@ For example, let's consider an underlying measure of `order_total.` | |
agg: sum | ||
``` | ||
|
||
We can compare the difference between a 1-month window and a monthly grain to date. The cumulative metric in a window approach applies a sliding window of 1 month, whereas the grain to date by month resets at the beginning of each month. | ||
We can compare the difference between a 1-month window and a monthly grain to date. | ||
- The cumulative metric in a window approach applies a sliding window of 1 month | ||
- The grain to date by month resets at the beginning of each month. | ||
|
||
```yaml | ||
metrics: | ||
- name: cumulative_order_total_l1m #For this metric, we use a window of 1 month | ||
- name: cumulative_order_total_l1m # For this metric, we use a window of 1 month | ||
label: Cumulative Order total (L1M) | ||
description: Trailing 1-month cumulative order amount | ||
type: cumulative | ||
type_params: | ||
measure: order_total | ||
window: 1 month | ||
- name: cumulative_order_total_mtd #For this metric, we use a monthly grain-to-date | ||
window: 1 month # Applies a sliding window of 1 month | ||
- name: cumulative_order_total_mtd # For this metric, we use a monthly grain-to-date | ||
label: Cumulative Order total (MTD) | ||
description: The month-to-date value of all orders | ||
type: cumulative | ||
type_params: | ||
measure: order_total | ||
grain_to_date: month | ||
grain_to_date: month # Resets at the beginning of each month | ||
window_choice: min # Value at the beginning of the window | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
period_agg: first # ADD CONTEXT | ||
``` | ||
|
||
### Implementation | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To calculate the cumulative value of the metric over a given window we do a time range join to a timespine table using the primary time dimension as the join key. We use the accumulation window in the join to decide whether a record should be included on a particular day. The following SQL code produced from an example cumulative metric is provided for reference: | ||
To calculate the cumulative value of the metric over a given window, use a time range join to a timespine table using the primary time dimension as the join key. Use the accumulation window in the join to decide whether to include on a particular day. Refer to the following example cumulative metric SQL code: | ||
|
||
``` sql | ||
mirnawong1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
select | ||
|
@@ -247,6 +261,49 @@ limit 100; | |
|
||
``` | ||
|
||
## Granularity options | ||
Ensure you can request cumulative metrics at different granularities, reducing redundancy and configuration efforts. | ||
|
||
For example, `dbt sl query --metrics orders_last_7_days --group-by metric_time__week`, will show cumulative metrics by week. Cumulative metrics are not additive over time and you can use either the minimum or maximum value within the requested granularity window. You can configure this choice, with `min` being the default. | ||
|
||
#### Example configurations | ||
|
||
Cumulative metric with no window, default to min: | ||
|
||
```yaml | ||
- name: cumulative_revenue | ||
description: The cumulative revenue for all orders. | ||
label: Cumulative Revenue (All Time) | ||
type: cumulative | ||
type_params: | ||
measure: revenue | ||
period_agg: first # Optional. Defaults to first. Accepted values: first|end|avg | ||
``` | ||
|
||
Cumulative metric with a specified window and choice: | ||
```yaml | ||
- name: orders_last_7_days | ||
description: Count of orders. | ||
label: orders last 7 days | ||
type: cumulative | ||
type_params: | ||
measure: order_count | ||
window: 7 days | ||
window_choice: min # or max | ||
``` | ||
|
||
Cumulative metric with grain to date: | ||
|
||
```yaml | ||
- name: orders_last_month_to_date | ||
label: Orders month to date | ||
type: cumulative | ||
type_params: | ||
measure: order_count | ||
grain_to_date: month | ||
window_choice: min # or max | ||
``` | ||
|
||
## Limitations | ||
|
||
If you specify a `window` in your cumulative metric definition, you must include `metric_time` as a dimension in the SQL query. This is because the accumulation window is based on metric time. For example, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jstein77 I'm not sure I udnerstand the definition? And what does teh start/end/avg mean?