Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions guides/formula-table-calculations.mdx
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
---
title: "Formula table calculations"
description: "Build table calculations using a spreadsheet-style formula syntax instead of raw SQL."
sidebarTitle: "Formula (Beta)"
sidebarTitle: "Formula"
---

<Info>
**Formula table calculations are in [Beta](/references/workspace/feature-maturity-levels).**

The feature is enabled by default on supported warehouses, with no
flag required, and we're actively expanding the function catalog.
We'd love your feedback while we iterate. See [feature maturity levels](/references/workspace/feature-maturity-levels)
for details.
</Info>

Formula table calculations let you write table calculations in a
spreadsheet-style syntax, the way you would in Google Sheets or Excel,
instead of raw SQL. When you create a new [table calculation](/guides/table-calculations)
on a supported warehouse, **Formula** is the default input mode. You
can switch to the SQL editor any time.
instead of raw SQL. When you create a new [table calculation](/guides/table-calculations),
**Formula** is the default input mode. You can switch to the SQL editor
any time.

<Frame>
<img src="/images/guides/formula-table-calculations/create-table-calculation-formula-light.png" alt="Creating a formula table calculation" className="block dark:hidden" />
Expand All @@ -37,20 +28,10 @@ editor is always one click away.

## Supported warehouses

| Warehouse | Formula support |
| ----------- | --------------- |
| BigQuery | ✅ |
| ClickHouse | ✅ |
| Databricks | ✅ |
| DuckDB | ✅ |
| PostgreSQL | ✅ |
| Redshift | ✅ |
| Snowflake | ✅ |
| Athena | 🚧 Not yet |
| Trino | 🚧 Not yet |

On warehouses where formulas aren't supported yet, the Formula toggle
is hidden and the SQL editor works exactly as it always has.
Formula table calculations work on every warehouse Lightdash supports:
Athena, BigQuery, ClickHouse, Databricks, DuckDB, PostgreSQL, Redshift,
Snowflake, and Trino. The same formula compiles to the correct SQL for
whichever warehouse your project is connected to.

## Writing your first formula

Expand Down Expand Up @@ -111,13 +92,21 @@ You can use:

### Date

| Function | Description |
| ------------ | --------------------- |
| `TODAY()` | Current date |
| `NOW()` | Current timestamp |
| `YEAR(d)` | Extract year |
| `MONTH(d)` | Extract month |
| `DAY(d)` | Extract day |
| Function | Description |
| --------------------------------- | -------------------------------------------------------------------------------------------- |
| `TODAY()` | Current date |
| `NOW()` | Current timestamp |
| `YEAR(d)` | Extract year |
| `MONTH(d)` | Extract month |
| `DAY(d)` | Extract day |
| `LAST_DAY(d)` | Last day of the month containing `d` |
| `DATE_TRUNC(d, unit)` | Truncate `d` to the start of `unit` (`"day"`, `"week"`, `"month"`, `"quarter"`, `"year"`) |
| `DATE_ADD(d, n, unit)` | Add `n` whole `unit`s to `d` (e.g. `DATE_ADD(orders_date, 3, "month")`) |
| `DATE_SUB(d, n, unit)` | Subtract `n` whole `unit`s from `d` |
| `DATE_DIFF(start, end, unit)` | Whole-`unit` calendar-boundary difference. Positive when `end > start`. |

`unit` is always one of `"day"`, `"week"`, `"month"`, `"quarter"`,
`"year"` — quoted as a string literal.

### Aggregation

Expand Down Expand Up @@ -205,12 +194,6 @@ a formula (or vice versa), delete the old one and create a new one in
the mode you want.
</Accordion>

<Accordion title="What happens if my warehouse isn't supported yet?">
The Formula toggle is hidden and the SQL editor is the only option.
Existing SQL table calculations keep working on every warehouse. The
beta is strictly additive.
</Accordion>

