Hi! I’m trying to calculate ratio of sums via API....
# gooddata-platform
в
Hi! I’m trying to calculate ratio of sums via API. I have two Sum metrics without names or aliases. Is it possible to assign name to metrics? Then I send this POST request with MAQL: script "SELECT {metric/Sum of Hire amount} / ({metric/Sum of Term amount})" And get the response:   "detail": "Some of given referenced 'metrics' entities do not exist.Not existing IDs: StockId(apiId=Sum of Hire amount, scope=WorkspaceObjectScope(organization=default, space=daceb3e88abd4bd5a7af036e9876a837)), StockId(apiId=Sum of Term amount, scope=WorkspaceObjectScope(organization=default, space=daceb3e88abd4bd5a7af036e9876a837))",     "status": 400,     "title": "Bad Request",     "traceId": "750ad93f87abe878" Please, help me to get the desired result.
p
Hi Vasilij, the metrics from your first screenshot are valid only within the insight you’re building and cannot be referenced outside (think of them as local variables). To make them global, POST them first like
SELECT SUM({fact/id})
- replace
id
with your id of Hire Amount. Then you can create your ratio metric and reference the two `{metric/id}`s. You can also expand the metric definitions and post simply
SELECT SUM({fact/id1}) / SUM({fact/id2})
.
в
Thank you! How can I get id of my Hire Amount metric? I’m getting emply metrics list by running command curl -H "Authorization: Bearer YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz" -X GET https://gooddata-dev.criterionhcm.com/api/entities/workspaces/daceb3e88abd4bd5a7af036e9876a837/metrics Response: {"data":[],"links":{"self":"http://localhost:3000/api/entities/workspaces/daceb3e88abd4bd5a7af036e9876a837/metrics?page=0&size=20","next":"http://localhost:3000/api/entities/workspaces/daceb3e88abd4bd5a7af036e9876a837/metrics?page=1&size=20"}}
p
First you need to create it using POST. The MAQL for it will use the id of your Hire Amount fact (go to Data, find its dataset, click View Details to find the id).
в
Thanks Petr! Is this teh right place to find the facts IDs? The IDs are fact.dshb_hire_n_term.hireamount and fact.dshb_hire_n_term.termamount? What POST do I need to create metrics?
I tried this POST but got "Bad request"
image.png
d
@Василий Островский The easiest way to find the identifiers of objects (facts, metrics, attributes, ...) is to list through the API, similarly to what you already did with metrics. E.g. in case of facts that would be https://gooddata-dev.criterionhcm.com/api/entities/workspaces/daceb3e88abd4bd5a7af036e9876a837/facts
❤️ 1
в
Thank you, David! I created Average Paid Wages metric by this "maql": "SELECT COUNT({fact/fact.dshb_payroll_income.id})/ SUM({fact/dshb_payroll_income.amount})" But the metric is ungrouped, although I used facts from dshb_payroll_income dataset. How can I move Average Paid Wages metric to Dshb payroll income group to analyze it with group's attributes Department and Work Location?
в
I added tag to metric by posting {   "data": {     "id": "APW",     "type": "metric",     "attributes": {       "title": "APW",       "description": "",       "tags": [         "Dshb payroll income"       ],       "content": {         "format": "#,##0",         "maql": "SELECT COUNT({fact/fact.dshb_payroll_income.id})/ SUM({fact/dshb_payroll_income.amount})"       }     }   } } It appeared in group, but it shows on value when added to an insight. What else may I have missed?
u
And you don’t need to move ” Average Paid Wages” metric to dshb_payroll_income dataset group to be able to dimension it with attributes from dshb_payroll_income dataset. If the facts from which the metrics is composed have relations (in LDM you should follow the reverse direction of arrows, and connection points ) to this dataset in your LDM then (in your case you have) you should be able to dimension in with those attributes.
I used the reference here and send the BODY like below
Copy code
{
  "data": {
    "attributes": {
      "title": "Order Amount Title",
      "content": {
        "format": "$#,##0",
        "maql": "SELECT SUM({fact/order_lines.price}*{fact/order_lines.quantity})"
      },
      "tags": [
        "Order Amount TAG"
      ]
    },
    "id": "order_amount_tagged",
    "type": "metric"
  }
}
and created the metric with tag and the tagged metric works. If you create the metric as above and your LDM supports this, then the metric displays without problem.
в
Thank you, Ulku!
p
@Василий Островский does your metric work now?
в
yes, now it works properly!