|
| 1 | +--- |
| 2 | +title: Calculated fields |
| 3 | +description: Create ad-hoc custom dimensions and measures with Semantic SQL in workbooks, with help from AI or the field picker. |
| 4 | +--- |
| 5 | + |
| 6 | +Calculated fields are ad-hoc dimensions and measures you add only to the current |
| 7 | +workbook report. They do not change the shared data model. |
| 8 | + |
| 9 | +As described in [Semantic SQL](/docs/introduction#semantic-sql), Cube routes |
| 10 | +analysis through the semantic layer instead of sending arbitrary SQL straight to |
| 11 | +the warehouse. The runtime validates every request and applies your security |
| 12 | +policies. Semantic SQL builds on Postgres-compatible SQL—including the |
| 13 | +`MEASURE()` function—so you can express derived logic on top of existing |
| 14 | +semantic definitions with both flexibility and governance. |
| 15 | + |
| 16 | +Calculated fields are expressed as Semantic SQL and pushed down to the Cube |
| 17 | +backend for evaluation. The semantic layer compiles them with the rest of the |
| 18 | +query—rather than applying them only in the browser—so the same validation, |
| 19 | +governance, and warehouse execution path apply as for any other Semantic SQL |
| 20 | +analysis. |
| 21 | + |
| 22 | +## Using AI to create calculated fields |
| 23 | + |
| 24 | +You can ask the Cube AI agent to create custom calculations in natural language. |
| 25 | +The agent can add or refine calculated fields from different parts of the |
| 26 | +product—for example while exploring in **Analytics chat** or working in |
| 27 | +**Workbooks**—so you are not limited to a single entry point when you want a new |
| 28 | +metric or dimension for the analysis in front of you. |
| 29 | + |
| 30 | +## Creating calculated fields in UI |
| 31 | + |
| 32 | +You can also build and edit calculated fields directly in the workbook. New |
| 33 | +fields appear in the **Calculated fields** section of the field picker sidebar. |
| 34 | + |
| 35 | +### Aggregations from existing dimensions |
| 36 | + |
| 37 | +Right-click a dimension column header and choose an aggregation to create a |
| 38 | +calculated field automatically. Available aggregations depend on the column type: |
| 39 | + |
| 40 | +| Column type | Available aggregations | |
| 41 | +| --- | --- | |
| 42 | +| Number | Count Distinct, Sum, Average, Min, Max | |
| 43 | +| Time | Count Distinct, Min, Max | |
| 44 | +| String, Boolean | Count Distinct | |
| 45 | + |
| 46 | +### Calculations from existing measures |
| 47 | + |
| 48 | +Open the menu on a measure column header and use the **Calculations** submenu |
| 49 | +for derived calculations: |
| 50 | + |
| 51 | +| Calculation | Description | |
| 52 | +| --- | --- | |
| 53 | +| % of total | Ratio of the measure value to the total across all rows | |
| 54 | +| % of previous | Ratio of the measure value to the previous row's value | |
| 55 | +| % change from previous | Percentage change compared to the previous row | |
| 56 | +| Running total | Cumulative sum of the measure across rows | |
| 57 | + |
| 58 | +<Info> |
| 59 | + |
| 60 | +**% of previous**, **% change from previous**, and **Running total** require at |
| 61 | +least one dimension in the query. |
| 62 | + |
| 63 | +</Info> |
| 64 | + |
| 65 | +Which calculations are offered depends on the measure’s aggregation type: |
| 66 | + |
| 67 | +| Aggregation type | Available calculations | |
| 68 | +| --- | --- | |
| 69 | +| Count, Sum | All calculations | |
| 70 | +| Min, Max | Running total | |
| 71 | +| Average, Count Distinct | None | |
| 72 | + |
| 73 | +### Filtered measures |
| 74 | + |
| 75 | +When working with query **Results**, pivot so at least one dimension is on |
| 76 | +columns, then open the header menu on a **pivoted measure column** and choose |
| 77 | +**Create filtered measure**. Cube adds a calculated measure that applies the |
| 78 | +column’s slice—for example, from **Count** broken down by **Status**, you get a |
| 79 | +measure that only aggregates rows matching that status (such as completed |
| 80 | +orders only). |
| 81 | + |
| 82 | +The option appears only for **native** measures on pivoted columns, not for |
| 83 | +calculated fields. The same flow works in **Explore** when results are pivoted |
| 84 | +the same way. |
| 85 | + |
| 86 | +### Editing a calculated field |
| 87 | + |
| 88 | +Select a calculated field in the sidebar to open the editor. You can change its |
| 89 | +**name** and **SQL expression**, then choose **Update** to apply. |
0 commit comments