# Metric Equation Reference

Metric Equations define how Firstparty should calculate the value of a Metric for each row of data returned in a Report.

## Defining an Equation

Metric Equations are comprised of a combination of three components:

1. Variables which reference Properties of Events, Sessions, or Profiles, idenfitied by the Property accessor inside curly braces (ex: `{event.name}`)
2. Operators which specify how to combine the values of the variables (ex: `+`, `-`, `*`, `/`)
3. Functions which specify how to calculate the value of the Metric (ex: `count`, `sum`, `avg`, etc.)

You may combine the components in any way, and the equation will be evaluated by following the typical arithmetic order or operations, further specified by the inclusion of parentheses.

## Available Equation Functions

• `count(_property_)`: Counts the number of the specified Property.
• `sum(_property_)`: Sums the values of the specified Property.
• `avg(_property_)`: Calculates the average value of the specified Property.
• `unique(_property_)`: Counts the number of unique values of the specified Property.
• `next(_property_)`: Returns the value of the specified Property for the next Event in the order the Events were created.
• `previous(_property_)`: Returns the value of the specified Property for the previous Event in the order the Events were created.
• `first(_property_)`: Returns the value of the specified Property for the first Event in the order the Events were created.
• `last(_property_)`: Returns the value of the specified Property for the last Event in the order the Events were created.
• `timediff(_end_, _start_)`: Calculates the difference between two timestamps.

## Metric Filters & Equations

Metrics support Filters, which limit the data that is included when calculating the value of that Metric. Filters are applied to the entire Metric, and will affect how the equation is calculated.

For example, if you want to calculate the number of pageviews, you can't simply rely on the `count` function. You must also add a Filter to the Metric that limits the data to only those Events that have a `name` that matches the value `Page Viewed`.

## Example Equations

• Total Sessions: `unique({events.session_id})`
• Total Events: `count({events.id})`
• Events Per Profile: `count({events.id}) / unique({events.profile_id})`
• Pageviews Per Session: `count({events.id}) / unique({events.session_id})`
• Avg. Time on Page: `avg(timediff(next({events.created_at}) - {events.created_at}))`