Azure – Querying Azure Cost Management REST APIs not returning PreTaxCost Column

azure

Im using the Management Rest API to retrieve information about some subscription's billing usage.

I Have created the service principal and call the API succesfully, but in the response object the only columns returned are:

{
   "name": "UsageDate",
   "type": "Number"
},
{
   "name": "Currency",
    "type": "String"
}

I need the PreTaxCost column shown in the example docs.

https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage

UPDATE

I was taking on the SubscriptionQuery-Legacy example with the subscription scope. To see if I can retrieve the PreTaxtColum as they did in doc's

but the API returned the following error:

{
    "error": {
        "code": "BadRequest",
        "message": "Model validation failed: Invalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set.\r\nInvalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set.\r\nInvalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set.\r\n\r\n (Request ID: bf5b35cd-739a-47b2-a1fc-6db18be3a71f)"
    }
}

I tried to change the example query a little bit and remove the filter property, but it was not returning the PreTaxtCost Column.

My query ends up like this:

{
  "type": "Usage",
  "timeframe": "MonthToDate",
  "dataset": {
    "granularity": "Daily"
  }
}

The exact responde was:

{
    "id": "XXX",
    "name": "XXXX",
    "type": "Microsoft.CostManagement/query",
    "location": null,
    "sku": null,
    "eTag": null,
    "properties": {
        "nextLink": null,
        "columns": [
            {
                "name": "UsageDate",
                "type": "Number"
            },
            {
                "name": "Currency",
                "type": "String"
            }
        ],
        "rows": [
            [
                20200501,
                "USD"
            ],
            [
                20200502,
                "USD"
            ],
            [
                20200503,
                "USD"
            ],
            [
                20200504,
                "USD"
            ],
            [
                20200505,
                "USD"
            ]
        ]
    }
}

I move foward to use the SubscriptionQueryGrouping-Legacy example and the returning object was the same as the doc's.

So, any opinion to help me understand the behavior here?
Docs are outdated?

Best Answer

I used the request body as documented like below and it worked.

{
  "type": "Usage",
  "timeframe": "TheLastMonth",
  "dataset": {
    "granularity": "None",
    "aggregation": {
      "totalCost": {
        "name": "PreTaxCost",
        "function": "Sum"
      }
    },
    "grouping": [
      {
        "type": "Dimension",
        "name": "ResourceGroup"
      }
    ]
  }
}
Related Topic