Union Your Data

You can union your data to combine two or more tables by appending values (rows) from one table to another. To union your data in Tableau data source, the tables must come from the same connection.

Supported connectors

If your data source supports union, the New Union option displays in the left pane of the data source page after you connect to your data. Supported connectors may vary between Tableau Desktop and Tableau Server and Tableau Cloud.

For best results, the tables that you combine using a union must have the same structure. That is, each table must have the same number of fields, and related fields must have matching field names and data types.

For example, suppose you have the following customer purchase information stored in three tables, separated by month. The table names are "May2016," "June2016," and "July2016."

May2016

Day Customer Purchases Type
4 Lane 5 Credit
10 Chris 6 Credit
28 Juan 1 Credit

June2016

Day Customer Purchases Type
1 Lisa 3 Credit
28 Isaac 4 Cash
28 Sam 2 Credit

July2016

Day Customer Purchases Type
2 Mario 2 Credit
15 Wei 1 Cash
21 Jim 7 Cash

A union of these tables creates the following single table that contains all rows from all tables.

Union

Day Customer Purchases Type
4 Lane 5 Credit
10 Chris 6 Credit
28 Juan 1 Credit
1 Lisa 3 Credit
28 Isaac 4 Cash
28 Sam 2 Credit
2 Mario 2 Credit
15 Wei 1 Cash
21 Jim 7 Cash

Union tables manually

Use this method to manually union distinct tables. This method allows you to drag individual tables from the left pane of the Data Source page and into the Union dialog box.

To union tables manually

  1. On the data source page, double-click New Union to set up the union.

  2. Drag a table from the left pane to the Union dialog box.

  3. Select another table from the left pane and drag it directly below the first table.

    Tip: To add multiple tables to a union at the same time, press Shift or Ctrl (Shift or Command on a Mac), select the tables you want to union in the left pane, and then drag them directly below the first table.

  4. Click Apply or OK to union.

Union tables using wildcard search (Tableau Desktop)

Use this method to set up search criteria to automatically include tables in your union. Use the wildcard character, which is an asterisk (*), to match a sequence or pattern of characters in the Excel workbook and worksheet names, Google Sheets workbook and worksheet names, text file names, JSON file names, .pdf file names, and database table names.

When working with Excel, text file data, JSON file, .pdf file data, you can also use this method to union files across folders, and worksheets across workbooks. Search is scoped to the selected connection. The connection and the tables available in a connection are shown on the left pane of the Data source page.

  1. On the data source page, double-click New Union to set up the union.

  2. Click Wildcard (automatic) in the Union dialog box.

  3. Enter the search criteria that you want Tableau to use to find tables to include in the union.

    For example, you can enter *2016 in the Include text box to union tables in Excel worksheets that end with "2016" in their names. Search criteria like this will result in the union of May2016, June2016, and July2016 tables (Excel worksheets), from the selected connection. In this case, the connection is called Sales, and the connection made to the Excel workbook containing the worksheets you wanted was in the quarter_3 folder in the sales directory (e.g., Z:\sales\quarter_3).

  4. Click Apply or OK to union.

Expand search to find more Excel, text, JSON, .pdf data

The tables initially available to union are scoped to the connection you've selected. If you want to union more tables that are located outside of the current folder (for Excel, text, JSON, .pdf files) or in a different workbook (for Excel worksheets), select one or both check boxes in the Union dialog box to expand your search.

For example, suppose you want to union all Excel worksheets that end with "2016" in its name outside of the current folder. The initial connection is made to an Excel workbook located in the same directory in the above example, Z:\sales\quarter_3.

  • Include: If you enter *2016 in the Include text box and leave the remaining search criteria of the dialog as is, Tableau looks for all Excel worksheets that end with "2016" in its name inside the current folder.

    In the diagram below, the yellow highlighted item represents the current location, that is, the Excel workbook that you created a connection to in the "quarter_3". The green box represents the tables belonging to workbooks and sheets that are unioned as result of this search criteria.

     

  • Include + Expand search to subfolders: If you enter *2016 in the Include text box and select the Expand search to subfolders check box, Tableau does the following:

    • Looks for all Excel worksheets that end with "2016" in their names inside the current folder.

    • Looks for additional Excel worksheets that end with "2016" in their names that are located in Excel workbooks in subfolders of the "quarter_3" folder.

    In the diagram below, the yellow highlighted item represents the current location, that is, the Excel workbook that you created a connection to in the "quarter_3" folder. The green box represents the tables belonging to workbooks and worksheets that are unioned as a result of this search criteria.

     

  • Include + Expand search to parent folder: If you enter *2016 in the Include text box and select the Expand search to parent folder check box, Tableau does the following:

    • Looks for all Excel worksheets that end with "2016" in their names inside the current folder, "quarter_3."

    • Looks for additional Excel worksheets that end with "2016" in their names that are located in parallel folders of the "quarter_3" folder. In this example, "quarter_4" is the parallel folder.

    In the diagram below, the yellow highlighted item represents current location, that is, the Excel workbook that you created a connection to in the "quarter_3" folder. The green boxes represent the tables belonging to the workbook and worksheets that are unioned as a result of this search criteria.

 

  • Include + Expand search to subfolders + Expand search to parent folder: If you enter *2016 in the Include text box and select both the Expand search to subfolders and Expand search to parent folder check boxes, Tableau does the following:
    • Looks for all Excel worksheets that end with "2016" in their namesinside the current folder, "quarter_3."

    • Looks for additional Excel workbooks that are located in the subfolders of the current folder, "quarter_3."

    • Looks for additional Excel workbooks that are located in parallel folders and subfolders of the "quarter_3" folder. In this example, "quarter_4" is the parallel folder.

    In the diagram below, the yellow highlighted item represents the current location, that is, the Excel workbook that you created a connection to. The green box represents the tables belonging to the workbook and worksheets that are unioned as a result of this search criteria.

