Overview
Calculated fields allow you to create new columns derived from existing data using SQL formulas. These virtual columns appear alongside regular columns in the Query Builder, enabling powerful analytics without modifying your source data.SQL Formulas
Write expressions using standard SQL syntax
Pre-built Templates
Start with common calculations for automotive data
Live Validation
Test formulas against real data before saving
Query Integration
Calculated fields work seamlessly in Query Builder
What Are Calculated Fields?
Calculated fields are virtual columns that compute values on-the-fly using SQL expressions. Unlike raw columns that store static data, calculated fields derive values from other columns.Common Use Cases
| Use Case | Example Formula | Result |
|---|---|---|
| Date Differences | stop_date - start_date | Days between two dates |
| Time Calculations | EXTRACT(EPOCH FROM (end_time - start_time)) / 60 | Minutes duration |
| Conditional Values | CASE WHEN sale_type = 'Lease' THEN 'Off-Lease' ELSE 'Consignment' END | Categorization |
| Text Operations | UPPER(LEFT(vin, 10)) | Formatted identifiers |
| Aggregation Ready | price * quantity | Computed values for sums |
Creating Calculated Fields
Accessing the Editor
Field Configuration
| Field | Required | Description |
|---|---|---|
| Field Name | Yes | Technical name (no spaces, lowercase) |
| Display Name | Yes | Human-readable name shown in UI |
| Description | No | Explanation for AI context |
| Formula | Yes | SQL expression |
| Source Columns | Auto | Columns used in formula (auto-detected) |
| Result Type | Yes | Expected output type (number, string, date) |
| Usage Examples | No | Example queries using this field |
Formula Editor
The formula editor provides:- Syntax highlighting for SQL keywords
- Column autocomplete - start typing to see matching columns
- Validation - check formula syntax before saving
- Live preview - see sample results from actual data
Formulas are validated against your dataset’s actual schema and data. Invalid column references or syntax errors are caught before saving.
Formula Syntax
Calculated fields use PostgreSQL SQL syntax. Any valid SQL expression that can appear in a SELECT clause is supported.Basic Arithmetic
Date Operations
Time Calculations
Conditional Logic
String Operations
Predefined Templates
The system includes templates for common automotive calculations. Click Use Template to populate a pre-built formula.Available Templates
days_since_checkin
days_since_checkin
Formula:
CURRENT_DATE - check_in_dateResult Type: numberDescription: Calculates the number of days since the vehicle was checked in. Useful for tracking lot aging and processing delays.Example Query: “Show average days since check-in by sale type”days_to_cr
days_to_cr
Formula:
cr_completion_date - check_in_dateResult Type: numberDescription: Days from vehicle check-in to condition report completion. Key metric for CR turnaround time.Example Query: “What’s the average time to CR this month vs last month?”cr_time_minutes
cr_time_minutes
Formula:
EXTRACT(EPOCH FROM (stop_time - start_time)) / 60Result Type: numberDescription: Duration of condition report in minutes. Used for writer productivity analysis.Example Query: “Show average CR time by writer”days_to_sale
days_to_sale
Formula:
sale_date - check_in_dateResult Type: numberDescription: Total days from check-in to sale. Important for inventory turn analysis.Example Query: “What’s our average days-to-sale by sale type?”age_at_sale
age_at_sale
Formula:
EXTRACT(YEAR FROM sale_date) - model_yearResult Type: numberDescription: Vehicle age in years at time of sale. Useful for fleet analysis.Example Query: “Show distribution of vehicle age at sale”Using Templates
Formula Validation
Real-Time Validation
As you type, the editor validates:- Syntax: SQL expression is well-formed
- Column References: All referenced columns exist in the dataset
- Type Compatibility: Operations are valid for the data types involved
Sample Execution
Click Test Formula to:- Execute the formula against sample data rows
- See actual computed values
- Verify the result type is correct
Common Validation Errors
| Error | Cause | Solution |
|---|---|---|
| ”Column not found” | Typo in column name | Use autocomplete or check spelling |
| ”Type mismatch” | Operation on wrong type | Cast values: ::INTEGER, ::DATE |
| ”Syntax error” | Invalid SQL | Check parentheses, quotes, operators |
| ”Division by zero” | Denominator can be zero | Use NULLIF(column, 0) wrapper |
Using Calculated Fields
In Query Builder
Calculated fields appear in the Column Reference Panel under a “Calculated” category. They can be:- Selected for output columns
- Used in WHERE conditions
- Aggregated (SUM, AVG, COUNT)
- Grouped in GROUP BY
Example Queries
Average Time-to-CR
Average Time-to-CR
With a
days_to_cr calculated field:Query: “What’s the average days to CR completion by sale type?”Generated SQL:Aged Inventory Count
Aged Inventory Count
With a
days_since_checkin calculated field:Query: “How many vehicles have been on lot more than 30 days?”Generated SQL:Writer Productivity
Writer Productivity
With a
cr_time_minutes calculated field:Query: “Show average CR time per writer for the last week”Generated SQL:Best Practices
Formula Design
Do
- Handle NULL values explicitly
- Use COALESCE for defaults
- Test edge cases
- Document complex formulas
Don't
- Assume data is always clean
- Create overly complex formulas
- Forget about division by zero
- Skip validation testing
Performance Considerations
Calculated fields are computed at query time. For optimal performance:- Keep formulas simple when possible
- Avoid nested subqueries in formulas
- Use appropriate indexes on source columns
- Consider materialized columns for frequently-used expensive calculations
Usage Examples Field
When adding a calculated field, include usage examples in the description. This helps the AI understand when to use the field:Troubleshooting
Formula returns NULL for all rows
Formula returns NULL for all rows
Cause: NULL propagation from source columnsSolution: Wrap nullable columns with COALESCE:
Division returns 0 instead of NULL
Division returns 0 instead of NULL
Cause: Integer division behaviorSolution: Cast to decimal and handle zero:
Date calculation returns unexpected values
Date calculation returns unexpected values
Cause: Timestamps vs dates mixingSolution: Cast to appropriate type:
Field not appearing in Query Builder
Field not appearing in Query Builder
Cause: Formula validation failed or field not savedSolution:
- Check that validation passed (green checkmark)
- Click Save explicitly
- Refresh the Query Builder page