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:
| Type | What it does | Defined in |
|---|---|---|
| Calculated Metric | Produces an aggregated number (a KPI) | calculatedMetrics section |
| Calculated Dimension | Produces a row-level grouping value | calculatedDimensions section |
| Inline Expression | Transforms a single column before aggregation | expression 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' ENDevaluates each of the 1,000 rows to categorize them - A calculated metric like
SUM(Revenue) / SUM(Cost)first computesSUM(Revenue)andSUM(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.
See Calculated Metrics and Calculated Dimensions for template syntax.
In the Explorer UI
Create calculated fields interactively without editing templates:
- Open the Explorer and click + Add Calculated Field
- Select Calculated Metric or Calculated Dimension
- Configure the expression and inputs
- 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.
- Click Generate with AI in the expression editor
- Describe your calculation in natural language
- Review the generated expression
- 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
| Error | Cause | Solution |
|---|---|---|
| Unknown field token | Field name doesn't match any available field | Check spelling or use the field picker |
| Self-reference detected | Expression references the field being edited | Remove the circular reference |
| Unsupported function | Database doesn't support this SQL function | Use 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.
| Scope | Available To | Use Case |
|---|---|---|
| Card (default) | All users | Experimental calculations, one-time analysis |
| Dashboard | All users | Fields shared across multiple charts in one dashboard |
| Domain | Org users only | Standard KPIs, business metrics, and reusable dimensions used organization-wide |
Requirements for Domain Scope
Domain scope is only available when both conditions are met:
- Your card/dashboard is connected to a semantic domain (not a direct database connection)
- You are an organization user (tenant users cannot save to domain)
Permissions by User Type
| Action | Organization User | Tenant User |
|---|---|---|
| View and use calculated fields | Yes | Yes |
| Create card-scoped fields | Yes | Yes |
| Create dashboard-scoped fields | Yes | Yes |
| Edit/delete card-scoped fields | Yes | Yes |
| Edit/delete dashboard-scoped fields | Yes | Yes |
| Save fields to domain scope | Yes | No |
| Edit/delete domain-scoped fields | Yes | No |
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
NULLIFto prevent division by zero:{Revenue} / NULLIF({Orders}, 0) - Use
COALESCEfor 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
Calculated Metrics
Create reusable KPIs with token-based expressions and nested references
Calculated Dimensions
Build custom groupings with row-level SQL expressions and grain mappings
Choosing the Right Type
Understand when to use a calculated metric, dimension, or inline expression
Inline Expressions
Define simple transforms and display formatting directly on dataset metrics