Add an object to a Metric View
This how-to demonstrates adding new objects to a loaded Metric View and setting their properties. Similar patterns apply to all Metric View collections.
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");
Add a field
Use AddField to create and return a new Field you can manipulate.
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
sb.AppendLine($"Fields before adding: {view.Fields.Count}");
var field = view.AddField("customer_city", "customer.city");
sb.AppendLine($"Fields after adding: {view.Fields.Count}");
Output(sb.ToString());
Output
Fields before adding: 6
Fields after adding: 7
Add and configure a Join
AddJoin
works similarly to AddField: it constructs the object, adds it to the Metric View, and returns it so you can set further properties.
Set the cardinality with the JoinCardinality enum.
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;
var view = SemanticBridge.MetricView.Model;
// add a join, then set its remaining properties
var supplier = view.AddJoin("supplier", "sales.dim.supplier");
supplier.On = "source.supplier_id = supplier.supplier_id";
supplier.Cardinality = MetricView.JoinCardinality.ManyToOne;
AddJoin is also a method on any existing Join.
You would use this to create nested joins, for example, supplier.AddJoin("region", "sales.dim.region"),
which models a snowflake dimension.
Add and configure a Measure
AddMeasure works similarly to the other Add methods.
Some properties, such as a field or measure Format, have their own types you need to construct to set the property.
Create the Format variant you want, such as Format.Currency or Format.Percentage, and assign it.
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;
var sb = new System.Text.StringBuilder();
var view = SemanticBridge.MetricView.Model;
// add a new measure, then give it a currency format
var totalCost = view.AddMeasure("total_cost", "SUM(cost)");
totalCost.Format = new MetricView.Format.Currency { CurrencyCode = "USD" };
// read the format back off the measure
sb.AppendLine($"{totalCost.Name} format: {totalCost.Format}");
Output(sb.ToString());
Output
total_cost format: Currency { Type = Currency, DecimalPlaces = , HideGroupSeparator = , Abbreviation = , CurrencyCode = USD }