The Display Totals feature in an Explore works by totaling the distinct underlying data, not just the rows and columns that are visible in the data table. This means that Display Totals might return a value that is different from what you were expecting if you were viewing just the visible data. Keep reading for more details and solutions for calculating the total you need, or check out this video tutorial that explains how Column Totals are calculated in Looker.
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. Because each user can make purchases from multiple categories of products, when we add up each row ourselves, we are double-counting some users. However, Looker uses a SQL query to get the count of distinct users and in this way 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
Two situations can occur where Looker totals can actually be higher than the totals we get by adding up the rows: when the query is filtered by a measure, or when the query is hitting the row limit.
- When the query is filtered by a measure, totals are being calculated before these filters or 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.
How to Calculate Value Totals
To calculate totals on just the values in the table, we 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.