Create Metrics

While working directly with facts is possible to deliver basic analytics, you may want to create metrics that to do more complex calculations to provide the consumers of your analytics with specific business contexts. For example:

  • Order Amount (Quantity * Price)
  • Revenue (Order Amount, but only for Delivered Orders)
  • Revenue per Order (Revenue / # of Delivered Orders)
  • Average Order Amount (Order Amount / # Orders)

For more information on how to create and edit metrics, see Metrics.

Use the Metric Editor to Create Metrics

To create a metric in the Metric Editor, follow these steps:

  1. Select the Metrics tab.

    Metrics tab

  2. Select Create metric.
    The Metric Editor window opens. The following image shows the editor with the corresponding step in the metric creation process highlighted.

    Metric editor

  3. Name your metric.
  4. (Optional) Add a description of your metric.
  5. Populate the body of the Metric Editor with your MAQL expression.
  6. (Optional) Select the format that numbers should use in the insight results. For more information, see Format Numbers.
  7. Select Save.
    Your metric saves and appears in the list of available Metrics.

Use the API to Create Metrics

You can use either the entity API interface or the declarative API interface to create a metric. The following example use the entity API interface to sequentially create two new metrics.

The first metric, Order Amount, is the sum price of all orders that the metric is applied to. The second metric, Number of Orders, is the total count of orders for whatever the metric is applied against.

Bash
PowerShell 7
# Order Amount
curl http://localhost:3000/api/entities/workspaces/demo/metrics \
  -H "Content-Type: application/vnd.gooddata.api+json" \
  -H "Accept: application/vnd.gooddata.api+json" \
  -H "Authorization: Bearer YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz" \
  -X POST \
  -d '{
      "data": {
          "attributes": {
              "title": "Order Amount",
              "content": {
                  "format": "$#,##0",
                  "maql": "SELECT SUM({fact/order_lines.price}*{fact/order_lines.quantity})"
              }
          },
          "id": "order_amount",
          "type": "metric"
      }
  }' | jq .
# Number of Orders
curl http://localhost:3000/api/entities/workspaces/demo/metrics \
  -H "Content-Type: application/vnd.gooddata.api+json" \
  -H "Accept: application/vnd.gooddata.api+json" \
  -H "Authorization: Bearer YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz" \
  -X POST \
  -d '{
      "data": {
          "attributes": {
              "title": "# of Orders",
              "content": {
                  "format": "#,##0",
                  "maql": "SELECT COUNT({attribute/order_lines.order_id})"
              }
          },
          "id": "amount_of_orders",
          "type": "metric"
      }
  }' | jq .
# Order Amount
Invoke-RestMethod -Method Post -Uri 'http://localhost:3000/api/entities/workspaces/demo/metrics' `
   -ContentType 'application/vnd.gooddata.api+json' `
   -H @{ 
     'Accept' = 'application/vnd.gooddata.api+json'
     'Authorization' = 'Bearer YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz' 
   } `
   -Body '{
      "data": {
          "attributes": {
              "title": "Order Amount",
              "content": {
                  "format": "$#,##0",
                  "maql": "SELECT SUM({fact/order_lines.price}*{fact/order_lines.quantity})"
              }
          },
          "id": "order_amount",
          "type": "metric"
      }
  }' | ConvertTo-Json
# Number of Orders
Invoke-RestMethod -Method Post -Uri 'http://localhost:3000/api/entities/workspaces/demo/metrics' `
   -ContentType 'application/vnd.gooddata.api+json' `
   -H @{ 
     'Accept' = 'application/vnd.gooddata.api+json'
     'Authorization' = 'Bearer YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz' 
   } `
   -Body '{
      "data": {
          "attributes": {
              "title": "# of Orders",
              "content": {
                  "format": "#,##0",
                  "maql": "SELECT COUNT({attribute/order_lines.order_id})"
              }
          },
          "id": "amount_of_orders",
          "type": "metric"
      }
  }' | ConvertTo-Json