Table of Contents

Serialize a Metric View to YAML

This how-to demonstrates how to serialize a Metric View back to YAML format, either as a string or saved to a file.

Warning

The MVP only supports v0.1 Metric View properties. Any v1.1 metadata present in a loaded Metric View is silently ignored and will be lost when you serialize. Do not overwrite a source YAML file that contains v1.1 metadata.

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)
    """);

Serialize to a string

Use Serialize() to get the YAML representation:

var yaml = SemanticBridge.MetricView.Serialize();

var sb = new System.Text.StringBuilder();
sb.AppendLine("YAML output:");
sb.AppendLine("------------");
sb.AppendLine(yaml);
Output(sb.ToString());

Save to a file

Use Save(path) to write the YAML directly to disk:

var path = "C:/MetricViews/updated-sales-metrics.yaml";

SemanticBridge.MetricView.Save(path);

Output($"Metric View saved to: {path}");

Round-trip workflow

A common workflow is to load, modify, and save a Metric View:

using System.Globalization;
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;

// The Metric View is already loaded from the include above

var view = SemanticBridge.MetricView.Model;
var textInfo = CultureInfo.CurrentCulture.TextInfo;

// Modify: rename dimensions from snake_case to Title Case
var renamed = view.Dimensions.Select(dim => new MetricView.Dimension
{
    Name = textInfo.ToTitleCase(dim.Name.Replace('_', ' ')),
    Expr = dim.Expr
}).ToList();

view.Dimensions.Clear();
foreach (var dim in renamed)
{
    view.Dimensions.Add(dim);
}

// Serialize to see the result
var yaml = SemanticBridge.MetricView.Serialize();

var sb = new System.Text.StringBuilder();
sb.AppendLine("Modified YAML:");
sb.AppendLine("--------------");
sb.AppendLine(yaml);
Output(sb.ToString());

Output:

Modified YAML:
--------------
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)

See also