Rename objects in a Metric View
This how-to demonstrates renaming a Metric View field.
The same pattern applies to every collection in a Metric View: Fields, Measures, Dimensions and Joins.
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
Assign to the object's Name property. Everything else about the object (its expression, comment, display name, synonyms and format) is left alone, and it keeps its place in the collection.
var view = SemanticBridge.MetricView.Model;
view.Fields["order_month"].Name = "Order Month";
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 collection's name index is updated with the object, so the field is reachable under its new name straight away:
var field = view.Fields["Order Month"];
Rules
- Names must stay unique within their collection. Renaming a field to a name another field already uses throws an
ArgumentException, and neither the object nor the collection is changed. - Name matching is case-insensitive, following Databricks SQL.
view.Fields["ORDER MONTH"]finds the field renamed above. A rename that only changes casing is still worth doing, since it refreshes the stored name. - The rename applies to the object model in memory. Serialize the view to write it out.