Version:

Entity Var

Learn how to create and use entity_var in your Profiles project.

An entity_var adds a calculated (aggregated) column to the entity var tables and downstream feature views. It calculates a single value for each member of the entity (for example, each user).

entity_vars can be features for a feature view or an input to another entity_var.

- entity_var:
    name: entity_var_name
    select: SQL statement
    default_value: value
    from: inputs/table
    where: condition
    window:
        order_by:
            - orderby_column
    description: Days when the user visited the website along with the number of pages visited per date stamp.
FieldDescription
nameName of the entity_var.
selectSQL state, equivalent to SELECT statement.
fromPath of the input table, equivalent to FROM statement.
mergeSQL expression defining how to combine previous feature values with new calculations for incremental features. Use rowset.feature_name to reference the previous value from the checkpoint.
merge_whereOptional SQL condition that filters which rows participate in the merge operation. Use rowset.column for previous values from the checkpoint and {{this.column}} for new values.
default_valueDefault value for null values.
whereFiltering condition, equivalent to WHERE statement.
windowAdds order_by key for window functions. Rows are automatically partitioned by rudder_id.
descriptionDescription of the entity_var.

Example

# Basic entity
- entity_var:
    name: first_order_date
    select: min(order_date)
    from: inputs/orders
    description: First order date

# Entity that is not a feature
- entity_var:
    name: last_order_date
    select: max(order_date)
    from: inputs/orders
    is_feature: false

# Using an entity_var as an input
- entity_var:
    name: days_since_last_order
    select: "date_diff(CURRENT_DATE(), date({{user.last_order_date}}), day)"
    description: Days since user last completed an order

# Using a window function
- entity_var:
    name: campaign_source_first_touch
    select: first_value(context_campaign_source)
    from: inputs/rsIdentifies
    where: context_campaign_source is not null and context_campaign_source != ''
    window:
        order_by:
            - timestamp

# Entity var referencing an input var
- input_var:
    name: last_order
    select: last_value(order)
    from: inputs/rsTracks
    window:
        partition_by:
            - user_id
        order_by:
            - timestamp
- entity_var:
    name: is_last_order
    select: boolor_agg({{rsTracks.Var("last_order")}})
    from: inputs/rsTracks

# Incremental entity var with merge clause
- entity_var:
    name: total_purchases
    select: SUM({{orders.value}})
    merge: SUM(rowset.total_purchases)
    from: inputs/orders
    description: Total purchase value

Incremental features

You can make entity_vars incremental by adding a merge: clause. Incremental features update existing feature values with newly arrived data instead of recalculating from the entire historical dataset, significantly improving performance for large datasets.

For detailed information about incremental features, see the following guides:

GuideDescription
Incremental Features OverviewOverview of incremental features in Profiles
Make Features IncrementalStep-by-step guide to convert features to incremental
Simple AggregationsDirect aggregations with merge clauses (SUM, MIN, MAX, COUNT)
Compound AggregationsComplex aggregations built on simple aggregations (AVG, arrays)

Questions? Contact us by Email or on Slack