Power BI and Excel
Load your Costfluent costs by month, service and sub-account into Power BI Desktop or Excel with a Power Query that reads the Public API and a read-only workspace API token.
The Costfluent query for Power Query reads a workspace's costs from the Public API into a table: one row per month, service and sub-account over the last 12 months, or fewer when the plan keeps less history, with the cost, amortized cost, list cost and currency. Power BI Desktop and Excel both run Power Query, so the same query works in either, and Refresh reads the latest costs again. To take one report's numbers into a spreadsheet once, export it instead; see Export to CSV or Excel.
Before you start
Available on the Pro, Business and Custom plans. Compare plans
- Creating the token needs the Owner, Integration Owner or Editor organization role; see API authentication.
- Power BI Desktop, or Excel for Microsoft 365.
- The workspace's ID: Settings, then Workspaces, lists it in the ID column with a copy button. Its currency settings present the amounts; see Currency settings.
Create a token
- Open Settings, then API tokens, and select Create API token.
- Enter a Name that says where the token is used, such as
Power BI finance report. - Under Access, choose the workspace the report reads.
- Under Start from, choose Read only, then clear every capability except View costs, the only one the query needs.
- Choose an Expiration, such as 90 days, and select Create API token.
- Copy the token from Your API token. It is shown only once.
The query
Copy the query below, or download costfluent-costs.pq. Replace the three values at the top: ApiToken with your token, WorkspaceId with the workspace's ID, and Months if you want more or fewer than 12 months. Months takes a whole number of 1 or more; any other value stops the query with an error that says so.
Code
Load it in Power BI Desktop
- On Home, open Get data and select Blank query. The Power Query Editor opens.
- Select Advanced Editor, replace its contents with the query, and select Done.
- When Power BI asks how to connect to
https://api.costfluent.com, choose Anonymous and select Connect. The token travels in the query's ownAuthorizationheader, which Power Query sends only for an anonymous source. - Rename the query, such as
Costfluent costs, and select Close & Apply.
Load it in Excel
- On Data, open Get Data, then From Other Sources, and select Blank Query.
- Select Advanced Editor, replace its contents with the query, and select Done.
- When Excel asks how to connect to
https://api.costfluent.com, choose Anonymous and select Connect. - Select Close & Load. The costs land in a table on a new sheet; Refresh All on Data reads them again.
The token is stored in the query, in plain text, so anyone who opens the .pbix or the workbook can read your costs with it. Before you share the file or publish the report, replace the token with a placeholder, or keep the file where only the people allowed to see these costs can open it. When someone who had the file leaves, or the token may have leaked, revoke it under API tokens with Revoke token and create a new one.
How it behaves
- Each row is one month, service and sub-account, with
Cost,Amortized costandList costas numbers andCurrencybeside them. The query reads with the Public API's defaults: amortized, refunds and tax included, credits left out; see Cost amounts. A month, service and sub-account with no cost data has no row: a missing row means Costfluent holds no data for it, not a cost of zero. The current month runs only to the latest day the providers have delivered. - The plan's history limit applies; the plan marker above links to each plan's limits. When the plan keeps fewer months than
Monthsasks for, the API reports the earliest day it keeps, and the query starts at the first whole month from that day, so no month shows only part of its cost. The earliest month in theMonthcolumn shows where the plan's history begins. - The API sends every amount as an exact decimal. Power BI and Excel hold a number with 15 significant digits, so an amount with more digits is rounded in its last places; the CSV export is exact.
- One read reaches 100,000 rows, 20 pages of 5,000. When the whole window holds more, the query reads it one month at a time. A single month with more than 100,000 rows fails with the API's error; remove
"subAccount"fromgroupByto need fewer rows, which leaves theSub-accountcolumn empty. - Each refresh sends one request to learn how many rows the period holds, then one per page of up to 5,000 rows, with at least one for the whole period, or at least one per month when the read is split. When the plan's history does not move the start, the first page repeats that first request, which Power Query may answer from its cache. Every request counts against the token's limits; see Rate limits.
- When the API answers
429because the token reached its rate limit, Power Query tries the request again up to three times, each time after the seconds the API'sRetry-Afterheader names, as Microsoft documents forWeb.Contents(Handle status codes with Web.Contents). A refresh that reaches the rate limit therefore slows down. A request the API still refuses after that, or refuses with another error, fails the refresh with the API's error, and the next refresh tries again. - An expired or revoked token makes the API answer
401, which Power Query reports as a credentials error and may answer by asking for credentials. Changing the credentials there does not help, because the token travels in the query: create a new token and replace it in the query. - Microsoft limits refreshes in the Power BI service: up to 8 scheduled refreshes a day with Power BI Pro and 48 with Premium, Premium Per User or Fabric capacity, each finishing within two hours on shared capacity, and a schedule pauses after two months without a visit to the report (Data refresh in Power BI).
Refresh in the Power BI service is not tested by Costfluent. Microsoft documents that a web source called with a fixed base URL and its RelativePath and Query options, as this query does, can be refreshed in the service, where a URL built at run time cannot. It does not document the credentials such a query needs there. After publishing, set the source https://api.costfluent.com to Anonymous under the semantic model's Data source credentials and select Skip test connection; if the refresh still fails, refresh in Power BI Desktop and publish again.