> ## Documentation Index
> Fetch the complete documentation index at: https://www.propeldata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Defining Metrics

> Define Metrics using the console, API, or Terraform.

<div />

## Key concepts

* **[Metric type](/docs/query-apis/types-of-metrics)**: Defines how the data is aggregated.
* **Data Pool**: The source of the data for the Metric.
* **Dimensions**: A set of columns used to categorize and segment the Metric data.
* **Filters**: A set of filters that define a subset of the Data Pool records to include in the Metric calculations.

## Defining a Metric

<Tabs>
  <Tab title="Console">
    This example shows how to create a Sum Metric to define a "Revenue" metric by summing the "total\_price" column in the TacoSoft sample dataset.

    <img src="https://mintcdn.com/propeldocs/7MySejCVmSGgOSJb/images/docs/2023-09-18-create-metric.gif?s=d235a298bb444898891b7c30feaab3e3" alt="A screen capture demonstrating how to define a Metric." width="1507" height="862" data-path="images/docs/2023-09-18-create-metric.gif" />
  </Tab>

  <Tab title="API">
    This example shows the `createSumMetric` API mutation to define a "Revenue" metric by summing the "total\_price" column in the TacoSoft sample dataset.

    <CodeGroup>
      ```graphql Query theme={"system"}
      mutation {
        createSumMetric(input: {
          dataPool: "DPOXXXXXXXXXXXXXX",
          uniqueName: "Revenue",
          dimensions: [
            {
              columnName: "restaurant_name"
            }
          ],
          measure: {
              columnName: "taco_total_price"
            }
        }) {
          metric {
            id
            uniqueName
          }
        }
      }
      ```

      ```json Response theme={"system"}
      {
        "createSumMetric": {
          "metric": {
            "id": "MET01JBAFZGFV5BC6H09DVB8VNWG8",
            "uniqueName": "Revenue"
          }
        }
      }
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Terraform">
    ```hcl theme={"system"}
    resource "propel_metric" "revenue_metric" {
      unique_name = "Revenue"
      data_pool   = "DPOXXXXXXXXXXXXXX"

      type    = "SUM"
      measure = "taco_total_price"

      dimensions = ["restaurant_name"]
    }
    ```
  </Tab>
</Tabs>

***

## Examples

### Example 1: Metrics with filters

This example shows how to create a Sum Metric with filters to define an "Al Pastor Revenue" metric by summing the "total\_price" column for Al Pastor tacos in the TacoSoft sample dataset.

<Tabs>
  <Tab title="Console">
    <img src="https://mintcdn.com/propeldocs/Dnye5EmtO6L5-zx0/images/docs/2024-10-28-create-metric-with-filters.png?fit=max&auto=format&n=Dnye5EmtO6L5-zx0&q=85&s=a9f41cf20bf186fda5a5899bde2aa44d" alt="A screen capture demonstrating how to define a Metric with filters." width="1387" height="928" data-path="images/docs/2024-10-28-create-metric-with-filters.png" />
  </Tab>

  <Tab title="API">
    This example shows the `createSumMetric` API mutation to define a "Revenue" metric by summing the "total\_price" column in the TacoSoft sample dataset with filters.

    <CodeGroup>
      ```graphql Query theme={"system"}
      mutation {
        createSumMetric(input: {
          dataPool: "DPOXXXXXXXXXXXXXX",
          uniqueName: "AlPastorRevenue",
          dimensions: [
            {
              columnName: "restaurant_name"
            }
          ],
          measure: {
            columnName: "taco_total_price"
          }
          filterSql: "taco_name = 'Al Pastor'"
        }) {
          metric {
            id
            uniqueName
          }
        }
      }
      ```

      ```json Response theme={"system"}
      {
        "createSumMetric": {
          "metric": {
            "id": "MET01JBAG7399E99T00F216R1Q414",
            "uniqueName": "AlPastorRevenue"
          }
        }
      }
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Terraform">
    ```hcl theme={"system"}
    resource "propel_metric" "al_pastor_revenue" {
      unique_name = "AlPastorRevenue"
      data_pool   = "DPOXXXXXXXXXXXXXX"

      type    = "SUM"
      dimensions = ["restaurant_name"]
      measure = "taco_total_price"
      filter_sql = "taco_name = 'Al Pastor'"
    }
    ```
  </Tab>
</Tabs>

***

### Example 2: Metrics with JSON fields

You can use JSON values in the metric definition, either as a measure or as filters.

This example shows how to create a Sum Metric using a measure from a JSON column.

<Tabs>
  <Tab title="Console">
    <img src="https://mintcdn.com/propeldocs/7MySejCVmSGgOSJb/images/docs/2023-09-18-create-metric-with-json.gif?s=a921faf8e8820f33e9653a04522457d0" alt="A screen capture demonstrating how to define a Metric with a JSON colum." width="1429" height="346" data-path="images/docs/2023-09-18-create-metric-with-json.gif" />
  </Tab>

  <Tab title="API">
    The following example demonstrates how to use the `createSumMetric` API mutation to define a "Revenue" metric. This metric sums the values located in a JSON column at the path: "OBJECT.order.total\_price".

    ```graphql Query theme={"system"}
    mutation {
      createSumMetric(input: {
        dataPool: "DPOXXXXXXXXXXXXXX",
        uniqueName: "Revenue",
        dimensions: [
          {
            columnName: "restaurant_name"
          }
        ],
        measure: {
            columnName: "OBJECT.order.total_price"
          }
      }) {
        metric {
          id
          uniqueName
        }
      }
    }
    ```
  </Tab>

  <Tab title="Terraform">
    ```hcl theme={"system"}
    resource "propel_metric" "revenue_metric" {
      unique_name = "Revenue"
      data_pool   = "DPOXXX"

      type    = "SUM"
      measure = "OBJECT.order.total_price"

      dimensions = ["Restaurant"]
    }
    ```
  </Tab>
</Tabs>

***

### Example 3: Custom Metrics

Custom Metrics use SQL expressions to aggregate data from your Data Pool, enabling more complex business logic.

This example shows how to create a Custom Metric for "Average revenue per order".

<Tabs>
  <Tab title="Console">
    <img src="https://mintcdn.com/propeldocs/7MySejCVmSGgOSJb/images/docs/2023-09-18-create-a-custom-metric.gif?s=c187431aa58d2e37d4074801cb6c8fd2" alt="A screen capture demonstrating how to define a Custom Metric." width="1490" height="862" data-path="images/docs/2023-09-18-create-a-custom-metric.gif" />
  </Tab>

  <Tab title="API">
    The following example shows the query and input variables for the `createCustomMetric` API mutation to define an “Average revenue per order” metric with a custom expression.

    **Query**

    ```graphql theme={"system"}
    mutation {
      createCustomMetric(input: {
        dataPool: "DPOXXX",
        uniqueName: "Average revenue per order",
        filters: [
          {
            column: "ORDER_ID",
            operator: "IS_NOT_NULL"
          }
        ],
        dimensions: [
          {
            columnName: "Restaurant"
          }
        ],
        expression: "SUM(total_price) / COUNT_DISTINCT(ORDER_ID)"
      }) {
        metric {
          id
          uniqueName
        }
      }
    }
    ```
  </Tab>

  <Tab title="Terraform">
    ```hcl theme={"system"}
    resource "propel_metric" "average_revenue_per_order" {
      unique_name = "Average revenue per order"
      data_pool   = "DPOXXX"
      dimensions = ["Restaurant"]
      expression = "SUM(total_price) / COUNT_DISTINCT(ORDER_ID)"
    }
    ```
  </Tab>
</Tabs>

***
