It it sometimes the case that the display totals feature in Looker does not match the expected total of the returned values, which can lead to confusion. The reason for this is that Looker totals do not calculate a simple sum over the rows or columns in our report, but rather execute the given measure’s aggregation over the underlying SQL, without the grouping applied in the look. This can lead to a discrepancy between Looker totals, and expected totals in certain scenarios.

### Why Totals Can Be Lower

Let’s start with an example. Say we are counting the number of distinct users who bought a certain category of product over a period of time:

The SQL used to create this table looks like this:

```
SELECT
products.category AS `products.category`,
COUNT(DISTINCT users.id ) AS `users.count`
```

Notice that if we tally up each of the categories ourselves, we get a number much higher than the total reported by Looker. Since each user can make purchases from multiple categories of products, when we add up each row ourselves, we are double-counting some users. But Looker uses a SQL query to get the count of distinct users and avoids double counting.

The SQL used to calculate this total looks like this:

```
SELECT
COUNT(DISTINCT users.id ) AS `users.count`
```

Notice that Looker uses the SQL from the `users.count`

measure to calculate the total. Any filters and joins we add will also be included in the totals query, which will keep the total consistent as the query changes.

### Why Totals Can Be Higher

There are two situations where Looker totals can actually be higher than the totals we get by adding up the rows: when the query is filtered on a measure and when the query is hitting the row limits. This is because our totals are being calculated *before* these filters/restrictions are applied.

In the case of row limits, the total query that Looker creates isn’t subject to those limits because the total is only ever going to return a single row

If you want to calculate totals on just the values in the table, you can create a new column using table calculations. Here are some examples.

For a column total:

```
sum(${view_name.field_name})
```

For a row total:

```
sum(pivot_row(${view_name.field_name}))
```

Note this approach will generate a new column, and not appear as a native ‘Total’ row or column.

## Comments

0 comments

Article is closed for comments.