From the course: Tableau 2024.1: Essential Training

Create a calculated field

When you analyze your data in Tableau, it's possible that your original data source might not contain all of the values that you want. For example, you might know the quantity and price of items sold, but not the total sales. I have it in this workbook, but for this exercise I will assume that I need to calculate it. The workbook I'll use is 04_02_Calculated, and you can find it in the Chapter_04 folder of the Exercise Files collection. I've started my visualization and it's a text table broken down by product category. And over on the left I can see that I have data for the price and quantity of items in each sale. With that information, I can calculate the total sales. So I will go to the analysis menu, and then almost all the way down. I'll click create calculated field. The calculation dialog box open and I will call it total sales. And then click in the editing box below. I'm going to be using the price and quantity fields, so I'll start typing those in by typing a left square bracket. And the first one I'll use is price. And price appears at the top of the autocomplete list and it's highlighted, so I'll press tab, and I see it in orange. And the dialog box I want to multiply price by quantity, so I'll type an asterisk, then left square bracket and Q for quantity it's highlighted. So I'll press tab, and I see below that the calculation is valid. That's great. I'll click okay. And I have a new total sales field over here and my measures area, and I can tell it's a calculated field because there is an equal sign next to the hash tag indicating that it is a measure. So I'll add total sales to the viz by dragging my new pil onto the text tables data area, and there I have the values. Now I happen to have the order total field also available, so I will drag that onto the data area as well, and I see different numbers. So order total, which is the built in value, is less than total sales., and that's true for every last one of the numbers. So the question is why is that happening? Well, as it turns out there is also a discount field. So some of our customers get a discount on their purchases. So that means that I need to go in and edit the calculated field to include that. So I go to turtle sales and click that pills down arrow. Then click edit. So I have price, times quantity and I happen to know from my analysis of the data before that the discount is a positive number. So you might get a 10% or 15% discount. So that means I need to multiply by in parentheses one minus the value in the discount field. So I have a left square bracket followed by the D discount is highlighted, so I press tab, right parentheses again, and I see that the calculation is valid. And I'll click okay. And you can see that the calculation has updated. There are some minor differences based on rounding, but the values are very very close. So I wouldn't use this for accounting. but for a visualization it should be fine. If you want to rename your calculated field, then you can go over to the fields pill, click the down arrow, click rename, and in this case I will call it Total Sales Calc, just to reinforce the fact that it is a calculated field. And if I want to delete the calculation, I can click the down arrow again, click delete, and then confirmed by clicking Yes. And the field has been removed. In the best case, all the information you want will be in your original data source. But if it's not, a calculated field will let you figure out what you need to know.

Contents