question

BlaiseLugeon-5253 avatar image
0 Votes"
BlaiseLugeon-5253 asked SadiqhAhmed-MSFT commented

Cost management GRAPH API: TheLastBillingMonth returns lastmonth amount

Hello,

I need to extract the cost of the last Azure invoice per resource group.
I use the Graph API following https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage

In timeframe param, the use of "TheLastMonth" returns the exact value of the last month => ok.
If I use instead "TheLastBillingMonth", I still get the same value [of the last month] instead of the last billing period which I expect.

See below the code I use... What's wrong in my understanding?

Thanks!



scope = 'subscriptions/' + subscription_id
base_url = 'https://management.azure.com/' + scope +'/providers/Microsoft.CostManagement/query?api-version=2019-11-01'

request header

headers = {'Authorization' : 'Bearer ' + access_token, 'Content-type' : 'application/json'}

 body = {
     "type": "Usage",
     **"timeframe": "TheLastBillingMonth",**
     "dataset": {
         "granularity": "None",
         "aggregation": {
             "totalCost": {
             "name": "PreTaxCost",
             "function": "Sum"
             }
         },
         "grouping": [
             {
                 "type": "Dimension",
                 "name": "ResourceGroup"
             }
         ]
     }
 }


azure-cost-management
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi!

@SadiqhAhmed-MSFT thanks for the answer.... and I confirm that the billing cycle start/end does not fit to the start/end of the month.
When I use the portal (cost management), I get different values if I use the 'last invoice' or if I use 'last month' period.

When I use the API (whatever I use 'TheLastMonth' or 'TheLastBillingMonth' parameter), I always get the exact value I get with the portal when I use 'last month' period.

This is why I suspect that the API using 'TheLastBillingMonth' does not work properly or at least how I expect/understand...

Does this function works whatever the type of offer I use, or is there restrictions? I use a basic 'pay as you go' subscription...

Best regards




0 Votes 0 ·

Looks like a bug in our API. We will work on fixing this. In the meantime, you can use the billing periods api to get the billing period dates of the current and previous billing periods.
You can then use those dates in the query api to get the cost.

0 Votes 0 ·

Thanks for your help... Yes please enter that in your backlog of bug.
I already tried to use the billing period, but it seems that it is not available without enterprise app plan... But I will find a way...

Best regards

Blaise

0 Votes 0 ·
Show more comments

1 Answer

SadiqhAhmed-MSFT avatar image
0 Votes"
SadiqhAhmed-MSFT answered

Hello anonymous user - Thank you for your post and I apologize for the delayed response!

If the billing cycle of your subscription is from 1st to last of the month, then the value returned by TheLastMonth and TheLastBillingMonth will be equal as we are looking at the same timeframe.

You can confirm the billing cycle from the subscription overview blade. My subscription billing cycle is 8/1 to 8/31 which means if I call last month or last billing month: both will be pointing to 7/1 to 7/30 and will return the same value:

125827-image.png


Hope this helps!


If the response helped, do "Accept Answer" and up-vote it.



image.png (94.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.