Table of Contents

Validate a Metric View with Default Rules

This how-to demonstrates validating a loaded Metric View using the built-in validation rules and interpreting the diagnostic messages.

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");

Default validation rules

The Semantic Bridge includes built-in rules that validate a Metric View definition against rules defined in the Databricks documentation. These rules are automatically run upon deserialization, whether via Deserialize directly or any method that reads a Metric View, such as Load or ImportToTabularFromFile. Diagnostics from those automatic runs remain available afterward through SemanticBridge.MetricView.ImportDiagnostics. You can also run these rules on demand against the loaded Metric View, which this document covers.

Run validation with default rules

Run SemanticBridge.MetricView.Validate(); with no arguments to run the built-in rules against the loaded Metric View.

var diagnostics = SemanticBridge.MetricView.Validate().ToList();

Output($"Validation complete: {diagnostics.Count} issue(s) found");

Output

Validation complete: 0 issue(s) found

The sample Metric View is valid, so this reports no issues.

Interpret diagnostic messages

Each diagnostic message contains:

  • Severity: Error, Warning, or Information
  • Message: Description of the issue
  • Path: Location of the object in the Metric View hierarchy
var diagnostics = SemanticBridge.MetricView.Validate().ToList();

var sb = new System.Text.StringBuilder();
sb.AppendLine("VALIDATION RESULTS");
sb.AppendLine("------------------");
sb.AppendLine("");

if (diagnostics.Count == 0)
{
    sb.AppendLine("No issues found.");
}
else
{
    foreach (var diag in diagnostics)
    {
        sb.AppendLine($"[{diag.Severity}] {diag.Message}");
        sb.AppendLine($"  Path: {diag.Path}");
        sb.AppendLine("");
    }
}

Output(sb.ToString());

Output

VALIDATION RESULTS
------------------

No issues found.

Example with a validation error

Validation always runs against the currently loaded Metric View, so you can introduce a violation in a script and see it caught. Here we clear a field's expression to trigger FieldExprRequired:

var view = SemanticBridge.MetricView.Model;
view.Fields["order_year"].Expr = "";

var diagnostics = SemanticBridge.MetricView.Validate().ToList();

var sb = new System.Text.StringBuilder();
sb.AppendLine("VALIDATION RESULTS");
sb.AppendLine("------------------");
sb.AppendLine("");

if (diagnostics.Count == 0)
{
    sb.AppendLine("No issues found.");
}
else
{
    foreach (var diag in diagnostics)
    {
        sb.AppendLine($"[{diag.Severity}] {diag.Message}");
        sb.AppendLine($"  Path: {diag.Path}");
        sb.AppendLine("");
    }
}

Output(sb.ToString());

Output:

VALIDATION RESULTS
------------------

[Error] Field 'order_year' expr cannot be empty
  Path: Model.Fields["order_year"].Expr

Filter diagnostics by severity

You can filter diagnostics to focus on errors only:

using System.Linq;
using TabularEditor.SemanticBridge.Orchestration;

var diagnostics = SemanticBridge.MetricView.Validate().ToList();
var errors = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToList();

var sb = new System.Text.StringBuilder();
sb.AppendLine($"Errors: {errors.Count}");
sb.AppendLine($"Total issues: {diagnostics.Count}");
Output(sb.ToString());

Output

Errors: 0
Total issues: 0

Next steps

See also