Formulas let a semantic query compute values that are not stored directly in the model. You write a formula as an expression string — combining field references, operators, literals, and functions — and the Semantic Query API evaluates it as part of the query. Formulas power calculated dimensions and calculated measurements: a calculated dimension groups or filters rows by a computed value, while a calculated measurement aggregates a computed value.
There are two ways to bring a formula into a query:
Model-defined calculated fields. The formula is authored once in the Semantic Data Model as a SemanticCalculatedDimension or SemanticCalculatedMeasurement, and the query references it by name through a semantic_field expression.
On-the-fly calculated fields. The formula is supplied directly in the request as a calculated_field expression, without being persisted to the model. Use this for query-scoped computations.
An expression is written in one of two formula dialects, selected per calculated field with the syntax property:
Tua ("Tua") — references fields with bracket notation [Table].[Field], and supports level-of-detail (LOD) expressions and table calculations.
Salesforce ("Salesforce") — references fields with dotted notation table.field.
When a calculated field omits syntax, the default depends on how the model is supplied with the request: a request that carries an inline semanticModel defaults to Tua, while a request that references the model by ID (with no inline model) defaults to Salesforce.
Both dialects share the same function library and operator set. A calculated field in one dialect can reference a model-defined calculated field authored in the other dialect.
Metadata in the model
Calculated fields can be defined at authoring time as part of the Semantic Data Model, so their formulas are reusable across queries. A calculated dimension is authored as a SemanticCalculatedDimension; a calculated measurement is authored as a SemanticCalculatedMeasurement. Each carries an apiName, a dataType, an expression, a syntax, and (for measurements) an aggregationType and a level. For the full authoring field schema, see Semantic Calculated Dimension and Semantic Calculated Measurement in the Authoring API.
To compute a value without persisting it to the model, place a calculated_field object inside a fields[] entry’s expression. A calculated_field has a name, an expression string, an optional syntax, and exactly one of calculated_measure_expression (for an aggregated numeric result) or calculated_dimension_expression (for a grouping value).
A calculated measurement declares its output type under calculated_measure_expression:
Expressions combine values with arithmetic, comparison, and logical operators. Operators evaluate in the following order, from highest to lowest precedence:
Parentheses ( )
Unary minus -
Exponentiation ^
Multiplication, division, modulo */%
Addition, subtraction +-
Comparisons ><>=<==!=
IN
NOT
AND
OR
Use parentheses to make grouping explicit. Arithmetic combines measures and literals — this on-the-fly measurement adds a constant to a model-defined calculated measurement:
ROUND(value, places) rounds to a given number of decimal places, and ABS(value) returns absolute value. This model-defined table calculation uses ABS in a period-over-period growth formula:
String functions build and transform text. CONCAT(...) joins strings, and TEXT(value) converts a value to text — combined here to format a fiscal-period label:
LEFT(text, count) returns the leftmost characters. STR(value) converts a value to a string, and text values concatenate with +. This model-defined calculated dimension formats a rounded score with a text suffix:
Date functions extract, truncate, and construct date and datetime values. DATE_TRUNC(part, datetime) truncates a datetime to a given part, and DATETIMEVALUE(...) produces a datetime value:
DATEPART(part, date) returns a numeric component of a date, such as the year. This calculated dimension extracts the year from another calculated field:
Fiscal date functions return fiscal-calendar components. FISCAL_YEAR(date) returns the fiscal year and FISCAL_QUARTER(date) returns the fiscal quarter for a date:
Conditional expressions branch on a condition. The IF ... THEN ... ELSEIF ... ELSE ... END form returns different values per branch; IN tests membership in a value list. This model-defined measurement returns an amount only for matching forecast categories:
ISNULL(expression) returns whether a value is null, and ZN(expression) converts a null numeric result to zero. The growth formula above uses ZN so that missing sales values participate in arithmetic as zero:
Aggregation functions collapse many rows into a single measurement value. The library includes SUM, MIN, MAX, AVG, COUNT, COUNTD (distinct count), and MEDIAN. COUNTD counts distinct values of a field:
When a calculated field wraps an aggregation, set the field’s aggregation method to SEMANTIC_AGGREGATION_METHOD_USER_AGG so the expression’s own aggregation is used rather than an outer one. Aggregate expressions also appear in Top-N criteria — here MEDIAN ranks dimension values:
Table calculations compute across the ordered result set — running totals, moving averages, rankings, and offset lookups. A calculated measurement authored at levelTableCalc carries a window function. WINDOW_AVG(expression, start, end) averages a measure over a sliding window of rows:
LOOKUP(expression, offset) returns a measure’s value from a row offset by a given number of positions — negative offsets look backward. The period-over-period formula uses LOOKUP(..., -1) to reference the prior row:
Table calculations use a PARTITION/ORDERBY window frame. The frame partitions rows by one set of columns, orders within each partition, and applies an analytics function. RANK(measure) assigns a rank within the ordered partition:
The ORDERBY clause accepts sort direction (ASC / DESC) and null placement (NULLS FIRST / NULLS LAST). Table-calculation expressions can also be used inside filters — this filter selects rows by an ordered index computed with an ORDERBY frame:
Identity functions return values about the current user. USERID() returns the running user’s ID; combine it with LEFT to compare on a 15-character ID prefix. This model-defined calculated dimension marks rows owned by the current user:
1{2 "apiName": "FFact_owner_opp_history_lv_clc",3 "label": "FFact owner - opp history lv",4 "dataType": "Boolean",5 "expression": "LEFT([SW_FF_OPP_HIS_PA_lv].[Forecasting_Fact10_Owner], 15) = LEFT(USERID(), 15)"6}
Referencing parameters in formulas
A formula can reference a model parameter. In Salesforce syntax, reference a parameter as Parameters.<name>; in Tua syntax, as [Parameters].[<name>]. This model-defined measurement multiplies a constant by a parameter value:
When syntax is omitted, the default is Tua if the request carries an inline semanticModel, and Salesforce if the model is referenced by ID with no inline model.
The same model can hold calculated fields in both dialects, and a formula in one dialect may reference a model-defined calculated field authored in the other. Here a Salesforce-syntax on-the-fly measurement references tua_model_calc, a model-defined measurement authored in Tua syntax:
You can annotate expressions with comments. Line comments (//) and block comments (/* */) are ignored during evaluation.
Reference
Field (wire name)
Type
Required
Description
calculated_field
CalculatedField
N
On-the-fly calculated field supplied in the request; holds name, expression, and an output type.
name
String
Y
Identifier for the calculated field within the request.
expression
String
Y
The formula, written in the field’s syntax dialect.
syntax
String (enum)
N
Formula dialect: "Tua" or "Salesforce". When omitted, defaults to "Tua" for requests with an inline semanticModel and "Salesforce" when the model is referenced by ID.
calculated_measure_expression
CalculatedMeasureExpression
Y*
Marks the field as a measurement; carries measure_output_type and optional decimal_places_options.
calculated_dimension_expression
CalculatedDimensionExpression
Y*
Marks the field as a dimension; carries dimension_output_type.
measure_output_type
String (enum)
Y*
Output type of a calculated measurement, e.g. SEMANTIC_MEASUREMENT_TYPE_NUMBER.
dimension_output_type
String (enum)
Y*
Output type of a calculated dimension, e.g. SEMANTIC_DIMENSION_TYPE_TEXT, SEMANTIC_DIMENSION_TYPE_DATE_TIME, SEMANTIC_DIMENSION_TYPE_NUMBER.
decimal_places_options
DecimalPlacesOptions
N
Sets decimal_places for a calculated measurement.
semanticField / semantic_field
SemanticFieldExpression
N
References a model-defined calculated field by name.
semantic_aggregation_method
String (enum)
N
Aggregation applied to the field, e.g. SEMANTIC_AGGREGATION_METHOD_SUM, _AVG, _MIN, _MAX, _USER_AGG, _AUTO, _NONE.
row_grouping
Boolean
N
When true, groups query rows by this (dimension) field.
A calculated field is either a measurement or a dimension, not both. Supply exactly one of calculated_measure_expression or calculated_dimension_expression per calculated_field.
Wrapped aggregations use USER_AGG. When an expression contains its own aggregation function (for example COUNTD(...) or a TableCalc-level formula), set the field’s aggregation method to SEMANTIC_AGGREGATION_METHOD_USER_AGG so the expression’s aggregation is honored.
Table calculations require an ordering. Window and ranking functions evaluate over an ordered frame; a table-calculation formula specifies its PARTITION and ORDERBY (or window bounds) so row order is well-defined.
Parameter reference notation is dialect-specific. Use Parameters.<name> in Salesforce syntax and [Parameters].[<name>] in Tua syntax.