Calculated Dimensions
Build custom groupings with row-level SQL expressions and grain mappings
Calculated dimensions are row-level computed fields that produce grouping values. Unlike calculated metrics (which aggregate numbers into KPIs), dimensions transform each row before any grouping happens. The result appears in the ROWS or COLUMNS area of a card.
Use them to bucket orders by size, truncate dates to months or quarters, combine text fields, or apply any row-level logic that your raw data doesn't already have.
Creating in the Explorer
The Explorer lets you create calculated dimensions by writing a SQL expression with field references.
Steps
- Open the Explorer and click + Add Calculated Field
- Select Calculated Dimension
- Enter a name (e.g., "Order Size Bucket")
- Write your SQL expression using
{FieldName}tokens to reference fields - Click Validate Expression to check for errors
- Select the output type (String, Number, or Date)
- Click Create Dimension
Expression Syntax
Use {FieldName} tokens as placeholders for field references. The tokens are replaced with fully-qualified column names at query time.
CASE WHEN bucketing:
Date truncation:
Combining text fields:
Handling nulls:
Example: Order Size Bucket
- Expression:
CASE WHEN {Amount} > 1000 THEN 'Large' WHEN {Amount} > 100 THEN 'Medium' ELSE 'Small' END - Output Type: String
- Result: Each row is categorized as Large, Medium, or Small based on its amount
Expression syntax varies by database. Semaphor validates expressions against your connection's SQL dialect (PostgreSQL, MySQL, BigQuery, etc.).
Not sure if you need a calculated dimension or a calculated metric? See Choosing the Right Type.
Defining in a Template
For version-controlled definitions, bulk configuration, or CI/CD workflows, define calculated dimensions in the calculatedDimensions section of your domain template.
Expression Syntax
Expressions use {input_name} tokens as placeholders for field references. The tokens are replaced with fully-qualified SQL at query time.
Two Input Types
Column reference -- points to a physical column in a dataset:
Dimension reference -- points to another calculated dimension (enables nesting):
Calculated dimensions cannot reference metrics. If you try, validation fails with error E007.
Common Patterns
Date truncation:
CASE WHEN bucketing:
Combining text fields:
Handling nulls:
Grain Mappings
Grain mappings tell Semaphor how a calculated dimension maps to columns in other datasets. They are needed when a calculated metric references fields from multiple datasets and you group by a calculated dimension.
The problem: If you define order_month as DATE_TRUNC('month', orders.order_date), Semaphor needs to know what column in the returns dataset represents the same time grain.
When are grain mappings needed?
Only when all three conditions are true:
- You have a calculated metric that spans multiple datasets (cross-dataset metric)
- You're grouping by a calculated dimension
- The dimension's time grain needs to be applied to columns in other datasets
Example: Net Revenue (orders.amount - returns.amount) grouped by Order Month requires a grain mapping so Semaphor knows to use DATE_TRUNC('month', returns.return_date) for the returns dataset CTE.
Without the grain mapping, Semaphor wouldn't know how to align the returns data to the same monthly grain as orders.
Field Reference
| Field | Type | Required | Description |
|---|---|---|---|
label | string | Yes | Display name |
description | string | No | Help text |
expression | string | Yes | SQL expression with {input} tokens |
inputs | object | Yes | Map of input name to column or dimension reference |
dataType | enum | No | string, number, boolean, date, datetime |
grainMapping | object | No | Maps dimension to columns in other datasets (see Grain Mappings) |