Tuesday, September 4, 2018

Use multiple tables to create a PivotTable

Use multiple tables to create a PivotTable

PivotTables are great for analyzing and reporting on your data. And when your data happens to be relational—meaning it's stored in separate tables you can bring together on common values—you can build a PivotTable like this in minutes:

PivotTable containing multiple tables

What's different about this PivotTable? Notice how the Field List on the right shows not just one but a collection of tables. Each of these tables contain fields you can combine in a single PivotTable to slice your data in multiple ways. No manual formatting or data preparation is necessary. You can immediately build a PivotTable based on related tables as soon as you import the data.

To get multiple tables into the PivotTable Field List:

Here's how you'd import multiple tables from a SQL Server database.

  1. Make sure you know the server name, database name, and which credentials to use when connecting to SQL Server. Your database administrator can provide the necessary information.

  2. Click Data > Get External Data > From Other Sources > From SQL Server.

  3. In the Server Name box, enter the network computer name of the computer that runs SQL Server.

  4. In the Log on credentials box, click Use Windows Authentication if you're connecting as yourself. Otherwise, enter the username and password provided by the database administrator.

  5. Press Enter and, in the Select Database and Table box, choose the database you want, then click Enable selection of multiple tables.

    Enable selection of multiple tables checkbox

  6. If you know exactly which tables you want to work with, manually choose them. Otherwise, pick one or two, then click Select Related Tables to auto-select tables that are related to those you selected.

  7. If the Import relationships between selected tables box is checked, keep it that way to allow Excel to recreate equivalent table relationships in the workbook.

  8. Click Finish.

  9. In the Import Data dialog box, choose PivotTable Report.

    Import options dialog box

  10. Click OK to start the import and populate the Field List.

Notice that the Field List contains multiple tables. These are all of the tables that you selected during import. You can expand and collapse each table to view its fields. As long as the tables are related, you can create your PivotTable by dragging fields from any table to the VALUES, ROWS, or COLUMNS areas.

PivotTable Fields list

  • Drag numeric fields to the VALUES area. For example, if you are using an Adventure Works sample database, you might drag SalesAmount from the FactInternetSales table.

  • Drag date or territory fields to the ROWS or COLUMNS area to analyze sales by date or territory.

  • Sometimes you need to create a relationship between two tables before you can use them in a PivotTable. If you get a message indicating a relationship is needed, click Create to get started.

    Create button appears when relationship is needed

    If you're working with other types of databases:

Import tables from other sources

In addition to SQL Server, you can import from a number of other relational databases:

Relational databases are not the only data source that lets you work with multiple tables in a PivotTable Field List. You can use tables in your workbook, or import data feeds that you then integrate with other tables of data in your workbook. To make all this unrelated data work together, you'll need to add each table to the Data Model, and then create relationships between the tables using matching field values.

Use the Data Model to create a new PivotTable

Perhaps you've created relationships between tables in the Data Model, and are now ready to use this data in your analysis. Here's how you build a new PivotTable or PivotChart using the Data Model in your workbook.

  1. Click any cell on the worksheet.

  2. Click Insert > PivotTable.

    PivotTable button on the Insert tab

  3. In the Create PivotTable dialog box, under Choose the data that you want to analyze, click Use an external data source.

    Create PivotTable dialog box with Use an external data source selected

  4. Click Choose Connection.

  5. On the Tables tab, in This Workbook Data Model, select Tables in Workbook Data Model.
    Tables in the Data Model

  6. Click Open, and then click OK to show a Field List containing all the tables in the Data Model.

More about PivotTables and the Data Model

No comments:

Post a Comment