Skip to content

Commit 81ecf5c

Browse files
Add multiple distinct keys section with warehouse processing fee example
Generated-By: mintlify-agent
1 parent 715e243 commit 81ecf5c

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

references/metrics.mdx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,82 @@ If the value you are aggregating is truly distinct based on the distinct key (e.
974974
</Tab>
975975
</Tabs>
976976

977+
**Using multiple distinct keys**
978+
979+
When you need to deduplicate by a combination of fields, you can specify multiple distinct keys. A row is considered unique only when the entire combination of key values is unique.
980+
981+
For example, warehouse processing fees are charged per warehouse per order. If there are two items from the same warehouse in the same order, the fee should not be counted twice:
982+
983+
| order_id | order_item_id | item_warehouse_location | warehouse_processing_fee_per_order |
984+
|----------|---------------|-------------------------|-------------------------------------|
985+
| 🟦 1 | 1 | US | 2.5 |
986+
| 🟦 1 | 2 | US | 2.5 |
987+
| 🟩 2 | 3 | US | 2.5 |
988+
| 🟩 2 | 4 | Canada | 3.0 |
989+
| 🟩 2 | 5 | Canada | 3.0 |
990+
| 🟩 2 | 6 | UK | 4.0 |
991+
992+
With `distinct_keys: [order_id, item_warehouse_location]`:
993+
- Order 1 🟦: One unique warehouse (US) → 2.5
994+
- Order 2 🟩: Three unique warehouses (US, Canada, UK) → 2.5 + 3.0 + 4.0 = 9.5
995+
- **Total: 12.0**
996+
997+
<Tabs>
998+
<Tab title="dbt v1.9 and earlier">
999+
```yaml
1000+
columns:
1001+
- name: warehouse_processing_fee_per_order
1002+
description: warehouse processing fee per order
1003+
meta:
1004+
dimension:
1005+
type: number
1006+
metrics:
1007+
total_processing_fee:
1008+
type: sum_distinct
1009+
distinct_keys: [order_id, item_warehouse_location]
1010+
description: >
1011+
Processing fees are charged per warehouse per order.
1012+
If there are two items from the same warehouse the fee
1013+
should not be counted twice.
1014+
```
1015+
</Tab>
1016+
<Tab title="dbt v1.10+ and Fusion">
1017+
```yaml
1018+
columns:
1019+
- name: warehouse_processing_fee_per_order
1020+
description: warehouse processing fee per order
1021+
config:
1022+
meta:
1023+
dimension:
1024+
type: number
1025+
metrics:
1026+
total_processing_fee:
1027+
type: sum_distinct
1028+
distinct_keys: [order_id, item_warehouse_location]
1029+
description: >
1030+
Processing fees are charged per warehouse per order.
1031+
If there are two items from the same warehouse the fee
1032+
should not be counted twice.
1033+
```
1034+
</Tab>
1035+
<Tab title="Lightdash YAML">
1036+
```yaml
1037+
type: model
1038+
name: order_line_items
1039+
1040+
metrics:
1041+
total_processing_fee:
1042+
type: sum_distinct
1043+
sql: ${TABLE}.warehouse_processing_fee_per_order
1044+
distinct_keys: [order_id, item_warehouse_location]
1045+
description: >
1046+
Processing fees are charged per warehouse per order.
1047+
If there are two items from the same warehouse the fee
1048+
should not be counted twice.
1049+
```
1050+
</Tab>
1051+
</Tabs>
1052+
9771053
### string
9781054

9791055
Used with fields that include letters or special characters.

0 commit comments

Comments
 (0)