Creating calculated fields
Point and click
In a Semantic Query tab, you can create calculated fields directly from the workbook UI:- Open a workbook and select a Semantic Query tab.
- Click the + button next to the measures or dimensions list to add a calculated field.
- Define your calculation using the available measures and dimensions. The UI provides an expression editor where you can combine fields with arithmetic operators, functions, and conditional logic.
- Give the calculated field a name and run the query.
AI agent
The AI agent in Analytics Chat and in workbook Semantic Query tabs can create calculated fields for you automatically. Ask a question that requires a derived metric, and the agent writes the appropriate Semantic SQL with post-processing to compute the result. For example:- “Show me the profit margin for each product category”
- “What percentage of orders are completed vs. total orders?”
- “Add a column that classifies customers as enterprise, mid-market, or small based on their lifetime value”
Semantic SQL
For full control, write Semantic SQL directly in the SQL editor of a workbook or Explore. Calculated fields use query post-processing: you write an inner query that selects measures and dimensions from your data model, then wrap it in an outer query that performs additional calculations.avg_order_value is a calculated field. The inner query
retrieves semantic model measures using the MEASURE() function, and the outer
query computes the derived value.
How it works under the hood
When you create a calculated field—whether through the UI, the AI agent, or by writing SQL—Cube compiles the entire query, including your calculated expressions, into a single SQL statement that runs on your data warehouse. This backend execution model has key advantages over tools that compute custom fields in the browser:- No row limits. Frontend-calculated fields are limited to the rows fetched to the browser (typically a few thousand). Cube’s calculated fields operate on the full dataset in the warehouse.
- Warehouse-native performance. Your data warehouse optimizes and parallelizes the computation. Complex calculations over millions of rows complete in seconds rather than stalling the browser.
- Consistent results. Because the calculation happens on the full dataset before any row limit is applied, aggregations and percentages reflect the complete data—not a truncated sample.
Examples
Ratios and percentages
Calculate a completion rate by dividing one measure by another:Conditional logic
UseCASE expressions to create categorical fields:
Arithmetic combinations
Combine multiple measures to calculate profit and margin:Calculated fields vs. data model measures
| Calculated fields | Data model measures | |
|---|---|---|
| Defined in | Query (UI, AI agent, or Semantic SQL) | Data model (YAML or JavaScript) |
| Scope | Single query or report | Available across all queries |
| Execution | Compiled to SQL, runs on the warehouse | Compiled to SQL, runs on the warehouse |
| Pre-aggregation support | Outer calculations are not pre-aggregated | Fully supported |
| Access control | Inherits from underlying members | Managed in the data model |
| Best for | Ad-hoc analysis, prototyping metrics | Reusable, governed metrics |