Skip to main content
Calculated fields let you derive new metrics and dimensions at query time without modifying the data model. You can create them by pointing and clicking in the workbook UI, asking the AI agent, or writing Semantic SQL manually. Unlike other analytics tools where custom fields are computed in the browser, Cube compiles calculated fields into SQL and pushes computation down to your data warehouse. This means calculated fields have no frontend row limits, can operate on the full dataset, and benefit from the same query optimization as any other warehouse query.

Creating calculated fields

Point and click

In a Semantic Query tab, you can create calculated fields directly from the workbook UI:
  1. Open a workbook and select a Semantic Query tab.
  2. Click the + button next to the measures or dimensions list to add a calculated field.
  3. 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.
  4. Give the calculated field a name and run the query.
The calculated field appears alongside your other query results and can be used in charts, pivots, and filters within the same workbook.

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”
The agent generates the query, including any calculated fields, and returns the results. You can then explore the results further or save the query to a workbook.

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.
SELECT
  status,
  total_revenue,
  total_orders,
  total_revenue / total_orders AS avg_order_value
FROM (
  SELECT
    status,
    MEASURE(total_revenue) AS total_revenue,
    MEASURE(count) AS total_orders
  FROM orders
  GROUP BY 1
) AS data
In this example, 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.
The inner query still passes through the semantic layer with full governance, access control, and pre-aggregation support. The outer calculation runs on top of those governed results.

Examples

Ratios and percentages

Calculate a completion rate by dividing one measure by another:
SELECT
  category,
  completed_orders,
  total_orders,
  ROUND(100.0 * completed_orders / total_orders, 1) AS completion_rate
FROM (
  SELECT
    category,
    MEASURE(completed_count) AS completed_orders,
    MEASURE(count) AS total_orders
  FROM orders
  GROUP BY 1
) AS data

Conditional logic

Use CASE expressions to create categorical fields:
SELECT
  customer_name,
  lifetime_value,
  CASE
    WHEN lifetime_value >= 10000 THEN 'Enterprise'
    WHEN lifetime_value >= 1000 THEN 'Mid-market'
    ELSE 'Small'
  END AS customer_tier
FROM (
  SELECT
    name AS customer_name,
    MEASURE(lifetime_value) AS lifetime_value
  FROM customers
  GROUP BY 1
) AS data

Arithmetic combinations

Combine multiple measures to calculate profit and margin:
SELECT
  product_name,
  revenue,
  cost,
  revenue - cost AS profit,
  ROUND(100.0 * (revenue - cost) / revenue, 1) AS margin_pct
FROM (
  SELECT
    name AS product_name,
    MEASURE(total_revenue) AS revenue,
    MEASURE(total_cost) AS cost
  FROM products
  GROUP BY 1
) AS data

Calculated fields vs. data model measures

Calculated fieldsData model measures
Defined inQuery (UI, AI agent, or Semantic SQL)Data model (YAML or JavaScript)
ScopeSingle query or reportAvailable across all queries
ExecutionCompiled to SQL, runs on the warehouseCompiled to SQL, runs on the warehouse
Pre-aggregation supportOuter calculations are not pre-aggregatedFully supported
Access controlInherits from underlying membersManaged in the data model
Best forAd-hoc analysis, prototyping metricsReusable, governed metrics
When you find yourself reusing the same calculated field across multiple queries, consider promoting it to a calculated measure or dimension in your data model. This gives you pre-aggregation support, access control, and discoverability across your team.