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.
Note
These how-tos target Tabular Editor 3.26.2 and later. Earlier versions do not support the v1.1 Metric View features shown here.
Load the sample Metric View for these code samples
Before starting, make sure you have Tabular Editor 3 open and have a Tabular model opened, or create a new model.
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). Use either method below to load it (either "download and load" or "copy and deserialize"), then follow along with the rest of this how-to. You can run either command in the same C# script as the rest of this example, or you can run it first, in its own C# script, and the rest of the example in its own C# script.
Download sample-metricview.yaml
and load it by path:
SemanticBridge.MetricView.Load("C:/path/to/sample-metricview.yaml");
Serialize to a string
Use Serialize() to get the YAML representation.
This simply re-serializes the YAML you loaded above.
var yaml = SemanticBridge.MetricView.Serialize();
var sb = new System.Text.StringBuilder();
sb.AppendLine("YAML output:");
sb.AppendLine("------------");
sb.AppendLine(yaml);
Output(sb.ToString());
Output
YAML output:
------------
version: 1.1
source: sales.fact.orders
joins:
- name: product
source: sales.dim.product
on: source.product_id = product.product_id
cardinality: many_to_one
- name: customer
source: sales.dim.customer
on: source.customer_id = customer.customer_id
cardinality: many_to_one
- name: date
source: sales.dim.date
on: source.order_date = date.date_key
cardinality: many_to_one
fields:
- name: product_name
expr: product.product_name
- name: product_category
expr: product.category
display_name: 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)
display_name: Total Revenue
format:
type: currency
decimal_places:
type: exact
places: 2
currency_code: USD
- name: gross_margin
expr: SUM(revenue) - SUM(cost)
- name: order_count
expr: COUNT(*)
- name: avg_order_value
expr: AVG(revenue)
- name: revenue_to_budget
expr: (SUM(revenue) - SUM(budget)) / SUM(budget)
display_name: Revenue vs Budget
format:
type: percentage
decimal_places:
type: max
places: 1
- name: unique_customers
expr: COUNT(DISTINCT customer_id)
Save to a file
Use Save(path) to write the YAML directly to disk.
This will write the Metric View you loaded above 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:
var view = SemanticBridge.MetricView.Model;
// set a display name on a field, then serialize to confirm it round-trips
view.Fields["order_month"].DisplayName = "Order Month";
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: 1.1
source: sales.fact.orders
joins:
- name: product
source: sales.dim.product
on: source.product_id = product.product_id
cardinality: many_to_one
- name: customer
source: sales.dim.customer
on: source.customer_id = customer.customer_id
cardinality: many_to_one
- name: date
source: sales.dim.date
on: source.order_date = date.date_key
cardinality: many_to_one
fields:
- name: product_name
expr: product.product_name
- name: product_category
expr: product.category
display_name: 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
display_name: Order Month
measures:
- name: total_revenue
expr: SUM(revenue)
display_name: Total Revenue
format:
type: currency
decimal_places:
type: exact
places: 2
currency_code: USD
- name: gross_margin
expr: SUM(revenue) - SUM(cost)
- name: order_count
expr: COUNT(*)
- name: avg_order_value
expr: AVG(revenue)
- name: revenue_to_budget
expr: (SUM(revenue) - SUM(budget)) / SUM(budget)
display_name: Revenue vs Budget
format:
type: percentage
decimal_places:
type: max
places: 1
- name: unique_customers
expr: COUNT(DISTINCT customer_id)