Problem
DJ doesn't have a clean way to define metrics that operate at different granularities than the query grain. Common use cases that are difficult today:
- Share of total - numerator respects filters, denominator is global
- Market share within category - compute parent-level totals while querying at child level
Proposed Solution
Add two new fields to metrics: fixed and exclude.
metrics:
- name: revenue
expression: SUM(revenue)
- name: total_revenue
expression: SUM(revenue)
fixed: true # Ignores all dimensions/filters
- name: genre_revenue
expression: SUM(revenue)
exclude: [title] # Ignores title + hierarchy children
- name: daily_active_users
expression: COUNT(DISTINCT user_id)
required_dimensions: [date] # Existing - must include date in query
| Field Def |
Type |
Behavior |
| required_dimensions |
list[str] |
Must include these dimensions |
| exclude |
list[str] |
Ignore these dimensions + hierarchy children |
| fixed |
bool |
Ignore all dimensions and filters (global) |
Example: Market Share Within Genre
metrics:
- name: revenue
expression: SUM(revenue)
- name: genre_revenue
expression: SUM(revenue)
exclude: [title]
- name: market_share_within_genre
expression: revenue / genre_revenue
type: derived
Query at title + genre grain yields generated SQL:
SELECT
title,
genre,
SUM(revenue) as revenue,
SUM(revenue) OVER (PARTITION BY genre) as genre_revenue,
SUM(revenue) / SUM(revenue) OVER (PARTITION BY genre) as market_share_within_genre
FROM fact_table
GROUP BY title, genre
Hierarchy Behavior for exclude
When excluding a dimension, its children in the hierarchy are also excluded:
Hierarchy: genre -> title -> episode
exclude: [title] means:
- title: EXCLUDED
- episode: EXCLUDED (child of title)
- genre: KEPT (parent of title)
Problem
DJ doesn't have a clean way to define metrics that operate at different granularities than the query grain. Common use cases that are difficult today:
Proposed Solution
Add two new fields to metrics: fixed and exclude.
Example: Market Share Within Genre
Query at
title+genregrain yields generated SQL:Hierarchy Behavior for exclude
When excluding a dimension, its children in the hierarchy are also excluded:
Hierarchy:
genre->title->episodeexclude: [title]means:-
title: EXCLUDED-
episode: EXCLUDED (child of title)-
genre: KEPT (parent of title)