Semaphor

Overview

Create custom metrics, dimensions, and inline expressions without modifying your data source

Calculated fields let you create custom KPIs, derived groupings, and column transforms without modifying your underlying data source. They work at every level -- on a single card, across a dashboard, or saved to a semantic domain for organization-wide reuse.

There are three types of calculated fields, each serving a different purpose:

TypeWhat it doesDefined in
Calculated MetricProduces an aggregated number (a KPI)calculatedMetrics section
Calculated DimensionProduces a row-level grouping valuecalculatedDimensions section
Inline ExpressionTransforms a single column before aggregationexpression field on a dataset metric

Calculated Fields with Semantic Domains

When using a semantic domain, you can save calculated fields to the domain for organization-wide reuse. Domain-level calculated fields are managed by administrators and appear with a Domain badge in the field list.

See Domain-Level Calculated Fields for administrator setup instructions.

Looking for Running Totals or Moving Averages?

For analytical functions like running totals, moving averages, period-over-period deltas, and percent-of-total calculations, see Metric Transforms. Calculated fields define what to measure, while metric transforms analyze how those measurements change across rows.

When Calculations Happen

Calculated Dimensions are computed before aggregation — they transform each row of data and can be used for grouping (GROUP BY). Think of them as creating new columns at the row level.

Calculated Metrics are computed after aggregation — they operate on already-aggregated values. Think of them as combining totals, averages, or counts that have already been computed.

Example: If you have 1,000 order rows:

  • A calculated dimension like CASE WHEN {Amount} > 100 THEN 'Large' ELSE 'Small' END evaluates each of the 1,000 rows to categorize them
  • A calculated metric like SUM(Revenue) / SUM(Cost) first computes SUM(Revenue) and SUM(Cost) across all rows, then divides the two results

When You Need Calculated Fields

  • Standardize business KPIs -- Define profit margin, average order value, or conversion rate once and reuse across all dashboards
  • Create custom groupings -- Bucket order amounts into "Small / Medium / Large", truncate dates to fiscal quarters, or combine text fields
  • Transform raw data -- Convert cents to dollars, apply conditional logic, or round values before aggregation
  • Build cross-dataset metrics -- Compute net revenue (orders minus returns) by aggregating each dataset independently

Two Ways to Create Calculated Fields

In the Domain Template

Define calculated fields in YAML/JSON as part of your domain template. This is ideal for standardized, version-controlled business logic.

calculatedMetrics:
  profit_margin:
    label: "Profit Margin %"
    expression: "{profit} / NULLIF({revenue}, 0) * 100"
    inputs:
      profit: { metric: profit }
      revenue: { metric: revenue }

See Calculated Metrics and Calculated Dimensions for template syntax.

In the Explorer UI

Create calculated fields interactively without editing templates:

  1. Open the Explorer and click + Add Calculated Field
  2. Select Calculated Metric or Calculated Dimension
  3. Configure the expression and inputs
  4. Click Create

Fields created in the Explorer can be scoped to a card, dashboard, or saved to the domain (see Scope Options below).


AI-Powered Expression Generation

When creating calculated dimensions in the Explorer, you can describe what you want in plain English and Semaphor generates the SQL expression for you.

  1. Click Generate with AI in the expression editor
  2. Describe your calculation in natural language
  3. Review the generated expression
  4. Click Use Expression to apply it, or Regenerate for a different approach

Example prompts:

  • "Create buckets for order amount: small under 100, medium 100-500, large over 500"
  • "Extract the quarter from the order date"
  • "Calculate profit as revenue minus cost"

Always review AI-generated expressions before using them. The AI provides a starting point that you can modify as needed.


Expression Validation

Semaphor validates expressions to catch errors before you save.

What gets validated:

  • Field references -- All {FieldName} tokens must match available fields
  • SQL syntax -- Expression must be valid SQL for your database dialect
  • Self-references -- Cannot reference the field being edited (prevents circular references)
  • Function support -- Functions must be supported by your database

After clicking Validate Expression, you'll see:

  • Tokens found -- Which fields were detected and resolved
  • Errors -- Issues that must be fixed (unknown fields, syntax errors)
  • Warnings -- Suggestions for improvement (missing ELSE clause, division by zero risk)
  • Inferred output type -- The detected data type of the result
ErrorCauseSolution
Unknown field tokenField name doesn't match any available fieldCheck spelling or use the field picker
Self-reference detectedExpression references the field being editedRemove the circular reference
Unsupported functionDatabase doesn't support this SQL functionUse an alternative function for your dialect

For validation errors specific to domain templates (E001-E013), see Validation & Examples.


Scope Options

Control where your calculated field is available.

ScopeAvailable ToUse Case
Card (default)All usersExperimental calculations, one-time analysis
DashboardAll usersFields shared across multiple charts in one dashboard
DomainOrg users onlyStandard KPIs, business metrics, and reusable dimensions used organization-wide

Requirements for Domain Scope

Domain scope is only available when both conditions are met:

  1. Your card/dashboard is connected to a semantic domain (not a direct database connection)
  2. You are an organization user (tenant users cannot save to domain)

Permissions by User Type

ActionOrganization UserTenant User
View and use calculated fieldsYesYes
Create card-scoped fieldsYesYes
Create dashboard-scoped fieldsYesYes
Edit/delete card-scoped fieldsYesYes
Edit/delete dashboard-scoped fieldsYesYes
Save fields to domain scopeYesNo
Edit/delete domain-scoped fieldsYesNo

Organization users are employees with direct access to Semaphor. Tenant users are end users from your customers who access embedded dashboards. Domain-level fields are read-only for tenant users, ensuring consistent business logic across all customer-facing analytics.

Domain scope requires a description explaining what the field represents. This helps other users understand and correctly use the metric or dimension.


Best Practices

Naming:

  • Use descriptive names that explain what the field represents
  • Include units when relevant (e.g., "Revenue per Order (USD)")
  • Follow your organization's naming standards for consistency

Expressions:

  • Keep expressions simple -- break complex calculations into multiple calculated fields
  • Use NULLIF to prevent division by zero: {Revenue} / NULLIF({Orders}, 0)
  • Use COALESCE for null handling: COALESCE({Discount}, 0) * {Amount}
  • For complex multi-step calculations, use nested metrics

Documentation:

  • When saving to domain scope, write clear descriptions covering what the field measures, how it's calculated, and any caveats

Getting Started

On this page