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 forDelivered 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:
- Select the Metrics tab.
- Select Create metric.
The Metric Editor window opens. The following image shows the editor with the corresponding step in the metric creation process highlighted. - Name your metric.
Note
By default, the name of the metric will become the metric ID. Once you save the metric, if you want to change the ID, you will need to manually edit the ID. To change the ID, select the current ID and replace the text with a new value.
- (Optional) Add a description of your metric.
- Populate the body of the Metric Editor with your MAQL expression.
Note
You can press control+space or CMD+I to open a list of possible keywords, facts, and attributes that you can use to help build your expression.
- (Optional) Select the format that numbers should use in the insight results. For more information, see Format Numbers.
- Select Save.
Your metric saves and appears in the list of available Metrics.Note
If the syntax for the expression is invalid, it is not possible to save the metric. Valid syntax does not necessarily mean that the metric will produce usable insights. This is because any combination of facts or attributes added to the query and any combination of attributes (either for slicing or for filtering) applied to the metric might not always produce a meaningful result when used together.
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.
# 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