Use Pivot Results
In this section we’ll learn how to filter our pivot table results and write formulas with them.
The end output we want is a table that has only comedy movies ordered by number of users descending, with another column that shows the percent of total users that rated the movie like below:
Filter for comedy movies
First let’s update our pivot to filter for comedy movies.
-
Go to the pivot1 tab and click on the Add button next to Filters in the Pivot window.
-
Add
is comedy?
field. -
You’ll see an error message that we need to remove the Sort from the pivot table in order to edit it.
-
Go to the Sort tab in the right pane.
-
Remove the
counta(userId)
sort by clicking the X button. -
Click Apply.
-
You’ll see the processing dialog for a quick second and then you’ll still see the popular movies at the top. This is because removing sorts won’t change the order of a table. You need to apply with new sorts to change the order.
-
Now we can go back to the Pivot tab and add the
is comedy?
filter. -
Click on the Search box and filter for
TRUE
. -
As usual, click on Apply to run the calculation. We should see 16,043 rows now instead of ~59K because we are filtering for comedy movies.
-
We lost our ordering of the most popular movies at the top because we recalculated the pivot table. Add the sort back using the steps described in the Sort section. The end result should look like the below screenshot.
Notice that movies without the Comedy genre tag have disappeared, such as Shawshank Redemption and Silence of the Lambs.
Copying pivot values to new table for formulas
We want to insert a calculation column to our pivot table that calculates the percent of total users that have rated the movie. But there is no button to add the column like in the Add Columns and Formulas section.
The solution is simple — we need to copy the pivot values to a new table first. Then we can treat it like a regular data table instead of a pivot table.
-
Click on the dropdown arrow in the pivot1 tab.
-
Select Copy values to new table.
-
Ensure pivot1 is selected as the Source table, and click COPY TO NEW TABLE.
-
In the new pivot1_copy table that was created, you can now click the + button to insert a new column.
-
Name the new column percentOfUsers.
-
Enter the formula
=pivot1_copy.'counta(userId)' / 162541
for the new column. You can use the mouse or keyboard to select thecounta(userId)
column in the formula as described in Add Columns and Formulas section.162541
is the result from thenum unique users table
created in section Sum, Count, Aggregate. -
As usual, click Apply or Shift+Enter on the keyboard to calculate the new column. We can see that ~50% of users have watched and rated Forrest Gump and Pulp Ficton.
-
[Optional] Let’s rename the table to
comedy_viewership
so that it is more descriptive of the contents.