Loading the documentation…

Calculated columns

A calculated column is a column you add to a BI table inside a semantic model. Its value is a ClickHouse SQL expression over the warehouse columns of the same BI table, worked out every time a report runs a query; nothing is stored in the warehouse. Use one for a value per row that the warehouse table does not have, such as a margin, a band or a cleaned-up code.

Before you start

Create a calculated column

  1. Open the semantic model. In the BI tables panel, right-click the BI table and choose New calculated column. In a report, see Create one from a report.
  2. The New calculated column dialog opens. Its subtitle reads "A column of <table> computed from its warehouse columns on every query."
  3. Enter a Name, for example Margin. Expressions and measures refer to the column by this name, and it cannot change later. Reports show the same text; to show something else, change the column's label after you save it.
  4. Write the Expression. Use the table's warehouse columns in double quotes, without a table name. The hint under the editor reads "Use the warehouse columns of <table> without a table name, for example "Amount" - "Cost". Ctrl+Enter saves." Suggestions offer the table's warehouse columns, and a counter shows the size, such as 24 / 8,192 bytes.
  5. Click Preview to run the expression on 20 rows and see its type. This step is optional.
  6. Click Save, or press Ctrl+Enter in the editor. The toast reads Calculated column "<name>" created. Cancel, or Escape in the editor, closes the dialog without saving.

A problem with the name shows under Name once you leave the field. A refused save shows its reason in red under the expression; when DataPrism ran the expression to check it, an Executed SQL section shows the statement it ran.

An expression can be as simple as "Amount" - "Cost", or sort rows into bands:

sql
multiIf("Amount" >= 1000, 'Large', "Amount" >= 100, 'Medium', 'Small')

Create one from a report

You can add a calculated column without leaving the report you are building.

  1. Open the report and choose Edit.
  2. In the report's BI tables panel, right-click the BI table and choose New calculated column.
  3. Fill in the dialog as above, preview the expression if you like, and click Save.

The column joins the semantic model, so every report on the model can use it. It appears in the panel under its BI table, ready to drag into a visual.

Write the expression with AI

The New calculated column and Edit calculated column dialogs carry an AI control beside Expression: describe the value, and the AI writes the expression for you to review.

  1. Click the AI calculated column control, or press Ctrl+Shift+M in the expression editor (⇧⌘M on a Mac). Its tooltip reads "AI calculated column - describe it, AI writes the expression".
  2. Describe the value you want for each row, for example Age band of the customer: under 30, 30 to 49, 50 and over..., and click Generate. The popover carries the column's name, or AI calculated column before you enter one.
  3. The AI replaces the expression in the editor.
  4. Check the result with Preview, then click Save. Nothing is saved until you do.

Fix the expression with AI

Fix with AI appears beside the reason in three places:

Click it. The AI reads the expression, the reason and the Executed SQL when there is one, and rewrites the expression in the editor; while it works, the button reads Fixing.... The toast AI updated the expression - review and save confirms it and adds the AI's notes when there are any. Run Preview, then click Save. If the AI cannot help, the message reads Failed to fix the calculated column expression and the expression stays as it was.

When a column is red for a reason outside its expression, such as a re-check that has not run yet or a warehouse column that now has its name, use Re-check or delete the column instead (When a calculated column turns red).

Preview and the detected type

The type is detected from the expression each time you save. A calculated column returns a number, text, a date, a date-time, a time, a duration or a true/false value, and starts with that BI type's default summarization and format. Lists, maps, tuples and other composite results are refused. When an edit changes the type, the summarization and the format go back to the new type's defaults.

Edit a calculated column

  1. In the BI tables panel, right-click the calculated column and choose Edit expression. The Edit calculated column dialog opens.
  2. Name is read-only and reads "Name cannot change". Change the Expression, preview it if you like, and click Save. The toast reads Calculated column "<name>" updated., and visuals open on the model run again.
  3. If someone else saved the column after you opened it, the dialog This calculated column was changed by someone else. asks you to choose. Reload loads their version; Overwrite saves yours on top.

Each save checks the expression again, even when it did not change. When measures use the column, they are checked against the new expression too: a measure that now fails turns red, and one that works again is cleared.

Everything else about the column is set like any other column: its label, folder, description, format, summarization, Sort by and In report, from the column toolbar in the BI tables panel or in the BI table popup (BI table columns). The popup's Expression column shows the expression read-only, with "Edit the expression with Edit expression in the BI tables tree".

