如何在Tableau中将固定长度字符串拆分为多列?
Hey there! Since your string follows a strict fixed format (1 leading letter + 5 digits, e.g., A12345), splitting it into separate letter and number columns is totally straightforward using Tableau's built-in string functions. Here's a step-by-step breakdown:
Step 1: Create a Calculated Field for the Leading Letter
- In your Tableau workbook, right-click the original string field in the Data pane and select Create > Calculated Field.
- In the calculation editor, use the
LEFT()function to grab the first character:
ReplaceLEFT([Your Original Field Name], 1)[Your Original Field Name]with the actual name of your string column. This function pulls the leftmost 1 character, which is exactly your leading letter. - Name the field something like "Leading Letter" and click OK.
Step 2: Create a Calculated Field for the 5-Digit String
You have two equally reliable options here:
Option A: Use RIGHT() (Simpler for Fixed Length)
- Create another calculated field the same way as Step 1.
- Use the
RIGHT()function to pull the last 5 characters:
This works perfectly because your digits are always the final 5 characters in the string.RIGHT([Your Original Field Name], 5)
Option B: Use MID() (Target Specific Position)
If you prefer to specify the starting position instead of counting from the end, use MID():
MID([Your Original Field Name], 2, 5)
This starts at the 2nd character (skipping the leading letter) and pulls the next 5 characters.
Bonus: Convert Digits to Numeric Type (If Needed)
If you want the digit column to be a numeric type (instead of a string) for calculations, wrap the function in INT():
INT(RIGHT([Your Original Field Name], 5))
That's it! Both new fields will show up in your Data pane, and you can use them just like any other column in your visualizations.
内容的提问来源于stack exchange,提问作者Jeffrey Levy

