---
schema_version: "1"
id: 705410daf6e9a169842779da4118eb7637f54bfff37bcf7dc56ae15e68b14b21
title: Add a Calculated Column to a View
collection: tableau
version: current
locale: en-us
portal_url: https://docs.salesforce.com/tableau/current/en-us/public/desktop/calcs_add_calculated_column.md
canonical_url: https://help.tableau.com/current/public/desktop/en-us/calcs_add_calculated_column.htm
source_site: help.tableau.com
content_type: documentation
content_hash: e5a9e001acfe5a6451d2ec67da8f845d035861bcfc92899be0dbc6622d1ea5fb
description: Add a calculated column to modify existing data, such as combining names or finding percent change.
---
[Skip to main content](#main-content)

# Add a Calculated Column to a View

Sometimes your data may not have all the pieces you need for your analysis. A common way to handle this is to create a calculated column (also referred to as a calculated field or just a calculation) that modifies the existing data. Calculations can be created by opening the **Analysis** menu and selecting Create Calculated Field...

It's important to note that there are often several different ways to achieve the same results for a calculated field. Additionally, the correct value of a calculation may depend on how the data is shaped and how the visualization is set up. Make sure you understand your data structure and how the viz will be built when determining how to write a calculation.

## Examples of when a calculation might be useful

### Combine first and last name

You may have First name and Last name in two different columns and want a single field for name.

![table with three columns titled first name, last name, and real person](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex1data.png)

The calculation:

\[First name\] + " " + \[Last name\]

A finished viz might look like this

![A viz with the fields First name and Last name on rows, and Full name on Text](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex1viz.png)

### Find a sequence inside a string

You may want to flag every record that has a certain manufacturing code XYZ in the Product ID field.

![A data table with three columns, the first is Product ID](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex2data.png)

The calculation will return "true" if the code is present and "false" if not.

CONTAINS(\[Product ID}, "XYZ")

A finished viz might look like this, with the calculation on Color.

![A viz with Product ID on rows and the calculated field XYZ Manufacturing on Color](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex2viz.png)

### Assign categories for value ranges

You want to assign categories based on several thresholds.

![A data table with two columns, Student and GPA](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex3data.png)

The calculation will evaluate each student's GPA against the cutoff value and assign a label.

IF \[GPA\] <3 THEN "Needs Improvement" ELSEIF \[GPA\] <3.5 THEN "Sufficient" ELSE "Excellent" END

A finished viz might look like this, with the calculation on Color.

![A viz with Student on Rows, GPA on Columns, and the calculated field on Color](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex3viz.png)

### Calculate percent change

You want to display the year-over-year (YOY) percentage change in the weight of children from age 1 to age 2.

![A data table with three columns, one for Baby (ID), one for Age, one for Weight](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex4data.png)

This calculation is for the _difference/original_ for each child.

(SUM((IF \[Age\] = 2 THEN \[Weight\] ELSE 0 END)) - SUM((IF \[Age\] = 1 THEN \[Weight\] ELSE 0 END)))/SUM(IF \[Age\] = 1 THEN \[Weight\] ELSE 0 END) 

A finished viz might look like this.

![A viz with Baby on Rows and the calculated field on Text](/tableau/current/en-us/public/desktop/en-us/Img/createcalcex4viz.png)

This example is one where the calculation could change quite a lot based on the structure of the data. If there was a field for weight at age 1 and weight at age 2, for example, the calculation would only need to be (\[Weight at age 2\] - \[Weight at age 1\]) / \[Weight at age 1\]. However, the data structure detailed above is more standard and more likely to be encountered.

## Follow along: create a YOY calculation

### Objective

To create a view like the following, that shows sales results for two years in the first two columns, and then the year-over-year change, as a percentage, in the third column. The scenario uses the Sample - Superstore data source provided with Tableau Desktop to show how to build the viz.

![A table illustrating the year-over-year shift in sales by product sub-category from 2013 to 2014.](/tableau/current/en-us/public/desktop/en-us/Img/yoy1.png)

### Create the necessary calculated fields

1. Connect to the Sample - Superstore data source.
2. Choose Analysis > Create Calculated Field to open the calculation editor. Name the calculation 2013 and type or paste the following in the formula area:
    
    `IF YEAR([Order Date]) = 2013 THEN [Sales] ELSE 0 END`
3. Create a second calculated field and name it 2014; the formula is the same except with 2014 instead of 2013:
    
    `IF YEAR([Order Date]) = 2014 THEN [Sales] ELSE 0 END`
4. Create a third calculated field, YOY Pct. Change, to calculate the change from 2013 to 2014:
    
    `(SUM([2014]) - SUM([2013])) / SUM([2013])`

### Format the calculated fields

1. Click 2013 in the Measures area of the Data pane and choose Default Properties > Number Format.
2. In the Default Number Format dialog box, set the format to Currency (Custom) and the Decimal places to 0.
    
    ![A dialog box with options for formatting the numbers as currency.](/tableau/current/en-us/public/desktop/en-us/Img/yoy2.png)
3. Format the 2014 measure the exact same way.
4. Format the YOY Pct. Change field as Percentage, with 2 decimal places.

### Build the view

1. Drag 2013 to Text on the Marks card.
2. Double-click 2014 and then YOY Pct. Change.
3. Drag Measure Names from Rows to Columns.
4. Drag Sub-Category to Rows.
    
    Your view should now look like the one at the beginning of this section.