Create a computed column inside a data model
In this guide, we will walk you through the process of creating a computed column in Cargo.
Computed columns allow you to use expressions, or embedded code, to dynamically generate data based on existing fields.
Before you begin
Ensure you have access to the data model you want to enhance with a computed column.
Select data from left sidebar menu. Pan the explorer or use the search box to find the data model to work on.
Right click the data model and select columns. This will bring columns configuration modal.
Click "Add +" next to the computed columns box.
Choose the data type for your computed column based on the kind of data you want to generate (e.g., String, Number, Boolean).
Once the column is added click it to open configuration pane. Configure:
- Label - human friendly name of the column that can contain any characters and can be edited
- Slug - a machine friendly name that needs to be unique and can't contain special characters, cannot be edited
- Type - the type of the data this column will yield to
Now we will dive into writing the expression that will run the computation.
Define the expression
Add an expression to define the formula for your computed column. This expression will determine how the column's values are computed based on existing fields in your data model.
For more complex expressions, refer to the guide on understanding the cargo expressions template.
For instance, concatenate string values from two columns:
Concatenate first and last name of a record
{{ record.firstName }} {{ record.lastName }}
You can also use more complex javascript expressions to calculate values. For example, calculate age from date of birth:
Calculate age from date of birth
{
{
new Date().getFullYear() - new Date(record.birthDate).getFullYear();
}
}
You will need to save the computed column's configuration to apply your changes to the data model.
Click Save
to apply new computed column's configuration.
Finish line
By now, you have successfully set up a computed column in Cargo, you can now use the computed column in your workflows.