Building Calculated Insights for Customer 360

Raw data tells you what happened. Calculated insights turn it into numbers you can act on: lifetime spend, number of orders, days since the last visit. In Data 360 (formerly Data Cloud), you build them once and reuse them in segments, activations and other tools. This guide shows how to write them in SQL and what to watch out for.

Calculated or streaming?

Data 360 has two kinds of SQL-based insights:

  • Calculated insights run over stored data and can do complex calculations across many objects. They run on a schedule.
  • Streaming insights run on data as it arrives, for near real-time metrics over short time windows.

This guide covers calculated insights, which is what you will use for most customer metrics.

Measures and dimensions

Every calculated insight has two parts.

Measures are the numbers. They hold aggregated values, like SUM, COUNT or AVG of a field.

Dimensions are what you group the numbers by, like customer ID or product category. Every dimension appears in the GROUP BY clause.

Think of it as a pivot table. Dimensions are the rows, measures are the values.

The basic structure

A calculated insight query follows this order:

  1. SELECT the measures and dimensions
  2. FROM one data model object
  3. JOIN other DMOs if needed (inner, left, right and full joins are supported)
  4. WHERE to filter rows, if needed
  5. GROUP BY your dimensions

The output columns get aliases that end in __c. DMO names end in __dlm, and standard fields start with ssot__.

Example: lifetime spend per customer

This insight adds up each customer's order totals.

SQL  ·  Customer lifetime spend
SELECT
    SUM(ssot__SalesOrder__dlm.ssot__GrandTotalAmount__c) AS customer_spend__c,
    ssot__Individual__dlm.ssot__Id__c AS customer_id__c,
    ssot__Individual__dlm.KQ_Id__c AS kq_customer_id__c
FROM ssot__SalesOrder__dlm
LEFT JOIN ssot__Individual__dlm
    ON ssot__SalesOrder__dlm.ssot__SoldToCustomerId__c = ssot__Individual__dlm.ssot__Id__c
    AND IFNULL(ssot__SalesOrder__dlm.KQ_SoldToCustomerId__c, '') = IFNULL(ssot__Individual__dlm.KQ_Id__c, '')
GROUP BY
    customer_id__c,
    kq_customer_id__c

Two things are worth pointing out.

The join uses the key qualifier too. Data from different sources can share the same ID value. Salesforce recommends joining on the key qualifier fields as well as the ID so records from different sources don't get matched by mistake.

IFNULL protects the join. If some source records have a blank key qualifier, a plain equals check fails because NULL never equals NULL. Wrapping both sides in IFNULL makes blanks match blanks.

Example: order count and average order value

You can have more than one measure in the same insight. ROUND keeps the average readable.

SQL  ·  Order count and average order value
SELECT
    COUNT(ssot__SalesOrder__dlm.ssot__Id__c) AS order_count__c,
    ROUND(AVG(ssot__SalesOrder__dlm.ssot__GrandTotalAmount__c), 2) AS avg_order_value__c,
    ssot__SalesOrder__dlm.ssot__SoldToCustomerId__c AS customer_id__c
FROM ssot__SalesOrder__dlm
GROUP BY
    customer_id__c

Example: a spend tier with CASE

You can add a CASE expression to label customers. CASE returns the result of the first condition that is true. If nothing matches and there's no ELSE, it returns NULL, so always include an ELSE.

SQL  ·  Spend tier as a dimension
SELECT
    SUM(ssot__SalesOrder__dlm.ssot__GrandTotalAmount__c) AS customer_spend__c,
    ssot__SalesOrder__dlm.ssot__SoldToCustomerId__c AS customer_id__c,
    CASE
        WHEN ssot__SalesOrder__dlm.ssot__GrandTotalAmount__c >= 500 THEN 'High'
        WHEN ssot__SalesOrder__dlm.ssot__GrandTotalAmount__c >= 100 THEN 'Medium'
        ELSE 'Low'
    END AS order_size__c
FROM ssot__SalesOrder__dlm
GROUP BY
    customer_id__c,
    order_size__c

Notice that this groups by order size, so a customer can have up to three rows: spend from high, medium and low orders. That is useful for analysis, but if you want one tier per customer for segmentation, calculate total spend first and assign the tier in the segment rule instead.

💡 Build on unified profiles when you can

These examples use the order's customer ID to keep them short. For marketing, you usually want the metric per Unified Individual, so one person with three source records gets one number. Join through the unified link object to roll the metric up to the unified profile.

Don't want to write SQL?

The Insights Builder lets you build the same thing with drag and drop. Go to the Calculated Insights tab, click New, and choose Create with Builder. You pick the DMO, add joins, set aggregations and choose dimensions, and the builder writes the SQL for you.

It is a good way to learn the structure. Many people start in the builder, then switch to SQL once the logic gets more involved.

Using the results

Once an insight has run, you can:

  • Use its measures in segment rules, like "customer spend greater than 1,000"
  • Send its values with activations, so emails can show the number
  • Query it through the Calculated Insights API, which supports up to 5 measures and 10 dimensions per request

Common mistakes

  • Forgetting GROUP BY. Every dimension must be in the GROUP BY, or the insight won't validate.
  • Measures without aggregation. A measure must be an aggregate like SUM or COUNT.
  • Joining on ID only. Always include the key qualifier in joins across sources.
  • Too many small insights. If five insights all group by customer ID over the same DMO, combine them into one with several measures.

Checklist

  • Every measure is aggregated and every dimension is grouped
  • Aliases end in __c
  • Joins include key qualifiers, wrapped in IFNULL where blanks are possible
  • CASE expressions have an ELSE
  • The insight is built on the object you will segment on, ideally the unified profile

Sources and further reading

Share this article

Help others discover this content

More Articles

  • All Posts
  • Marketing Cloud
  • Data Cloud
  • Sales Cloud
  • SFMC Next