ThoughtSpot formula examples
Turbonomic SaaS reporting is provided by the ThoughtSpot analytics software. Report editors and report admins can create formulas to customize the configuration of data from ThoughtSpot answers, views, and worksheets.
In ThoughtSpot, an answer is the result of a search of a particular data source. You can save answers as views so that you can use them as data sources in other searches. A worksheet (or model) is a logical view of your data that ThoughtSpot uses to model and visualize complex datasets. You can use formulas to filter, format, and map these resources and create customized insights into your data. Use the ThoughtSpot formula editor to create and save formulas so that you can easily reuse them across your environment.
Customizing your data with formulas
You can use the ThoughtSpot formula editor create a formula either within a single answer or as part of a worksheet. To create a formula at the answer-level, you can extend the default worksheets. However, if you want to use a formula across multiple answers, you might need to copy it across each answer. Alternatively, creating a worksheet-level formula in a custom worksheet gives you access to all formulas across a set of answers. You can also use a custom worksheet to add unique filters to a Liveboard.
The following examples demonstrate general and Turbonomic-specific formula operations that you can extend to your reports.
Mapping and filtering qualitative data
Qualitative data, labeled as ATTRIBUTE in ThoughtSpot, includes string columns
for values such as entity ID, metric type, and datetime.
-
Mapping similar data values to a common alias: If you have different aliases for similar data values, you can use a formula to map them all together as a single representative data value.
For example, this formula maps the
turbo-park,turbo_park, andturbo_parkingcolumns together asturbo-park. It also maps thesuspendandsuspendtagcolumns together assuspend. It leaves any other columns in the data source as they are.if ( key = "turbo-park" or key = "turbo_park" or key = "turbo_parking") then "turbo-park" else if (key = "suspend" or key="suspendtag") then "suspend" else key - Filtering columns: If you want to filter columns on particular conditions, you can use
the
ifformula. The filter returns only the specified columns. Any data that you want to remove returns anullvalue and is excluded from the result.The following example filters out all columns except
business_applicationandstate_id:if ( type = 'business_application' and state_id != null ) then name else null -
Handling date-time data: To refer to dates within a formula, you must use specific date function keywords. For example, the following formula uses the
is_weekenddate function to exclude weekends (Saturday and Sunday) find the maximumUsedvalue:if ( not is_weekend( Datetime (UTC) )) then max(Used) else nullFor more information about date functions, see Time series analysis and About date formulas in the ThoughtSpot documentation.
Aggregating and converting quantitative data
Quantitative data, labeled as MEASURE in ThoughtSpot, includes columns such as
used, capacity, and savings. For quantitative
columns, you can use number keywords in the search bar for quick aggregation. However, sometimes
you might need to use formulas to access more complex logic.
-
Converting units: When a certain column contains values that you want to convert to another unit, enter the conversion as a mathematical formula.
For example, the following formula converts a
column_with_kmcolumn with values in kilometers to meters:column_with_km / 1000For better maintainability and readability, you can replace the numerical value with a parameter.
For example, you can create a parameter that is called
km_and_m_conversion, with a value of 1000, and use it in this formula:colmn_with_km / km_and_m_conversionFor more information, see Simple number calculations and Type conversion functions in the ThoughtSpot documentation.
-
Aggregating with conditions: The following formula template shows how to aggregate a data sum:
sum_if ( ${some condition} = '${desired condition}' , ${data column} * ${Optional unit conversion calculation} )For example, the following formula uses a parameter to convert any carbon metric values from grams to kilograms and then sums the results:
sum_if ( metric_type = 'carbon' , used / kg_and_g_conversion )This formula first checks whether the
metric_type= 'carbon'condition is met and then divides theusedcarbon value by a preconfiguredkg_and_g_conversionparameter, with a value of 1000, to convert from grams to kilograms. It then aggregates the converted values that met the condition. -
Aggregating without conditions: The following formula adds all the values under
valuewith thecontainer_cluster_idgroup_sum ( value , container_cluster_id ) -
Aggregating by group or filter: Group aggregation is a powerful formula to aggregate columns and values together given specific filter and group conditions. The formula uses the following format:
group_aggregate( [aggregation type] ( [column] ), [groupings], [filters] )Grouping options:query_groups(): Dynamically adds the attribute columns from the answer{ col1, col2, ... coln }: Static list of attribute columns+or-: Add or remove column sets
Filtering options:query_filters(): Dynamically adds the filters from the answer{ boolean_condition }: Boolean condition based on static list of filters+or-: Add or remove filter sets
If no modifications from the groups and filters in the query are needed, then
query_groups( )andquery_filters ( )can be omitted.Because the formula is the most flexible format, you can use it for a simple aggregation. For example, a simple average based on the
Usedvalue, which uses the groups and filters in the answer:group_aggregate( average(Used), query_groups(), query_filters() )Or you can perform more complex logic. For example:
sum ( group_aggregate ( sum ( savings ) , query_groups ( ) + { [id] } , query_filters ( ) + {[type] = cloud} ) ) * tree_sequestration_factor / metric_ton_to_gIn this example, you obtain the sum of all savings among the IDs, which are shown as
{[id]}, and anything the user set within the query, which is shown asquery_groups( ). This formula also filters the type ascloud, which is shown as{[type] = cloud}, and anything the user set within the query, which is shown asquery_filters ( ).After the group aggregation is obtained, it is then summed all together with the outer
sum().The formula then use parameters to multiply the aggregated sum by the
tree_sequestration_factorvalue and convert the result from grams to metric tons.For more examples, see this official documentation on group aggregation and flexible group aggregation functions.
Creating a formula for average utilization of all VMs or clusters
This specific example is from the cluster_capacity_namespac_consumed default
worksheet.
The utilization value is under the metric_fact table, which includes the values
for capacity, used, utilization, and
consumed.
This table also includes utilization calculations for cluster capacity and namespace consumed. It
uses a namespace_consumed_daily view to obtain the namespace that is consumed by
the day called consumed_day. It then obtains the capacity from the
cluster_capacity_by_date table called capacity_hour.
The following formula calculates the utilization for the cluster:
consumed_day / capacity_hour
Creating a formula for entity metric capacity in bytes
This specific example is from the Entity Metrics default worksheet.
Similar to the previous example, you can obtain the capacity from the
metric_fact table, under the capacity column. However, for better
readability, you want to display them in either KB, MB, or GB.
- Create a parameter called
Units (Bytes), with the allowed values asList. - Enter the following conversions, with the default value as
1:
Value: 1 Display as (optional): KB Value: 1024 Display as (optional): MB Value: 1048576 Display as (optional): GB - With the parameter created, you can now create the formula.
- Check whether the capacity is in the unit
bytes. Theis_byteidentifier is in themetric_type_dimtable. -
If the
is_bytevalue istrue, start the calculation:if ( is_byte ) then capacity / units (bytes) else capacityThis formula displays the capacity in the bytes if the capacity unit is bytes. If not, then it shows that the capacity as is.
- Check whether the capacity is in the unit
Getting the latest data from a table
If you want to show only the latest value of all the time series data, set the
datetime_id to itself.
if ( ${some conditions} = '${some desired state}' and datetime_id = datetime_id ) then true else false
For example, you can create the following formula and name it Latest Data:
if ( state = 'ready' and datetime_id = datetime_id ) then true else false
Alternatively, you might use datetime instead of
datetime_id:
if ( datetime = group_max ( datetime ) ) then true else false
You can add as many conditions as you need.
Then, use the formula in a ThoughtSpot query to show all data with the latest value:
... Latest Data = true ...