Using Calculated Fields on the Filters Shelf

Published on December 2, 2015

Scenario:  The San Francisco Police Department (SFPD) wants to analyze the total number of incidents of offense in different years and compare them. To help the SFPD with the analysis, you can use Area Charts. Now, if you want to show selected number of years in the visual, you may want to use years as filter and pick values for years, but you can’t because years is a calculated field derived from date.

In this post, I’ll show you how you can filter calculated field values on the dataset SFPD Reported Incidents for 2014 [data source]. Prior to importing the data, I changed the Date field to the format yyyy-mm-dd.

Create an Area Chart

  1. Create a new Area Chart visual.
  2. Populate the shelves from the available fields (Dimensions and Measures) listed in the left navigation menu.
    • Under Dimensions, select Category, drag and drop it on the X shelf.
    • Under Measures, select incidntnum, drag and drop it on the Y shelf. Set its Sort Order to Descending.
    • Under Dimensions, select Year, drag and drop it on the Colors shelf.
    • Place pddistrict, Category, and Year to the Filters shelf, and select appropriate values for these variables.

The visualization for number of Incidents in different years looks like this:

rtaimage

Notice the Filters shelve containing filters for pddistrict, Category, and the calculated field Year. If you want to visualize information for selected years, you can select values for Years on the Filters shelf.

rtaimage01

If you click save and refresh the visualization, it should show information for only the years you selected, 2008 and 2015, but it doesn’t. This happens because you can drop calculated fields on the Filters shelf, but cannot select values for the filter. You must first create a copy of this calculated field as an attribute, and then use it as a filter.

Use a Calculated Field as a Filter

  1. Go to the data source, click on Attributes, and create a copy of Years field with the following changes in the Column View window.
rtaimage02
  1. Click New Visual and create an Area Chart.
  2. Drag the newly created attribute Year as Attribute to the Filters shelf, and pick values 2008, 2011, and 2015:
rtaimage03
  1. Click Refresh Visual.
  2. The visual now shows information only about the selected years.
rtaimage04

I suggest you use this method whenever you wish to use a calculated field as a filter.