Remove an object from a Metric View
This how-to demonstrates removing Metric View fields and measures. Similar approaches apply to all collections in a Metric View.
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");
Note
Each removal script here affects the currently loaded Metric View.
If you want to run all of these, make sure to run the Deserialize above before each removal.
Remove by name
Get the Metric View field and delete it.
After you delete an object, you should not attempt to modify it.
You can still read properties off of the deleted object.
It is safe to call Delete() on an object multiple times; after the first, these are no-ops.
var view = SemanticBridge.MetricView.Model;
var sb = new System.Text.StringBuilder();
sb.AppendLine($"Fields before: {view.Fields.Count}");
var fieldToRemove = view.Fields["order_month"];
fieldToRemove.Delete();
fieldToRemove.Delete(); // note we can call Delete twice safely
sb.AppendLine($"Removed: {fieldToRemove.Name}");
sb.AppendLine($"Fields after: {view.Fields.Count}");
Output(sb.ToString());
Output:
Fields before: 6
Removed: order_month
Fields after: 5
Observe that there are multiple calls to Delete() but only one removal.
Remove a measure
Measures are removed the same way: get a reference to the measure and delete it.
var view = SemanticBridge.MetricView.Model;
var sb = new System.Text.StringBuilder();
sb.AppendLine($"Measures before: {view.Measures.Count}");
var measureToRemove = view.Measures["gross_margin"];
measureToRemove.Delete();
sb.AppendLine($"Removed: {measureToRemove.Name}");
sb.AppendLine($"Measures after: {view.Measures.Count}");
Output(sb.ToString());
Output:
Measures before: 6
Removed: gross_margin
Measures after: 5
Remove multiple Metric View fields
Filter to the fields you want to remove, snapshot them with ToList, then delete each one.
Snapshotting first avoids modifying the collection while iterating it.
var view = SemanticBridge.MetricView.Model;
var sb = new System.Text.StringBuilder();
sb.AppendLine($"Fields before: {view.Fields.Count}");
// Remove all date-related fields
string[] toRemove = ["order_date", "order_year", "order_month"];
foreach (var field in view.Fields.Where(f => toRemove.Contains(f.Name)).ToList())
{
field.Delete();
}
sb.AppendLine($"Fields after: {view.Fields.Count}");
sb.AppendLine();
sb.AppendLine("Remaining fields:");
sb.AppendLine("-----------------");
foreach (var field in view.Fields)
{
sb.AppendLine($" {field.Name}");
}
Output(sb.ToString());
Output:
Fields before: 6
Fields after: 3
Remaining fields:
-----------------
product_name
product_category
customer_segment
Remove Metric View fields from a specific table
Remove all Metric View fields that reference the date table.
Warning
This example is not guaranteed to remove all and exclusively Metric View fields which reference a given Metric View Join. Metric View fields may include near-arbitrary SQL expressions, and may also reference previously defined Metric View fields. This example is for illustrative purposes only.
var view = SemanticBridge.MetricView.Model;
var sb = new System.Text.StringBuilder();
sb.AppendLine($"Fields before: {view.Fields.Count}");
foreach (var field in view.Fields.Where(f => f.Expr.StartsWith("date.")).ToList())
{
field.Delete();
sb.AppendLine($"Removed: {field.Name} ({field.Expr})");
}
sb.AppendLine($"Fields after: {view.Fields.Count}");
Output(sb.ToString());
Output:
Fields before: 6
Removed: order_date (date.full_date)
Removed: order_year (date.year)
Removed: order_month (date.month_name)
Fields after: 3