Exposures and Semantic Layer
Semantic Layer Architecture
Architecture Diagram
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DBT SEMANTIC LAYER ARCHITECTURE β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β SEMANTIC MODELS β β
β β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β β β ENTITIES β β DIMENSIONS β β MEASURES β β β
β β β β β β β β β β
β β β β’ Primary β β β’ Time β β β’ Additive β β β
β β β β’ Foreign β β β’ Categoricalβ β β’ Semi-additive β β β
β β β β’ Unique β β β’ Numeric β β β’ Non-additive β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β METRICS STORE β β
β β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β β’ Derived metrics β’ Simple metrics β’ Cumulative metrics β β β
β β β β’ Conversion metrics β’ Ratio metrics β’ Window metrics β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β EXPOSURES β β
β β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β β β DASHBOARDS β β APPLICATIONS β β EXPORTS β β β
β β β β β β β β β β
β β β β’ Looker β β β’ APIs β β β’ Data sets β β β
β β β β’ Tableau β β β’ ML models β β’ External systems β β β
β β β β’ PowerBI β β β’ Services β β β’ File exports β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Metrics Flow
Architecture Diagram
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β METRICS DEFINITION AND USAGE β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β SEMANTIC MODEL β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β name: orders_semantic β β
β β defaults: β β
β β agg_time_dimension: order_date β β
β β entities: β β
β β - name: order_id β β
β β type: primary β β
β β - name: customer_id β β
β β type: foreign β β
β β dimensions: β β
β β - name: order_date β β
β β type: time β β
β β type_params: β β
β β time_granularity: day β β
β β measures: β β
β β - name: order_total β β
β β type: sum β β
β β expr: amount β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β METRICS DEFINITION β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β - name: total_revenue β β
β β type: simple β β
β β type_params: β β
β β measure: order_total β β
β β description: "Total revenue from all orders" β β
β β β β
β β - name: avg_order_value β β
β β type: derived β β
β β type_params: β β
β β expr: total_revenue / order_count β β
β β description: "Average order value" β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β QUERY INTERFACE β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β metrics: [total_revenue, order_count] β β
β β group_by: [order_date__day, customer_segment] β β
β β where: "order_date >= '2024-01-01'" β β
β β β β
β β Result: β β
β β ββββββββββββββ¬ββββββββββ¬βββββββββββ¬βββββββββββββ β β
β β β Date β Segment β Revenue β Orders β β β
β β ββββββββββββββΌββββββββββΌβββββββββββΌβββββββββββββ€ β β
β β β 2024-01-01 β Premium β $150,000 β 1,500 β β β
β β β 2024-01-01 β Standardβ $85,000 β 2,100 β β β
β β ββββββββββββββ΄ββββββββββ΄βββββββββββ΄βββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Exposure Dependencies
Architecture Diagram
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EXPOSURE DEPENDENCY GRAPH β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ β
β β RAW DATA β β
β ββββββββββ¬βββββββββ β
β β β
β βββββββββββββββββββββΌββββββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β stg_orders β β stg_customersβ β stg_products β β
β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β
β β β β β
β ββββββββββββββββββββββΌβββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββ β
β β fct_orders β β
β ββββββββ¬ββββββββ β
β β β
β βββββββββββββββββββββββΌββββββββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β dim_customersβ β dim_products β β dim_dates β β
β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β
β β β β β
β ββββββββββββββββββββββΌβββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββ β
β β Order β β
β β Analytics β β
β β Dashboard β β
β β (Exposure) β β
β ββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Detailed Explanation
The dbt semantic layer provides a standardized way to define metrics, dimensions, and business logic that can be consumed by downstream applications.
Semantic Models
Semantic models are the foundation of the semantic layer. They define:
- Entities: Keys that identify records (primary, foreign, unique)
- Dimensions: Qualitative attributes for grouping and filtering
- Measures: Quantitative values that can be aggregated
Metrics
Metrics are defined calculations based on semantic models:
- Simple metrics: Direct aggregations of measures
- Derived metrics: Calculations based on other metrics
- Cumulative metrics: Running totals over time
- Conversion metrics: Funnel analysis and conversion rates
Exposures
Exposures define how data is consumed downstream:
- Dashboards: BI tool dashboards (Looker, Tableau, PowerBI)
- Applications: Data applications and APIs
- Exports: Data exports to external systems
Benefits of the Semantic Layer
- Single source of truth: Consistent metric definitions
- Governance: Centralized business logic management
- Reusability: Metrics defined once, used everywhere
- Lineage: Track metric dependencies and usage
- Performance: Pre-computed aggregations
Code Examples
Semantic Model Definition
# semantic/order_semantic.yml
version: 2
semantic_models:
- name: orders_semantic
description: "Semantic model for order analytics"
defaults:
agg_time_dimension: order_date
model: ref('fct_orders')
entities:
- name: order_id
type: primary
expr: order_id
- name: customer_id
type: foreign
expr: customer_id
dimensions:
- name: order_date
type: time
type_params:
time_granularity: day
- name: order_status
type: categorical
expr: status
- name: customer_segment
type: categorical
expr: segment
measures:
- name: order_total
description: "Total revenue from orders"
agg: sum
expr: amount
- name: order_count
description: "Count of orders"
agg: count
expr: order_id
- name: avg_order_value
description: "Average order value"
agg: average
expr: amount
Metrics Definition
# semantic/metrics.yml
version: 2
metrics:
- name: total_revenue
description: "Total revenue from all orders"
type: simple
type_params:
measure: order_total
filter: |
{{ Dimension('order_status') }} != 'cancelled'
- name: order_count
description: "Total number of orders"
type: simple
type_params:
measure: order_count
- name: avg_order_value
description: "Average order value"
type: derived
type_params:
expr: "total_revenue / order_count"
- name: revenue_growth
description: "Month-over-month revenue growth"
type: derived
type_params:
expr: "(current_month_revenue - prev_month_revenue) / prev_month_revenue"
- name: conversion_rate
description: "Conversion rate from views to purchases"
type: conversion
type_params:
measure: order_count
conversion_measure: view_count
window: 7 days
Exposure Definition
# exposures/order_dashboard.yml
version: 2
exposures:
- name: order_analytics_dashboard
type: dashboard
description: "Main dashboard for order analytics"
url: https://looker.example.com/dashboards/order_analytics
depends_on:
- ref('fct_orders')
- ref('dim_customers')
- ref('dim_products')
owner:
name: Analytics Team
email: analytics@company.com
meta:
refresh_frequency: hourly
data_source: Looker
- name: ml_feature_store
type: ml
description: "ML feature store for customer segmentation"
url: https://ml.example.com/features/customer_segmentation
depends_on:
- ref('customer_features')
- ref('order_features')
owner:
name: Data Science Team
email: ds@company.com
- name: data_export_daily
type: application
description: "Daily data export to external system"
url: https://api.example.com/exports/daily
depends_on:
- ref('fct_daily_metrics')
owner:
name: Data Engineering
email: data-eng@company.com
Semantic Model with Joins
# semantic/order_customer_semantic.yml
version: 2
semantic_models:
- name: order_with_customer
description: "Orders with customer dimensions"
defaults:
agg_time_dimension: order_date
model: ref('fct_orders')
joins:
- name: customers
join_type: left
sql: "ON {{ entity('order_id') }} = {{ entity('customer_id') }}"
entities:
- name: order_id
type: primary
expr: order_id
dimensions:
- name: order_date
type: time
type_params:
time_granularity: day
- name: customer_name
type: categorical
expr: "customers.customer_name"
- name: customer_segment
type: categorical
expr: "customers.segment"
measures:
- name: total_revenue
agg: sum
expr: amount
Querying the Semantic Layer
# Query definition for Looker
query:
metrics:
- total_revenue
- order_count
- avg_order_value
group_by:
- order_date__day
- customer_segment
where: |
{{ Dimension('order_date') }} >= '2024-01-01'
AND {{ Dimension('order_status') }} = 'completed'
order_by:
- order_date__day
limit: 1000
Performance Metrics
| Component | Description | Performance Impact |
|---|---|---|
| Semantic Model | Query compilation | Low |
| Metrics | Aggregation | Medium |
| Dimensions | Grouping | Low |
| Exposures | Dependency tracking | Minimal |
| Cache | Query result caching | High (positive) |
Best Practices
- Define semantic models for all analytical entities
- Use consistent naming for metrics and dimensions
- Document all exposures with clear descriptions
- Define relationships between semantic models
- Use appropriate measures for different aggregation types
- Filter at the metric level to ensure consistency
- Monitor exposure usage to understand data consumption
- Version control all semantic definitions