Delete a calculated column

  1. Right-click the calculated column and choose Delete column.
  2. The confirmation Delete calculated column? reads This will permanently delete "<name>". This action cannot be undone. and says what the delete affects: Measures that turn red: <names>. or No measure turns red., and <n> charts in <m> reports use it and stop working until a column named "<name>" exists again. or No chart uses it.
  3. Click Delete. The toast reads Calculated column "<name>" deleted., and adds These measures are red now: <names>. when measures broke.

Create and change calculated columns with Model AI

Model AI works with calculated columns under the same rules as the dialogs, and a refusal names the rule.

The cards, approvals and status lines are on Model AI.

Where you can use it

A calculated column works like any other column of its BI table:

The BI table's preview lists working calculated columns after the warehouse columns, and View SQL on a visual shows the expressions it computes.

Rules for the expression and the name

What is not supported

These are refused with a sentence that names the rule:

Not supportedUse instead
Columns of other BI tables, or a lookup in a related tableA measure, which reads across the tables joined to the fact table, or the value added to the warehouse table.
Aggregates such as sum or count, and window functions (OVER)A measure.
Other calculated columns, and measuresRepeat the expression, or use a measure.
Subqueries, table functions, references to tables, dictionaries, SETTINGS, aliases, * and COLUMNS(...)Name the warehouse columns directly.
Pipelined SQL (|>) and FROM-first queries, and IN with a list of columnsOne SQL expression for the column, comparing values with = and OR.
Random and other non-repeatable functions, arrayJoin, sleep, throwIf, and user-defined functionsBuilt-in functions that give the same result for the same row.
A list, map, tuple or other composite resultOne value per row.

A calculated column also cannot be:

When you need one of these, add the value to the warehouse table instead, for example as a computed column. It then reaches the BI table as an ordinary warehouse column.

Conversions: use the OrNull forms

An expression must not fail on one bad row. A function that stops with an error on a value it cannot convert is refused when a form that does not fail exists, and the refusal names that form, for example toInt32 fails on text that is not a valid value, which is not allowed in calculated columns; use toInt32OrNull instead.

Instead ofWrite
toInt32("Code") or toDate("Day") over texttoInt32OrNull("Code"), toDateOrNull("Day")
CAST("Code" AS Int32) or "Code"::Int32 over textaccurateCastOrNull("Code", 'Int32')
toDecimal64("Amount", 2), or a CAST to Decimal, Enum or FixedStringaccurateCastOrNull("Amount", 'Decimal(18, 2)')
intDiv("Total", "Units") or "Total" % "Units"intDivOrZero("Total", "Units"), moduloOrZero("Total", "Units")
intDiv("Total", "Units") over fractional valuesfloor("Total" / "Units")
Text compared with a number or a date, such as "Code" = 5Convert the text first: accurateCastOrNull("Code", 'Int64') = 5

When a calculated column turns red

A calculated column that stops working is never deleted. It turns red instead: the fx badge carries a red dot, and pointing at it shows the reason. The column's properties add a Broken row with the same reason.

While it is red:

ReasonWhat to do
column "<X>" is no longer a warehouse column of BI table "<T>"A column the expression reads was dropped or renamed in the warehouse. Edit the expression, or bring the column back.
this calculated column was not re-checked after the warehouse changed BI table "<T>"; re-check itA column it reads changed type or came back, or a clash with a warehouse column went away. DataPrism re-checks it in the background and clears the reason when the check passes; choose Re-check to do it now.
this calculated column was not re-checked after BI table "<T>" was replaced; re-check it, or the same sentence after a reverted replacement or a deleted BI tableThe change could not check the column in time. The background re-check clears it, or choose Re-check.
a warehouse column of BI table "<U>" now has this name; delete this calculated column to use the warehouse columnA warehouse column with the same name arrived; see below.
the warehouse view behind BI table "<U>" now has a column with this name; delete this calculated columnThe warehouse table behind a BI table now has a column with this name. Delete the calculated column and create it under another name.
the expression failed against BI table "<T>": <message>The expression no longer runs against the table, for example after a column changed type. Edit the expression.

Fix a red calculated column

  1. Open the model and click Health. The finding for the column carries Fix, with the tooltip "Edit the expression of <name>".
  2. Click Fix. The Edit calculated column dialog opens.
  3. Correct the expression, or click Fix with AI in the notice at the top of the dialog. Check it with Preview, and click Save. A passing save clears the red state, and the measures that were red only because of the column work again.