Note: When working with Excel data, wildcard search includes named ranges but excludes tables found by Data Interpreter.

Rename, modify, or remove unions

Perform basic union tasks directly in the canvas of the Data Source page.

To rename a union
  1. Double-click the logical table that contains unioned physical tables.

  2. Double-click the union table on the physical layer canvas.

  3. Enter a new name for the union.

To add or remove tables in the union
  1. Double-click the logical table that contains unioned physical tables.

  2. Click the union drop-down arrow and then select Edit Union.

  3. You can drag additional tables that you want to union from the left pane, or hover over a table until the remove icon displays and then click the icon to remove the table.

  4. Click Apply or OK to complete the task.

To remove a union
  • Double-click the logical table that contains unioned physical tables, and then click the union drop-down arrow and select Remove.

Matching field names or field ordering

Tables in a union are combined by matching field names. When working with Excel, Google Sheets, text file, JSON file or .pdf file data, if there are no matching field names (or your tables do not contain column headers), you can tell Tableau to combine tables based on the order of the fields in the underlying data by creating the union and then selecting Generate field names automatically option from the union drop-down menu.

Metadata about unions

After you create a union, additional fields about the union are generated and added to the grid. The new fields provide information about where the original values in the union come from, including the sheet and table names. These fields are useful when unique information that is critical to your analysis is embedded in the sheet or table name.

For example, the tables used in the example above have unique month and year information stored in the table name instead of in the data itself. In this case, you can use the Table Name field that is generated by the union to access this information and use it in your analysis.

If a named range is used in a union, null values display under the Sheet field.

Note: You can use the fields generated by a union, such as Sheetor Table Name, as join keys. You can use a unioned table in a join with another table or unioned table.

Merge mismatched fields in the union

When field names in the union do not match, fields in the union contain null values. You can merge the non-matching fields into a single field using the merge option to remove the null values. When you use the merge option, the original fields are replaced by a new field that displays the first non-null value for each row in the non-matching fields.

You can also create your own calculation or, if possible, modify the underlying data to combine the non-matching fields.

For example, suppose a fourth table, "August2016", is added to the underlying data. Instead of the standard "Customer" field name, it contains an abbreviated version called "Cust."

August2016
Day Cust. Purchases Type
7 Maria 2 Credit
9 Kathy 1 Credit
18 Vijay 7 Cash

A union of these tables creates a single table that contains all rows from tables, with several null values. You can use the merge option to combine the related customer fields into a single field.

Union (with null values)

Day Customer Purchases Type Cust.
4 Lane 5 Credit null
10 Chris 6 Credit null
28 Juan 1 Credit null
1 Lisa 3 Credit null
28 Isaac 4 Cash null
28 Sam 2 Credit null
2 Mario 2 Credit null
15 Wei 1 Cash null
21 Jim 7 Cash null
7 null 2 Credit Maria
9 null 1 Credit Kathy
18 null 7 Cash Vijay

 

 

 

 

 

 

 

Union (with columns that have been merged)

Day Purchases Type Customer, Cust.
4 5 Credit Lane
10 6 Credit Chris
28 1 Credit Juan
1 3 Credit Lisa
28 4 Cash Isaac
28 2 Credit Sam
2 2 Credit Mario
15 1 Cash Wei
21 7 Cash Jim
7 2 Credit Maria
9 1 Credit Kathy
18 7 Cash Vijay

After you merge fields, you can use the field generated from the merge in a pivot or split, or use the field as a join key. You can also change the data type of the field generated from a merge.

To merge mismatched fields

  1. Select two or more columns in the grid.

  2. Click the column drop-down arrow, and then select Merge mismatched fields.

To remove a merge
  • Click the column drop-down arrow of the merged field and select Remove merge.

At a glance: Working with unions

Tableau Desktop and web authoring (Tableau Cloud and Tableau Server)

  • A unioned table can be used in a join.

  • A unioned table can be used in a join with another unioned table.

  • The fields generated by a union, Sheet and Table name, can be used as the join key.

  • If a named range is used in union, null values display under the Sheet field.

  • The field generated from a merge can be used in a pivot.

  • The field generated from a merge can be used as a join key.

  • The data type of the field generated from a merge can be changed.

  • Union tables from within the same connection. That is, you cannot union tables from different databases.

Tableau Desktop only

  • When working with Excel data, wildcard search includes named ranges but excludes tables found by Data Interpreter.

  • The field generated from a merge can be used in a pivot or split.

  • To union a JSON file, it must have a .json, .txt, or .log extension. For more information about working with JSON data, see JSON File.

  • When using wildcard search to union tables in a .pdf file, the result of the union is scoped to the pages that were scanned in the initial .pdf file you connected to. For more information about working with data in .pdf files, see PDF File.

  • Stored procedures cannot be unioned.

  • When working with database data, you can convert your union into custom SQL.

Thanks for your feedback!Your feedback has been successfully submitted. Thank you!