<Accordion title="I found a bug or want a function that isn't listed">
Post in the [Lightdash Community Slack](https://join.slack.com/t/lightdash-community/shared_invite/zt-3pbwqmq5e-vhTc7HHcS787w618ngoPHA)
or [open a GitHub issue](https://github.com/lightdash/lightdash/issues).
Expand All @@ -224,4 +207,3 @@ See [Contact Us](/contact/contact-info) for more options.
- [Table calculations overview](/guides/table-calculations)
- [Table calculation functions](/references/table-calculation-functions/row-functions)
- [SQL templates for table calculations](/guides/table-calculations/sql-templates)
- [Feature maturity levels](/references/workspace/feature-maturity-levels)
34 changes: 18 additions & 16 deletions guides/table-calculations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ description: "Table calculations make it easy to create metrics on the fly (for
You add table calculations in the Explore view and they appear as green columns in your results table (remember, [dimensions](/references/dimensions) are blue, and [metrics](/references/metrics) are orange).

<Frame>
<img src="/images/guides/table-calculations/table-calculations-add-a932fb32af1dc604d4ef709886f9a833.jpg"/>
<img src="/images/guides/table-calculations/table-calc-results-light.png" alt="Table calculation column in the results table" className="block dark:hidden" />
<img src="/images/guides/table-calculations/table-calc-results-dark.png" alt="Table calculation column in the results table" className="hidden dark:block" />
</Frame>

Table calculations can be built in two ways:

- **[Formula](/guides/formula-table-calculations)** (Beta): a spreadsheet-style syntax that reads like Google Sheets. The default mode for new table calculations on supported warehouses.
- **SQL**: raw SQL, available on every warehouse.
- **[Formula](/guides/formula-table-calculations)**: a spreadsheet-style syntax that reads like Google Sheets. The default mode for new table calculations.
- **SQL**: raw SQL.

<Info>
**[Formula table calculations](/guides/formula-table-calculations) are in [Beta](/references/workspace/feature-maturity-levels).** Enabled by default on supported warehouses: BigQuery, ClickHouse, Databricks, DuckDB, PostgreSQL, Redshift, and Snowflake.
</Info>

With either mode you can build mathematical, True/False, text, and date-based calculations on top of your results.
Both modes are available on every warehouse Lightdash supports. With either mode you can build mathematical, True/False, text, and date-based calculations on top of your results.

## When to use table calculations

Expand Down Expand Up @@ -48,7 +45,8 @@ Table calculations work with whatever data you've already pulled into your resul
Quick calculations are shortcuts to the most common table calculations, these are only available to metrics in the contextual menu in the results table.

<Frame>
![](/images/guides/table-calculations/quick-table-calculation-b1e76d3e54d84ca3c5066ffdf989514b.png)
<img src="/images/guides/table-calculations/quick-calculation-menu-light.png" alt="Quick calculation menu on a metric column" className="block dark:hidden" />
<img src="/images/guides/table-calculations/quick-calculation-menu-dark.png" alt="Quick calculation menu on a metric column" className="hidden dark:block" />
</Frame>

To learn more about what these calculations are doing, check out the docs [here](/guides/table-calculations/sql-templates). Once the table calculation is generated you can edit it to modify the SQL query or update the format.
Expand All @@ -58,7 +56,8 @@ To learn more about what these calculations are doing, check out the docs [here]
Once you've got some data in your results table, you can create a table calculation by clicking on the `+ Table calculation` in the `Results` tab of the Explore view:

<Frame>
![](/images/guides/table-calculations/create-new-table-calc-772add6d063a5122c0a25accf798fbb3.jpg)
<img src="/images/guides/table-calculations/create-table-calc-button-light.png" alt="+ Table calculation button in the Results header" className="block dark:hidden" />
<img src="/images/guides/table-calculations/create-table-calc-button-dark.png" alt="+ Table calculation button in the Results header" className="hidden dark:block" />
</Frame>

##### Write the SQL for your table calculation in the pop-up box
Expand All @@ -68,7 +67,8 @@ Your table calculation is defined using raw SQL that you write up in this pop up
To reference the metrics and dimensions in your results table, you can either use the autocomplete, or you can manually write the full field name using the format `${table_name.field_name}`.

<Frame>
![](/images/guides/table-calculations/add-table-calc-sql-20de5398dc117ef3d4087cf3b6d9939b.jpg)
<img src="/images/guides/table-calculations/table-calc-modal-light.png" alt="Add table calculation modal with SQL editor and inline format options" className="block dark:hidden" />
<img src="/images/guides/table-calculations/table-calc-modal-dark.png" alt="Add table calculation modal with SQL editor and inline format options" className="hidden dark:block" />
</Frame>


Expand All @@ -84,10 +84,11 @@ These functions compile to SQL window functions automatically, so you don't need

##### Update the format of your table calculation (if needed)

If needed, you can update the format of your table calculation to things like percent formatting using the `format` tab.
If needed, you can update the format of your table calculation — percent, currency, number with custom prefix/suffix — in the **Format** section of the modal, just below the SQL editor.

<Frame>
![](/images/guides/table-calculations/format-table-calc-c0e3828b6681db084b4ddd6799ff2c96.jpg)
<img src="/images/guides/table-calculations/table-calc-format-light.png" alt="Format options for a table calculation" className="block dark:hidden" />
<img src="/images/guides/table-calculations/table-calc-format-dark.png" alt="Format options for a table calculation" className="hidden dark:block" />
</Frame>

| Format types | Description | Raw value | How it might look like in Lightdash |
Expand All @@ -97,12 +98,13 @@ If needed, you can update the format of your table calculation to things like pe
| Currency | Adds currency symbol and default locale format | 1234.1234 | $ 1234.12 |
| Number | Formats number with prefix and suffix | 123.1234 | Speed: 123.12 km/h |

##### To delete or edit your table calculation, just click on the gear icon by the field name
##### To edit or remove your table calculation, open the column dropdown

If you need to edit or delete a table calculation, you can just click on the toggle beside the field and do what you need to do!
Click the chevron next to the table calculation column header in the results table. From there you can **Edit calculation** to reopen the modal, or **Remove** to drop it.

<Frame>
![](/images/guides/table-calculations/edit-delete-table-calc-520776c8631697527bf3760000dce88e.jpg)
<img src="/images/guides/table-calculations/table-calc-edit-delete-light.png" alt="Edit / delete dropdown on a table calculation column header" className="block dark:hidden" />
<img src="/images/guides/table-calculations/table-calc-edit-delete-dark.png" alt="Edit / delete dropdown on a table calculation column header" className="hidden dark:block" />
</Frame>


Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading