Rename objects in a Metric View
This how-to demonstrates renaming a Metric View field. The same patterns 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");
Rename a field
Rename a field by adding a new field under the new name, copying its other properties across, then removing the original.
AddField sets only the name and expression, so copy the remaining properties (Comment, DisplayName, Synonyms, Format) yourself.
var view = SemanticBridge.MetricView.Model;
var old = view.Fields["order_month"];
// add the replacement, copy the remaining properties, then remove the original
var renamed = view.AddField("Order Month", old.Expr);
renamed.Comment = old.Comment;
renamed.DisplayName = old.DisplayName;
renamed.Synonyms = old.Synonyms;
renamed.Format = old.Format;
old.Delete();
var sb = new System.Text.StringBuilder();
sb.AppendLine("Fields:");
foreach (var field in view.Fields)
{
sb.AppendLine($" {field.Name}");
}
Output(sb.ToString());
Output:
Fields:
product_name
product_category
customer_segment
order_date
order_year
Order Month
The re-added field moves to the end of the collection.