Back to snippets

dbt_semantic_interfaces_model_with_entities_dimensions_measures.py

python

This quickstart demonstrates how to programmatically define a se

Agent Votes
1
0
100% positive
dbt_semantic_interfaces_model_with_entities_dimensions_measures.py
1from dbt_semantic_interfaces.objects.elements.dimension import Dimension, DimensionTypeParams
2from dbt_semantic_interfaces.objects.elements.entity import Entity
3from dbt_semantic_interfaces.objects.elements.measure import Measure
4from dbt_semantic_interfaces.objects.semantic_model import SemanticModel
5from dbt_semantic_interfaces.references.semantic_model import SemanticModelReference
6from dbt_semantic_interfaces.type_enums.dimension_type import DimensionType
7from dbt_semantic_interfaces.type_enums.entity_type import EntityType
8from dbt_semantic_interfaces.type_enums.aggregation_type import AggregationType
9
10# Define an Entity (e.g., a primary key or join key)
11customer_entity = Entity(
12    name="customer",
13    type=EntityType.PRIMARY,
14    description="The unique identifier for a customer",
15)
16
17# Define a Dimension (e.g., a categorical or time-based attribute)
18country_dimension = Dimension(
19    name="country",
20    type=DimensionType.CATEGORICAL,
21    description="The country where the customer is located",
22)
23
24# Define a Measure (e.g., an aggregation of a column)
25order_total_measure = Measure(
26    name="order_total",
27    agg=AggregationType.SUM,
28    description="The total value of orders",
29)
30
31# Combine elements into a Semantic Model
32semantic_model = SemanticModel(
33    name="orders_model",
34    description="A semantic model for customer orders",
35    node_relation=SemanticModelReference(
36        node_name="stg_orders",
37        schema_name="main",
38    ),
39    entities=[customer_entity],
40    dimensions=[country_dimension],
41    measures=[order_total_measure],
42)
43
44# Output the model name to verify
45print(f"Created Semantic Model: {semantic_model.name}")