The 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 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
Suppose you want to count the number of distinct users who bought a certain category of product over a period of time. You can create an Explore query with Users Count grouped by Products Category:
The SQL used to create this table looks like this:
SELECT products.category AS 'products.category', COUNT(DISTINCT users.id ) AS 'users.count'
With the Totals option enabled, the total users is 12,290.
Notice that if you manually add the values in the Users Count column, you get a number that is much higher than the total that is reported by Looker. Because each user can make purchases from multiple categories of products, when you add up each row yourself, you add some users more than once. However, Looker uses a SQL query to calculate the count of distinct users and in this way avoids double-counting.
The SQL that Looker uses to calculate the 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 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 you get by adding up the row values: when a query is filtered by a measure, or when a query is hitting the row limit.
- When the query is filtered by a measure, totals are being calculated before 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, you can create a new column using table calculations. See the following examples for details.
Calculating a column total
To calculate a total of column values, you can use the
sum function in a table calculation:
Alternatively, you can use the Running column total shortcut calculation when you have the permissions to create table calculations.
Calculating a row total
To calculate a total of row values, you can use the
pivot_row() functions in a table calculation:
Note: This approach will generate a new column and will not appear as a native "Total" row or column.
Alternatively, you can use the Running row total shortcut calculation when you have the permissions to create table calculations.
Some table calculations that perform aggregations, such as calculations that use
median, might also not add up as you expect. This is because table calculations calculate totals by using the values in the Total row, not by using the values in the data column. See the Display potentially confusing table calculation totals as Null Best Practices page for troubleshooting tips.