Load and Inspect a Metric View
This how-to demonstrates how to load a Databricks Metric View into Tabular Editor and explore its structure using C# scripts. This is the foundational skill for all other Metric View operations.
Sample Metric View
This how-to uses a sample e-commerce Metric View representing sales data with three dimension tables (product, customer, date) joined to a fact table (orders).
version: 0.1
source: sales.fact.orders
joins:
- name: product
source: sales.dim.product
on: source.product_id = product.product_id
- name: customer
source: sales.dim.customer
on: source.customer_id = customer.customer_id
- name: date
source: sales.dim.date
on: source.order_date = date.date_key
dimensions:
- name: product_name
expr: product.product_name
- name: product_category
expr: product.category
- name: customer_segment
expr: customer.segment
- name: order_date
expr: date.full_date
- name: order_year
expr: date.year
- name: order_month
expr: date.month_name
measures:
- name: total_revenue
expr: SUM(revenue)
- name: order_count
expr: COUNT(order_id)
- name: avg_order_value
expr: AVG(revenue)
- name: unique_customers
expr: COUNT(DISTINCT customer_id)
Load a Metric View from a file
Use SemanticBridge.MetricView.Load to load a Metric View from a YAML file on disk.
// Load from a file path
SemanticBridge.MetricView.Load("C:/MetricViews/sales-metrics.yaml");
// Confirm it loaded
Output($"Loaded Metric View version: {SemanticBridge.MetricView.Model.Version}");
Deserialize Metric View for these code samples
This how-to uses a sample e-commerce Metric View representing sales data with three dimension tables (product, customer, date) joined to a fact table (orders). Run the snippet below first, if you'd like to follow along with the code in the rest of this how-to
SemanticBridge.MetricView.Deserialize("""
version: 0.1
source: sales.fact.orders
joins:
- name: product
source: sales.dim.product
on: source.product_id = product.product_id
- name: customer
source: sales.dim.customer
on: source.customer_id = customer.customer_id
- name: date
source: sales.dim.date
on: source.order_date = date.date_key
dimensions:
- name: product_name
expr: product.product_name
- name: product_category
expr: product.category
- name: customer_segment
expr: customer.segment
- name: order_date
expr: date.full_date
- name: order_year
expr: date.year
- name: order_month
expr: date.month_name
measures:
- name: total_revenue
expr: SUM(revenue)
- name: order_count
expr: COUNT(order_id)
- name: avg_order_value
expr: AVG(revenue)
- name: unique_customers
expr: COUNT(DISTINCT customer_id)
""");
Access the loaded Metric View
After loading, the Metric View is available in any script as SemanticBridge.MetricView.Model.
This returns a Metric View View object, the root of the Metric View object graph.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine($"Version: {view.Version}");
sb.AppendLine($"Source (fact table): {view.Source}");
Output(sb.ToString());
Inspect Metric View joins (dimension tables)
The Metric View Joins property contains the dimension tables joined to the fact.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine($"Number of joins: {view.Joins?.Count ?? 0}");
sb.AppendLine("");
foreach (var join in view.Joins ?? [])
{
sb.AppendLine($"Join: {join.Name}");
sb.AppendLine($" Source: {join.Source}");
sb.AppendLine($" On: {join.On}");
sb.AppendLine("");
}
Output(sb.ToString());
Output:
Number of joins: 3
Join: product
Source: sales.dim.product
On: product_id = product.product_id
Join: customer
Source: sales.dim.customer
On: customer_id = customer.customer_id
Join: date
Source: sales.dim.date
On: order_date = date.date_key
Inspect Metric View dimensions (fields)
The Metric View Dimensions property contains all field definitions.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine($"Number of dimensions: {view.Dimensions?.Count ?? 0}");
sb.AppendLine("");
foreach (var dim in view.Dimensions ?? [])
{
sb.AppendLine($"{dim.Name,-20} <- {dim.Expr}");
}
Output(sb.ToString());
Output:
Number of dimensions: 6
product_name <- product.product_name
product_category <- product.category
customer_segment <- customer.segment
order_date <- date.full_date
order_year <- date.year
order_month <- date.month_name
Inspect Metric View measures
The Metric View Measures property contains all Metric View measure definitions with their aggregation expressions.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine($"Number of measures: {view.Measures?.Count ?? 0}");
sb.AppendLine("");
foreach (var measure in view.Measures ?? [])
{
sb.AppendLine($"{measure.Name,-20} = {measure.Expr}");
}
Output(sb.ToString());
Output:
Number of measures: 4
total_revenue = SUM(revenue)
order_count = COUNT(order_id)
avg_order_value = AVG(revenue)
unique_customers = COUNT(DISTINCT customer_id)
Generate a complete summary
Here is a complete script that outputs a formatted summary of the entire Metric View.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine("METRIC VIEW SUMMARY");
sb.AppendLine("===================");
sb.AppendLine("");
sb.AppendLine($"Version: {view.Version}");
sb.AppendLine($"Fact Source: {view.Source}");
sb.AppendLine("");
// Joins
sb.AppendLine($"JOINS ({view.Joins?.Count ?? 0})");
sb.AppendLine("---------");
foreach (var join in view.Joins ?? [])
{
sb.AppendLine($" {join.Name,-15} -> {join.Source}");
}
sb.AppendLine("");
// Dimensions
sb.AppendLine($"DIMENSIONS ({view.Dimensions?.Count ?? 0})");
sb.AppendLine("--------------");
foreach (var dim in view.Dimensions ?? [])
{
sb.AppendLine($" {dim.Name,-20} <- {dim.Expr}");
}
sb.AppendLine("");
// Measures
sb.AppendLine($"MEASURES ({view.Measures?.Count ?? 0})");
sb.AppendLine("------------");
foreach (var measure in view.Measures ?? [])
{
sb.AppendLine($" {measure.Name,-20} = {measure.Expr}");
}
Output(sb.ToString());
Next steps
Now that you can load and inspect a Metric View, you can:
- Validate the Metric View to check for issues
- Import the Metric View to Tabular to create tables, columns, and measures