Create Measures Using the APIs

How to create a custom measure?

While working directly with facts is handy, you may want to create measures with higher order computations to provide the consumers of your analytics with better business context. 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)

Similar to other entities, you can manage measures with the following APIs:

Create Measures

Create two new measures using the entity API interface. This API will create the measures one by one.

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

Check the Measures in Analytical Designer

To check the created measures, go to Analytical Designer . You can see the new measures under the Ungrouped group in the left panel.

Created measures