How to Truncate a Zip Code in Tableau

Cody Schneider••7 min read

Working with zip codes in Tableau can feel like a simple task until it suddenly isn’t. You import your data, but maps won't render correctly or your zip codes look wrong, with critical leading zeros missing. We’ll show you how to fix these common issues by correctly formatting and truncating zip codes in Tableau.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Why Tableau Fumbles Zip Codes

The core of the problem is that zip codes are identifiers, not numbers to be used in calculations. A zip code like 07030 (Hoboken, NJ) is not the same as the number 7,030. But because they are made of digits, Tableau and other tools often misinterpret them and default to a numeric data type when your data is imported.

This creates two major headaches:

  • Missing Leading Zeros: Many zip codes, particularly in the northeastern United States, start with a zero. When Tableau reads 07030 as a number, it drops the leading zero and stores it as 7030. This makes it impossible to map accurately, as 7030 is not a valid U.S. zip code.
  • ZIP+4 Formatting: Your data might contain extended ZIP+4 codes, like 90210-5555. Tableau will correctly read this as a string of text (because of the hyphen), but you can't map it directly. You need to truncate it to get the standard 5-digit code (90210) that Tableau recognizes for mapping.

Fortunately, you can solve both problems with a few simple steps, usually involving calculated fields. Let's walk through the exact methods.

Method 1: Change the Data Type to String

The simplest fix is often the right one. Before diving into calculations, check if your zip code field is accidentally set as a number. If your data already has the correct 5-digit format but isn't working, changing the data type might be all you need.

This is also the first step you need to take before using functions to fix leading zeros or truncate ZIP+4 codes.

How to Change a Field’s Data Type:

  1. Navigate to the Data Source tab or find your field in the Data pane on the left side of your worksheet.
  2. Locate your zip code field. If it's a number, it will have a number sign icon (#) next to it.
  3. Click the icon.
  4. Select String from the dropdown menu. The icon will change to "Abc".

Now that the field is treated as text, you can tell Tableau it represents a geographic location.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

How to Assign a Geographic Role:

  1. In the Data pane, right-click your newly converted zip code field.
  2. Hover over Geographic Role.
  3. Select ZIP Code/Postcode from the menu.

A globe icon will appear, indicating Tableau now understands how to map this data. If this step solves your problem, great! If you still have missing zeros or need to shorten ZIP+4 codes, move on to the next methods.

Method 2: Fix Missing Leading Zeros with LPAD

The most common issue by far is the disappearance of leading zeros. The fix is to create a calculation that pads the shortened number with a '0' at the front to restore it to its 5-digit format.

Previously, this required a somewhat cumbersome IF LEN... statement, but newer versions of Tableau include the elegant LPAD() function, which stands for "left pad." It adds a character to the left side of a string until it reaches a desired length.

Using the LPAD() Function:

  1. Right-click in an empty area of the Data pane and select Create Calculated Field.
  2. Name your new field something clear, like "ZIP Code (Fixed)".
  3. Enter the following formula, replacing [Your Zip Field] with the name of your zip code field:

LPAD(STR([Your Zip Field]), 5, '0')

Let’s break down what this does:

  • STR([Your Zip Field]): First, it converts your zip code field into a string (text). The LPAD function only works on strings.
  • LPAD(..., 5, '0'): It then checks the length of that string. If it's less than 5 characters long, it adds **'0'**s to the left side until it is 5 characters long.

Here’s the result:

  • 7030 becomes '07030'
  • 9123 becomes '09123'
  • 90210 stays '90210' (because it's already 5 characters)

Click OK to save the calculated field. Remember to assign this new field a Geographic Role of "ZIP Code/Postcode" as described in the first section.

Method 3: Truncate ZIP+4 Codes to 5-Digit ZIPs

If your dataset includes the extra four digits on your zip codes (e.g., 10001-1234), Tableau won't be able to map them. You need to trim off the hyphen and the four digits that follow, leaving just the core five digits. There are two excellent functions for this: LEFT() and SPLIT().

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Using the LEFT() Function

The LEFT() function is the most direct way to accomplish this. It simply grabs a specified number of characters from the left end of a string.

Steps:

  1. Create a new Calculated Field. Name it "ZIP Code (5-Digit)".
  2. Enter this formula, replacing [Your Zip+4 Field] with your field name:

LEFT([Your Zip+4 Field], 5)

This command tells Tableau to take the text value in your zip code field and return only the first 5 characters from the left. Simple and effective.

  • 10001-1234 becomes '10001'
  • 90210-9876 becomes '90210'

Using the SPLIT() Function

The SPLIT() function is another great option, especially if your data is structured with a consistent separator like a hyphen. This function divides a string into parts based on a delimiter and returns one of the parts.

Steps:

  1. Open the Create Calculated Field window again.
  2. Enter this formula:

SPLIT([Your Zip+4 Field], '-', 1)

Here's how this works:

  • SPLIT(...): is the function call.
  • [Your Zip+4 Field]: identifies the string to be split.
  • '-': is the delimiter. Tableau will break the string every time it encounters a hyphen.
  • 1: is the "token number." It tells Tableau to return the first part of the split string.

In the case of 10001-1234, the split creates two parts: '10001' and '1234'. The formula returns the first part: a clean, map-ready 5-digit zip code.

Best Practice: A Single Calculation for Messy Data

In the real world, your zip code column might be a mix of everything: 4-digit numbers, 5-digit ZIPs, and full ZIP+4 strings. Instead of creating multiple fields and conditional logic, you can combine the functions we’ve reviewed into one powerful, all-in-one calculated field.

This elegant formula handles all cases at once: converting to a string, splitting off the "+4" part if it exists, and padding with leading zeros if necessary.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

The Combined Formula:

LPAD(SPLIT(STR([Your Zip Field]), '-', 1), 5, '0')

This works by nesting the functions in the order of operations needed:

  • STR([Your Zip Field]): It starts by ensuring the data is a string.
  • SPLIT(..., '-', 1): It then splits the string by the hyphen. If there's no hyphen (like in 7030 or 90210), this function just returns the original string. If there is one (like in 90210-1234), it returns just the first part (90210).
  • LPAD(..., 5, '0'): Finally, it takes the output of the split — now universally a standard zip code without any extensions — and pads it with a leading zero if needed.

This single line of code effectively truncates ZIP+4 and fixes leading zero issues simultaneously. Just remember to assign the geographic role to your new field, and you'll be ready to build your map.

Final Thoughts

Dealing with zip codes in Tableau is a perfect example of why data formatting matters. By ensuring your zip codes are treated as text (strings) and using tools like LPAD, LEFT, and SPLIT, you can clean your data for flawless geographical analysis and visualization. These calculated fields give you the control needed to turn messy location data into crisp, actionable maps.

We know that managing these small data-cleaning hoops across different platforms can quickly add up to hours of tedious work. This frustration is why we built Graphed. It’s designed to handle these transformations for you automatically. Instead of writing formulas to truncate a zip code, you can use plain English to ask, "Show me sales by zip code on a map versus last quarter." Graphed instantly connects to your data, cleans it up, and builds the visual you need, letting you focus on insights, not manual data prep.

Related Articles