# Connect FOCUS in BigQuery

Costfluent reads FOCUS cost straight from a table in BigQuery: Google's own FOCUS export of a
billing account, or a table or view you keep with FOCUS columns. Nothing is exported to a bucket.
Costfluent reads the table through the service account it created for your organization in its own
Google Cloud project, the same one your Google Cloud connections use, and you grant that account
read access to the table's dataset. No key is created or exchanged.

One connection is one table or view. To read several, connect each of them.

## How Costfluent connects

**BigQuery Data Viewer** on the dataset that holds the table, and nothing else. Costfluent runs its
queries in its own project and pays for them there, so it needs no role in your project, cannot see
any other dataset, and cannot change anything.

The queries count against your organization's daily BigQuery budget, which it shares with its
[Google Cloud](/connect/google-cloud) connections. Each read selects the rows whose
`ChargePeriodStart` falls in one month, or in one day once a month holds more than one read
takes. How much of the budget a read uses depends on how the table is stored:

- **A table partitioned by day on `ChargePeriodStart`**, or a view over one, is read only for the
  days the read asks for.
- **Any other table** is scanned whole on every read, so a large one uses the budget quickly.

Once the day's budget is spent, collection waits until 00:00 UTC and goes on from there. A table so
large that one scan of it needs more than the whole daily budget cannot be collected at all; see
[Troubleshooting](#troubleshooting).

The connection is identified by its table, `<project>.<dataset>.<table>`: the same table connected
a second time is refused.

## Prerequisites

1. The Owner or Integration owner organization role in Costfluent.
2. A BigQuery table or view with FOCUS cost data, as under **Choose the table**.

## Choose the table

### Google's FOCUS export

Google writes a billing account's cost in FOCUS columns to BigQuery when you turn on the FOCUS
export in the Google Cloud console under **Billing**, then **Billing export**, then **FOCUS**. Its
table is named `gcp_billing_export_focus_<BILLING_ACCOUNT_ID>`.

:::caution
Google offers the FOCUS export as a Preview, and says its table's schema can change over time and
recommends reading it through views. Costfluent checks the columns it needs when you connect, and
names a missing one ([Troubleshooting](#troubleshooting)).
:::

### A table or view of your own

Any table or view with FOCUS columns can be connected. Its name may hold letters, digits,
underscores and hyphens only: no wildcard, no `$` partition suffix and no dot.

Every table needs these FOCUS columns: `BilledCost`, `BillingAccountId`, `BillingCurrency`,
`ChargeCategory`, `ChargePeriodEnd`, `ChargePeriodStart` and `ServiceName`. They are matched as FOCUS
spells them, case included, and `ChargePeriodStart` must be a `TIMESTAMP`. Costfluent reads the other
FOCUS columns where the table has them.

A view is read with the service account's own access, so the account needs **BigQuery Data Viewer**
on the datasets of the tables under the view too, unless the view is authorized on them.

## Connect

<Stepper>

1. Open **Settings**, then **Integrations**, select **FOCUS in BigQuery**, and open the **Connect**
   tab.
2. Select **Add table**. Under **Service account**, Costfluent shows the service account it created
   for your organization, with a button to copy its address. The address is the same every time you
   open the dialog.
3. In the sharing settings of the table's dataset in Google Cloud, grant that address
   **BigQuery Data Viewer**. If your organization restricts sharing to known domains, first allow the
   **Organization ID** and **Customer ID** Costfluent shows beside the address.
4. Back in the dialog, keep the suggested **Display name** or enter your own, then enter the
   **Project ID of the table**, the **BigQuery dataset name** and the **Table or view name**.
5. Select **Test connection** to see each check, or **Connect** to save.

</Stepper>

Before storing anything, Costfluent runs a dry run of one day's read as the service account, which
BigQuery does not charge for. It proves that the dataset is shared, that the table exists and that
it has the columns under **Choose the table**. If Google does not answer during that check, the
connection is saved anyway and checked again automatically.

## Data and freshness

Connecting imports as far back as your plan's backfill reaches, and at most six months
([Manage connections](/connect/manage-connections#how-it-behaves)), as long as the table holds rows
that far back. Cost arrives by the day of each row's `ChargePeriodStart`, in the row's
`BillingCurrency`.

:::note
A billing account that a [Google Cloud](/connect/google-cloud) connection also collects is in both
connections' cost, so it would count twice. Costfluent never refuses such a connection: once the
table's rows name the billing account, both connections' pages show **Cost counted twice**, naming
the account and the other connection. Keep one of the two: remove the other connection, or replace
this one with a connection of a view that leaves the billing account out.
:::

## Reporting dimensions

What each group-by dimension of a [cost report](/cost-reports/explore) holds for a FOCUS in
BigQuery connection, and the FOCUS column it comes from.

| Dimension | For FOCUS in BigQuery | From |
|---|---|---|
| **Seller** | The provider the row names, such as Google Cloud; `BigQuery` for a row that names none | `ServiceProviderName`, or `ProviderName` in tables before FOCUS 1.3 |
| **Sub-account** | The sub-account the row states, the project in Google's export; the billing account for a row without one | `SubAccountId`, else `BillingAccountId` |
| **Service** | The provider's service | `ServiceName` |
| **Region** | The region's name the row states | `RegionName` |
| **Resource** | The resource's name the row states | `ResourceName` |
| **Charge category** | The charge category the row states | `ChargeCategory` |
| **Service category** | The FOCUS category the row states; for a row without one, as in Google's export, its service's category, such as Compute for Compute Engine, or Other for a service Costfluent does not know | `ServiceCategory`, else `ServiceName` |
| **SKU** | The meter the row states; the SKU ID where it states none | `SkuMeter`, else `SkuId` |
| **Tag** | Every key of the row's tags, as written; for a row without tags, the labels of Google's export | `Tags`, else `x_Labels` |
| **Segment** | Your allocation rules | [Segments and allocation rules](/allocation/segments) |
| **Cluster**, **Namespace** | Never filled; they hold Kubernetes cost ([Connect Kubernetes](/connect/kubernetes)) | |
| **Virtual tag** | Your virtual tag rules | [Virtual tags](/allocation/virtual-tags) |

## Update or disconnect

### After connecting

The **Manage** tab lists the connection by its display name, with its **Table**, **Project** and
**Dataset**. Open the gear icon beside it to see whether the table is still readable, which
workspaces the connection feeds, and which months have been imported. **Run checks** runs the checks
again on request.

### Rotating or removing access

There is no key to rotate. To stop Costfluent reading the table, remove the service account's
**BigQuery Data Viewer** grant on the dataset, then remove the connection in Costfluent. Removing it
erases the cost it imported ([Remove a connection](/connect/manage-connections#remove-a-connection)).
To read another table, connect it as a new connection.

The service account is the one your Google Cloud connections use, and Costfluent deletes and
recreates it as [Connect Google Cloud](/connect/google-cloud#rotating-or-removing-access) describes.

## Troubleshooting

- **"The dataset … is not shared with … yet."** Grant the service account **BigQuery Data Viewer**
  on the dataset the message names, and try again a few minutes later, since a new grant can take
  that long to apply.
- **"The table … was not found."** Check the project ID, the dataset and the table's name. A table
  name is entered without its project and dataset.
- **"The table … has no column …, which FOCUS requires."** The table lacks one of the columns under
  [Choose the table](#choose-the-table), or spells it in another case. Add or rename the column, or
  connect a view that does.
- **"The table … has no TIMESTAMP column ChargePeriodStart."** The column is missing or has another
  type. Connect a view that casts it to `TIMESTAMP`.
- **The table name is refused before anything is sent.** It holds a wildcard, a `$` partition suffix,
  a dot or another character BigQuery does not allow in a table name. Enter the table's own name.
- **Collection is postponed.** The organization's daily BigQuery budget is
  spent, by this table's reads or by your Google Cloud connections. Collection goes on after
  00:00 UTC. Partition the table by day on `ChargePeriodStart`, or connect a view over a table that
  is, so each read scans only its own days.
- **A day fails because its query "needs more than the organization's whole daily query budget".**
  One scan of the table needs more than the whole daily budget, so no day can be read. Connect a
  table partitioned by day on `ChargePeriodStart`, or a view over one, or
  [contact us](https://costfluent.com/contact).
- **Google Cloud cost is counted twice.** See the note under
  [Data and freshness](#data-and-freshness).

## From your assistant

With the [Costfluent MCP server](/developers/mcp) connected, your assistant answers these with `query_costs` and `list_anomalies`.

```text
Which sellers named in our FOCUS table in BigQuery cost the most last month, and in which service categories?
```

```text
Which open cost anomalies come from services in our FOCUS table in BigQuery, and which project contributed to each?
```
