Solving a(n) issue with grammar in Tableau

Tableau is fantastic at letting us see and understand our data. But more often than not, I'm face with the challenge of getting my grammar correct.The devil is in the detail and it simply seems a bit unprofessional, when grammatical errors are showing up time and time. It's these details that help you turn your dashboard from good to spotless! One of the reoccurring issues, is that the text in my tooltips is hardcode text that does not take my dynamic values into account. An example is the a/an issue of the indefinite article. With that said, lets look at an example of how we can make Tableau set the correct indefinite article (a or an) on our dashboard.

In the picture below I'm looking at an AirBnb dashboard, showing listings in Barcelona. I have three types of listings: Private Room, Entire home/apt. and Shared Room. When I hover over the points, a tooltip appears and provides me information about the listing:

Right now my Tooltip says that the user Luna&Selene "is renting out a Entire home/apt." The correct sentence would of course be an Entire home/apt. Similarly you would never say "a apple" or "an house". So how do we tell Tableau how to fix this? Well, logic of the indefinite article is very tricky to convert into code, as the article is based on the wording of the first letter of a word, and not the first letter itself. E.g. we say both "an hour" and "a house", despite both start with an H. So what to do? There is of course the easy way, which is to reformulate your sentence, but that's not what we are looking for here. Instead we will create a calculated field where we can decide what should be done.

In this case, we only have 3 types of listings, so this can be solved in a jiffy. I create a calculated field called "A/an" that uses the following logic:

IF CONTAINS([Room Type], "entire") then "an" else "a" END

I can now edit my Tooltip and replace the hardcoded "a" with my new field named "A/an" (in row 3):

When i hover the points on my map, I now see a correct usage of the the indefinite article:

Seeing that this example only has 3 types of listing, it's rather effective for me to just create a calculated field. But if you have hundred of dimension members, this will be time consuming and perhaps too much an effort for the effect of correct grammar. In that case you we can improve our calculation a bit. It won't be 100% perfect but it will definitely be an improvement. Most words starting with a,e,i,o or u uses the article an and the rest uses a. When I say most words, it because this is not 100% bulletproof (think back to "an hour" as mentioned earlier). But lets see how we can create an improved version of or A/an calculations:

IF STARTSWITH(UPPER([Room Type]),"A") orSTARTSWITH(UPPER([Room Type]),"E") orSTARTSWITH(UPPER([Room Type]),"I") orSTARTSWITH(UPPER([Room Type]),"O") orSTARTSWITH(UPPER([Room Type]),"U")then "an" else "a" END

The code above takes into consideration the first letter of the room type and evaluates wether it starts with a,e,i,o or u. If it does, "an" will be selected. If not, "a" will be our result. I use the UPPER function to make sure both words starting with an upper- and lowercase letter will be included in the logical statement. Again, let me emphasize that we do have grammatical irregularities, but this calculation will solve most of our a/an issues.

Lastly a short disclaimer: Grammatical errors in this blog post may appear.

Back to blog

Subscribe for updates