Calculated Measures
Why calculated measures exist
Business reports are full of numbers that are not stored anywhere — gross margin percent, year-on-year growth, average order value, sell-through rate. Each one is built from two or three measures that are stored. When every BI tool computes these on its own, every tool gets a slightly different answer, and the analyst ends up policing definitions in spreadsheets instead of answering questions.
A calculated measure is Tessallite's way of defining those derived numbers once, in the model, so every tool — dashboards, notebooks, pivot tables, agents — reads the same definition. If finance renames "gross margin percent" from a SUM to a safe-division ratio, they change it in one place and the next query everywhere reflects it.
This page walks through: what a calculated measure is, the small expression language, the aggregation-mode choice (the decision most new users get wrong the first time), validation behaviour, and a worked example you can copy.
A calculated measure, in one sentence
A calculated measure is a named expression over other measures, stored on the model, that the Query Router evaluates on every query.
It has no source column of its own. It borrows the values of the measures it references, combines them with arithmetic and a small set of safe functions, and returns a single number per cell — just like a plain measure.

Figure 1 — Creating a calculated measure. The green chips under the expression confirm both referenced measures were found; the validation pill confirms the expression parsed cleanly. Full description: calculated-measure-form.txt.
Before you start
- The model must already contain the base measures you want to combine. Calculated measures reference other measures by
name— if you plan to writesafe_div(measure("margin"), measure("sales")), bothmarginandsalesmust exist first. See Define Measures. - Decide the aggregation mode in advance. This is the "think about this for a minute" decision — the same expression in the two modes typically produces different numbers. Skip ahead to Aggregation mode if you are unsure.
- If the business question is truly row-level (for example, "profit per order where order is a row on the fact table"), a calculated measure may be the wrong tool — consider whether a plain measure over a derived column is simpler.
Expression language
The expression combines existing model measures with arithmetic and allowed scalar functions. Reference a measure with measure("name"); the governed services resolve it and calculate the result. Mathematical functions include roots, logarithms, trigonometry and hyperbolic functions. A deployed measure uses the same definition in JDBC, native Excel, the Excel add-in and the Conversational Agent.
The source must support the requested function or its exact SQLGlot translation. random() and random_normal() are query-time operations, so they are not saved model-field definitions. Persistent seed controls are not offered.
| Construct | Example | When to use it |
|---|---|---|
| Reference another measure | measure("sales") | Pulls the value of another measure in the same model. Use the measure's name, not its display_name. |
| Safe division | safe_div(measure("margin"), measure("sales")) | Division that returns NULL when the denominator is zero, so a ratio on a row with no sales does not blow up the whole query. |
| Safe ratio | safe_ratio(measure("a"), measure("b")) | Alias for safe_div. Use whichever reads more naturally. |
| Arithmetic | measure("a") + measure("b"), measure("a") * 1.1 | Addition, subtraction, multiplication, division, remainder (%), powers (^), unary signs and parentheses. Use parentheses to make the intended order clear. |
| Numeric literals | 0.5, 100, 1e-3 | Only numeric literals are accepted. No string, no date, no boolean literals. |
Mathematical examples
The examples below use expression_as_written: combine the input rows first, then apply the function. Replace each measure name with one in your model. The sample inputs illustrate the calculation; they are not expected values for your data.
| Purpose | Expression | Sample input and result |
|---|---|---|
| Ignore an adjustment's sign | abs(measure("adjustment")) | An adjustment of -12 becomes 12. |
| Round a number | round(measure("sales"), 2) | Sales of 12.345 become 12.35 for a decimal input. |
| Take a square root | sqrt(measure("units")) | Units of 81 give 9. Negative inputs have no real square root. |
| Raise a number to a power | power(measure("units"), 2) | Units of 9 give 81. measure("units") ^ 2 expresses the same power. |
| Use a base-10 logarithm | log(10, measure("sales")) | Sales of 1000 give 3. The input must be positive. |
| Calculate a sine in degrees | sind(measure("angle")) | An angle of 30 degrees gives 0.5. sin() expects radians instead. |
| Put a value into an equal-width bucket | width_bucket(measure("sales"), 0, 100, 5) | Sales of 45 fall in bucket 3, covering values from 40 up to, but not including, 60. |
Choosing when to calculate matters. With input rows 3 and 4, squaring their total gives (3 + 4) ^ 2 = 49; squaring each row and adding gives 3 ^ 2 + 4 ^ 2 = 25. Choose the aggregation mode for the business meaning, not just the desired appearance. See Aggregation mode.
Use safe_div() when a zero denominator should produce an empty result rather than fail the calculation. Rounding is different from formatting: round() changes the calculated value; a display format changes only how it is shown.
Expression limits
Things the expression language deliberately does not support:
- Column references. You cannot write
orders.price * 0.9— wrappricein a plain measure first. - Functions outside the accepted scalar catalogue, inline aggregates, window functions and subqueries.
- Dimension references. Calculated measures are measure-over-measure; slicing by dimension happens at query time, not in the expression.
- References to a time variant (
measure("revenue_prior_year")). A time variant has a value only where a report gives it a period, so the save is refused. Write that calculation in the report instead: a SQL expression such asSUM(revenue) - SUM(revenue_prior_year)grouped by a period, or a calculated measure in Excel. See What a time variant can be used for.
If you find yourself wanting any of these, the solution is almost always a new base measure or a new column in the source, not a bigger expression.
Aggregation mode
Every calculated measure carries a calc_agg_mode flag. It controls when the expression is evaluated relative to the GROUP BY. The choice is small on paper and enormous in practice.

