Rename objects in a Metric View
This how-to demonstrates how to rename Metric View dimensions using a copy-modify pattern for bulk transformations. The same patterns apply to all collections in a Metric View.
Deserialize Metric View for these code samples
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). Run the snippet below first, if you'd like to follow along with the code in the rest of this how-to
SemanticBridge.MetricView.Deserialize("""
version: 0.1
source: sales.fact.orders
joins:
- name: product
source: sales.dim.product
on: source.product_id = product.product_id
- name: customer
source: sales.dim.customer
on: source.customer_id = customer.customer_id
- name: date
source: sales.dim.date
on: source.order_date = date.date_key
dimensions:
- name: product_name
expr: product.product_name
- name: product_category
expr: 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)
- name: order_count
expr: COUNT(order_id)
- name: avg_order_value
expr: AVG(revenue)
- name: unique_customers
expr: COUNT(DISTINCT customer_id)
""");
The copy-modify pattern
Since Metric View dimension names are properties on objects in a collection, the cleanest approach is to:
- Create new Metric View
Dimensionobjects with the modified names - Clear the original collection
- Add the new objects
This avoids issues with modifying objects while iterating.
Convert snake_case to Title Case
Transform Metric View dimension names from product_name to Product Name:
using System.Globalization;
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;
var view = SemanticBridge.MetricView.Model;
var textInfo = CultureInfo.CurrentCulture.TextInfo;
var sb = new System.Text.StringBuilder();
sb.AppendLine("BEFORE");
sb.AppendLine("------");
foreach (var dim in view.Dimensions)
{
sb.AppendLine($" {dim.Name}");
}
// Create renamed dimensions
var renamed = view.Dimensions.Select(dim => new MetricView.Dimension
{
Name = textInfo.ToTitleCase(dim.Name.Replace('_', ' ')),
Expr = dim.Expr
}).ToList();
// Replace the collection
view.Dimensions.Clear();
foreach (var dim in renamed)
{
view.Dimensions.Add(dim);
}
sb.AppendLine();
sb.AppendLine("AFTER");
sb.AppendLine("-----");
foreach (var dim in view.Dimensions)
{
sb.AppendLine($" {dim.Name}");
}
Output(sb.ToString());
Output:
BEFORE
------
product_name
product_category
customer_segment
order_date
order_year
order_month
AFTER
-----
Product Name
Product Category
Customer Segment
Order Date
Order Year
Order Month
Rename using a mapping dictionary
Apply specific renames using a lookup:
using MetricView = TabularEditor.SemanticBridge.Platforms.Databricks.MetricView;
var view = SemanticBridge.MetricView.Model;
// Define rename mappings
var renames = new Dictionary<string, string>
{
{ "product_name", "Product" },
{ "product_category", "Category" },
{ "customer_segment", "Segment" },
{ "order_date", "Date" },
{ "order_year", "Year" },
{ "order_month", "Month" }
};
var sb = new System.Text.StringBuilder();
// Create renamed dimensions
var renamed = view.Dimensions
.Select(
dim => new MetricView.Dimension
{
Name = renames.TryGetValue(dim.Name, out var newName) ? newName : dim.Name,
Expr = dim.Expr
})
.ToList();
// Replace the collection
view.Dimensions.Clear();
foreach (var dim in renamed)
{
view.Dimensions.Add(dim);
}
sb.AppendLine("Renamed dimensions:");
sb.AppendLine("-------------------");
foreach (var dim in view.Dimensions)
{
sb.AppendLine($" {dim.Name,-20} <- {dim.Expr}");
}
Output(sb.ToString());
Output:
Renamed dimensions:
-------------------
Product <- product.product_name
Category <- product.category
Segment <- customer.segment
Date <- date.full_date
Year <- date.year
Month <- date.month_name