When the cause is already gone, for example the warehouse column is back, right-click the column and choose Re-check. The item appears only while the column is red. The toast reads "The calculated column works again." when it passes, or names the reason when the column still fails or could not be checked. More on the panel: Check, document and trace a model.

When the warehouse brings a column with the same name

Names are unique in a model, so a warehouse column that arrives with a calculated column's name is a clash. The update from the warehouse never fails because of it:

Actions that would bring such a name are refused instead:

Table replacement, model files and history

Limits

LimitValue
Expression size8,192 bytes. Above it the counter turns red and Preview and Save are disabled.
Calculated columns per BI table50. One more is refused with BI table "<T>" already has 50 calculated columns, the most a BI table can have.
Name128 characters
Preview20 rows, showing up to 10 of the columns the expression reads
A visual's query that computes calculated columns1,400 KB. Above it the visual answers this query is <n> KB, over the 1,400 KB limit, because of the calculated columns it computes; use fewer calculated columns in this visual or shorten their expressions.

The other model capacities are on Limits and capacities, and every message on this page is listed in Troubleshooting.

Permissions

Questions, answered

Should I use a calculated column, a measure or a warehouse computed column?

Use a calculated column for a value per row from one BI table that you want to group, slice, filter or sort by. Use a measure for totals, ratios and anything that adds up rows or reads several tables: a calculated column that names a column of another BI table is refused, and the refusal names the table that holds it. Use a warehouse computed column when the value should be stored, or seen by tools outside DataPrism.

Why is there no AI control in the calculated column editor?

The control appears for people with Use DataPrism AI who can edit the semantic model, and it is off while a save runs. Fix with AI appears only beside a refused save, a failed preview or the notice of a red column. Ask an administrator for Use DataPrism AI if you lack it (Who can do what in DataPrism).

Why was my toInt32 or CAST refused?

The conversion would stop with an error on a value it cannot convert, and a form that does not fail exists. Use the form the message names, such as toInt32OrNull("Code") or accurateCastOrNull("Code", 'Int32'); it returns an empty value for that row instead.

Can I rename a calculated column?

You can change its label, which is what reports show. The label starts as the name; change it with Rename on the column's menu or from the column toolbar (BI table columns). The name is fixed. To use another name, create a new calculated column with the same expression and delete the old one; charts and measures that used the old name stay broken until a column with that name exists again or you point them at the new one.

Why did my calculated column turn red after a warehouse change?

A column it reads was dropped, renamed, retyped or brought back. After a type change or a return, DataPrism re-checks the column in the background and clears the red state when the check passes. After a drop or a rename, edit the expression. Hover the fx badge to read which applies, or choose Re-check.

Does a calculated column slow down my reports?

A visual that uses no calculated column runs the same query as before. A visual that uses one computes the expression over the rows it reads, so a heavy expression on a large table costs time in those visuals only.

Can I use a calculated column in row-level security or as a relationship key?

No. Row-level security, relationship keys and the Type and version columns use warehouse columns only, and the editors for them do not list calculated columns. Add the value to the warehouse table if you need it there.

Can Model AI create, change or delete calculated columns?

Yes. It previews a draft expression on 20 rows, creates up to 12 calculated columns in one change with their settings, changes expressions and settings, re-checks red columns and deletes columns, under the same rules as the editor. Changing the expression of a column that something uses, and deleting one, wait for your approval unless the mode is Unrestricted, and every change can be reverted. It does not create calculated columns during a Model Builder round from the Warehouse Builder. See Model AI.

Why does a failing visual only show an error code?

On a model with row-level security turned on, a visual that uses a calculated column and fails shows only Query failed - contact an administrator (ClickHouse error code N). Open the column in the model editor and run Preview, which shows the full error.

Why do today() and now() show an earlier time?

They return the time the query ran. A visual that is answered from a recent cached result shows the time that result was computed.

What does an expression read in a Tokenized PII model?

The same values as the rest of the model. In a Tokenized PII model, an expression over a PII column computes over the tokenized stand-ins, not the original values (Concepts and glossary).

Why can't I use the name I want?

Names are unique across the whole model: the name may not belong to any column of any BI table, a measure or a BI table, or to a column of a warehouse table behind one of the model's BI tables. It also cannot be a name a row-level security rule names, or a key column of a relationship uses, because those rules and joins find their column by name. The refusal names the holder, for example name "Region" is already a column of BI table "Customers", name "Region" is used by row-level security group "Europe"; pick another name or name "Region Key" is a key column of a relationship of BI table "Sales"; pick another name. Pick another name and use the label for what reports show.