Figure 2 — Why aggregation mode matters. Same expression, same rows, dramatically different answers. Full description: calculated-measure-agg-mode-compare.txt.
| Mode | How it evaluates | Reads as |
|---|---|---|
expression_as_written | Aggregate first, then apply the expression once on the aggregated values. | safe_div(SUM(margin), SUM(sales)) — overall margin percent for whatever is being sliced. |
per_row_then_aggregate | Apply the expression per input row, then aggregate the result with the default aggregation (usually SUM). | SUM(margin / sales) — a sum of per-row ratios, which is almost never what the business wants for a percentage. |
Rule of thumb for ratios and percentages: use expression_as_written. It gives the overall ratio, which is what a finance or marketing reader expects when they see "Gross Margin %" in a report.
When per_row_then_aggregate is right: when the expression truly is an additive quantity per row — for example, measure("base_price") - measure("discount") expressed as a calculated measure rather than a column. The per-row subtraction followed by a SUM gives net revenue; evaluating SUM(base_price) - SUM(discount) happens to give the same number, so the mode does not matter for simple addition and subtraction. The modes only diverge on division, multiplication, and non-linear operations.
The mode is stored on the measure. The rewriter always applies the same mode, so the answer is deterministic across the canvas pivot, BI tools, and drill-through.
Validation and what happens when things break
The expression is parsed at save time. Errors are reported inline, next to the expression editor, so the measure cannot be saved in a broken state. There are three common failure shapes:
- Syntax error — a mistyped function name, an unbalanced parenthesis. The editor underlines the position and offers a message like
Unexpected token at position 23. - Unknown reference —
measure("gross_margin")where no measure namedgross_marginexists. The editor flags the chip red and offers a "did you mean" suggestion if a close name exists. - Cycle — a chain like
A → B → A, whereBreferencesAwhich referencesB. Caught at save time. The validator names both measures in the error so the loop is obvious.
Deleting or renaming a base measure that a calculated measure depends on marks the calculated measure as invalid rather than deleting it. Invalid measures:
- Are hidden from the BI catalogue, so a downstream dashboard will not silently show zeros.
- Keep their expression so you can see what they were trying to do.
- Re-enable automatically the moment the reference is fixed — either edit the expression to point at the new name, or rename the base measure back.

Figure 3 — An invalid calculated measure. No data has been lost; the expression still parses, but one reference no longer resolves. Fixing the name re-enables the measure. Full description: calculated-measure-invalid-chip.txt.
Drill-through on calculated measures
A calculated measure has no single source column, so it cannot drill to a single fact-table query. The Measure Query Panel opens a decomposed drill drawer that runs one drill-through per referenced base measure and shows the mini-panels stacked. The calculated value sits read-only at the top of the drawer, and each base-measure panel paginates independently.
This means a cell like "Gross margin % = 31.4%" drills into "the rows behind margin" and "the rows behind net_sales" side by side — the analyst can see both the numerator and the denominator behind the ratio without leaving the page.
Worked example — Gross margin percent
A minimal, copyable recipe for the single most-requested calculated measure.
Context. The model already has two base measures: gross_margin (SUM of the margin column on the orders fact) and net_sales (SUM of net_sales on the same fact).
Goal. Add a calculated measure gross_margin_pct that reads as a percentage and always shows the overall margin at whatever grain the report asks for — by month, by region, by product, or overall.
Steps.
- Open the model in Model Builder → Measures.
- Click Add Measure. Switch Measure type to Calculated.
- Fill the form:
- Name:
gross_margin_pct - Display name:
Gross margin % - Expression:
safe_div(measure("gross_margin"), measure("net_sales")) - Calc aggregation mode:
expression_as_written - Format:
percent_2dp
- Confirm both referenced-measure chips under the expression are green. If one is red, the name is misspelled — fix it before saving.
- Click Save to retain the model change, then Deploy to publish it to JDBC, Excel and other query clients. Saving alone does not change the deployed model.
- Open the Measure Query Panel, pick
Gross margin %as the measure andregionas the row dimension, and click Run. Check each returned ratio against that region's gross margin and net sales. For example, a margin of25and sales of100give25%; a zero denominator gives an empty result withsafe_div(). - Click any cell to open the decomposed drill drawer. You will see two mini drill-through panels — one for the numerator rows, one for the denominator rows — each paginating at 50 rows per page.
Steps (reference)
- Open a model in Model Builder and click Measures in the Toolbelt.
- Click Add Measure and set Measure type to Calculated.
- Enter a Name and Display name.
- Write the expression in the Expression field. The form validates as you type; referenced measures are shown as chips below the editor.
- Choose a Calc aggregation mode — see the table above.
- Optionally set a Format (for example
percent_2dpfor a ratio). - Click Save, then Deploy to publish the change to query clients.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Red chip under the expression | Typed the display_name instead of name | Use the measure's name (lowercase, underscore) |
Measure saves but the pivot shows NULL everywhere | Denominator is always zero at the chosen grain | Check the base measure; safe_div intentionally returns NULL on zero |
| Mode switch changes every cell's value | Expected — the two modes are not interchangeable for ratios | Pick expression_as_written for percentages and ratios |
| "Invalid" badge appears after someone renamed a base measure | Reference no longer resolves | Edit the expression to use the new name, or rename the base measure back |