Have you ever found yourself struggling to visualize a measure associated with a specific member of a field? I know I have. Therefore in this blog post, I will show you how to return that measure in a calculated field using a logical statement.
Calculated fields are an essential feature in Tableau that allow you to create new dimensions or measures by combining, transforming, or aggregating existing data in your data source. They enable you to perform complex calculations on your data and display them in your visualizations.
Logical statements, on the other hand, are used to filter or conditionally format data based on specific criteria. They are typically written with comparison operators (e.g. >, <, >=, <=, =), logical operators (e.g. AND, OR, NOT), and other functions. The result would be either True or False, depending on whether the conditions are met.
With those definitions out of the way, let’s examine some examples of a logical statement serving as a filter in a calculated field.
An example of a logical statement in a calculated field is in my Survivor dashboard (which I write about here):
Do you see the donuts with the percentages of winning as Sole Survivor?
That percentage is the result of a calculated field with a logical statement. This is the calculated field:
Let’s break this calculation down.
I want a percentage of Sole Survivor winners; thus, I need a fraction with the number of Sole Survivors as the numerator and the number of Castaways as the denominator.
For the numerator, I need a logical statement: if the result is being the Sole Survivor, return the Castaway ID. Then count the distinct Castaway IDs.
On the other hand, the denominator is a distinct count of all Castaway IDs. #nofilter
More recently, in this Makeover Monday challenge, I used calculated fields to isolate vegan protein sources, as well as vegetarian protein sources.
The following calculated fields are used in the sloped dumbbell visualization. They calculate the average cost of 20 grams of protein of vegetarian/vegan protein sources and other protein sources.
Let’s review the vegetarian/vegan-related calculated field first.
This calculated field looks a little overwhelming, but it also includes my Parameter, Vegan or Vegetarian.
If the “VEGETARIAN” parameter is selected from the dropdown, then return the average cost of vegetarian protein sources. The first line specifies the condition of the parameter and the second line returns the average - if the protein source is vegetarian, then return the cost.
Otherwise, if “VEGAN” is selected, then return the average cost of vegan protein sources. The third line specifies the condition of the parameter and the fourth line returns the average - if the protein source is vegan, then return the cost.
There are logical statements nested in a logical statement!
This calculated field is the opposite of the one above.
Can you explain this in plain English?