72
PowerPivot in SQL Server 2008 R2

PowerPivotinSQLServer2008R2

Embed Size (px)

Citation preview

Page 1: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 2: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Table of Contents

PowerPivot in SQL Server 2008 R2 ......................................................................................... 1

Exercise 1 Loading Data using PowerPivot for Excel ..................................................................................................... 2

Exercise 2 Creating PowerPivot PivotTables ................................................................................................................ 29

Exercise 3 Using DAX with PowerPivot PivotTables .................................................................................................... 39

Exercise 4 Collaborating with PowerPivot for SharePoint ........................................................................................... 59

Page 3: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 1 of 70

PowerPivot in SQL Server 2008 R2

Objectives After completing this lab, you will be better able to:

Use various methods of loading data into PowerPivot for Excel and structuring it for analysis

Create and use PivotTables against PowerPivot data

Understand DAX concepts and several basic DAX functions

Publish PowerPivot workbooks to Excel Services and understand their server-based structure

Scenario

Microsoft SQL Server 2008 R2 delivers many powerful new features. PowerPivot is one of the most significant, arguably Microsoft’s most important Business Intelligence (BI) advance since the advent of Microsoft SQL Server Analysis Services 2005. It seriously delivers on the long-awaited promise of “self-service BI” and “BI for the masses”, by leveraging Microsoft Excel, the world’s most-used analysis platform, Microsoft SharePoint, the rapidly growing collaboration platform, and a new, in-memory version of Analysis Services. PowerPivot enables end users to build high performance analytic applications without having to wait for IT to build cubes and develop the extract transform and load (ETL) process to populate them, or to learn MDX to achieve multidimensional calculations. At the same time it allows users to collaborate by sharing a single copy of the application and allows IT to monitor such usage, ultimately taking over management and control of the application where that makes sense. This lab will expose you to the full range of new features in Microsoft Office 2010, Microsoft SharePoint 2010 and Microsoft SQL Server 2008 R2 which, working together, achieve the self-service BI vision.

Estimated Time to Complete This Lab

90 Minutes

Computers used in this Lab VEGAS

The password for the Administrator account on all computers in this lab is: Password;1

Page 4: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 2 of 70

Exercise 1 Loading Data using PowerPivot for Excel

Scenario In this exercise, you will become familiar with loading data into Microsoft SQL Server PowerPivot for Excel. As in any version of Excel, this is the first step in preparing data for analysis. The tasks in this exercise may seem tedious compared to the ones that follow but be assured that the time invested will pay dividends later.

Tasks Detailed Steps

Complete the following tasks on:

VEGAS

1. Importing Relational Data Into PowerPivot

a. All the requisites for this lab are verified using the Configuration Wizard. To make sure that everything is correctly configured, follow these steps.

Note: To perform the setup steps you need to run the scripts in a command window with administrator privileges.

b. Launch the Configuration Wizard for this Lab by running the Setup.cmd script located under the Setup folder in the C:\LabFiles\Labs\SQL10R2UPD00-HOL-01\Source folder of this lab. Install any pre-requisites that are missing (rescanning if necessary) and complete the wizard.

Note: The Configuration Wizard creates and populates a new table in AdventureWorksDW2008R2 database used in this lab and also creates the 'SQLServerTrainingKitAlias' sever alias used to establish a connection with the database server.

If your setup fails, please restart the VEGAS Virtual Machine by hitting Start | Restart, give it a few minutes to restart on the backend then connect to the lab again, then repeat the step above.

c. The setup operation for this lab can take a couple of minutes because the new table is being populated with over 1.3 million rows.

d. Start Microsoft Excel 2010 with PowerPivot for Excel installed from Start | All Programs | Microsoft Office | Microsoft Excel 2010. When Excel opens, notice the PowerPivot ribbon tab that appears to the right of the standard View ribbon tab. Click on it to expose the ribbon for PowerPivot for Excel; it should look like Error! Reference source not found.below.

Page 5: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 3 of 70

Tasks Detailed Steps

e. Inside PowerPivot ribbon, click PowerPivot Window to open PowerPivot for Excel. The Home tab expands to offer the different types of data sources for import as shown in Error! Reference source not found.below.

Page 6: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 4 of 70

Tasks Detailed Steps

f. In the Home tab, click From Database and then select From SQL Server from the context menu. This will start the Table Import Wizard.

Page 7: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 5 of 70

Tasks Detailed Steps

Note: The Table Import Wizard is very similar to “Choose Database” wizard of other Microsoft products.

g. In the Connect to a Microsoft SQL Server Database step, add the information required to connect to the SQL Server database. To do this, set the following values:

h. Type PowerPivot AW in Friendly connection name textbox.

i. Type SQLServerTrainingKitAlias (the server alias) in the Server name combo box.

j. Select AdventureWorksDW2008R2 in the Database name combo box.

k. Optionally click Test Connection and finally click Next.

Page 8: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 6 of 70

Tasks Detailed Steps

l. In the Choose How to Import the Data step, choose the Select from a list of tables and views to choose the data to import option, and click Next.

Note: It is also possible to create an arbitrary SQL statement at this stage to create the data to import. Unlike the first option, this can only create a single table, and therefore the automated related table discovery described later in this task cannot occur – you will need to create any relationships to other imported data manually.

Page 9: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 7 of 70

Tasks Detailed Steps

m. Explore the options available in the Select Tables and Views steps shown in Error! Reference source not found.below. This step diverges from standard “Choose Database” wizards, exposing features unique to PowerPivot for Excel. We can do the following:

n. Select which tables and views are imported

o. Select which columns of each selected table and view are imported

p. Select which rows of each selected table and view are imported based on column filters

q. Request that tables and views directly related to those already selected also be selected

Page 10: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 8 of 70

Tasks Detailed Steps

r. In the Select Tables and Views steps, scroll down and check the FactInternetSalesPowerPivot table. This table is created and populated during the lab setup process.

Page 11: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 9 of 70

Tasks Detailed Steps

Note: When a table is selected from the list, the Friendly Name column changes to the table name – it can then be changed as desired. This friendly name will become the name of the “worksheet” within PowerPivot for Excel representing the table once it is imported.

s. With FactInternetSalesPowerPivot table selected, click Preview & Filter. The ensuing dialogue shown in Error! Reference source not found.below is where the rows and columns of the selected table or view can be restricted.

Note: In this lab we will only slightly exercise the filtering feature, but normally it would be a best practice to filter rows and columns on all tables and views to the minimum required. This will maximize the amount of useful data that can be held in available memory, minimize the data import time, and maximize analytical performance later. Included columns that are not needed will decrease efficiency but can be hidden.

Page 12: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 10 of 70

Tasks Detailed Steps

t. Explore the options available in the Preview Selected Table pane shown below. To exclude a column from the import click on the toggle “check” icon ( )on the left of the column header so that it disappears. To filter rows based on values in a particular column, select the dropdown to the right of the column header( ), uncheck “(Select All)”, and then check the desired values.

Note: If the number of rows in the source table is large, the filtering from the column header could take some time, as all rows for that column must be read from the source. In such a case it might be more efficient to use the SQL query option mentioned in Step 5 to do the filtering, though relationships will then have to be added manually.

u. In the Preview Selected Table pane, remove some columns from the import. To do this, scroll to the right to uncheck the UnitPriceDiscountPct, DiscountAmount, CarrierTrackingNumber and CustomerPONumber columns. Then click OK.

Note: For purposes of this lab we will not impose any row filters.

v. This will return to the prior pane. Note that now the Filter Details column for the table shows an Applied filters link, which if clicked will display details about the column and row filters applied. Do not yet click Finish.

Page 13: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 11 of 70

Tasks Detailed Steps

w. Make sure the FactInternetSalesPowerPivot table is selected and click Select Related Tables. This will automatically select other tables which are directly related to FactInternetSalesPowerPivot via database PK/FK constraints, if this information is available from the selected provider – which is the case for many, including SQL Server and Access. Observe that six additional tables were selected.

Page 14: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 12 of 70

Tasks Detailed Steps

x. Compare the selected tables to the set of table relationships shown in Error!

Reference source not found., and observe that only those tables which are directly related are selected. The tables which are indirectly related, namely DimGeography, DimProductSubcategory and DimProductCategory were not automatically selected and must be selected manually since we need them for the desired analysis.

y. Still in the Select Tables and Views step, select the DimGeography table and click Finish.

Note: We could at this stage check all three missing tables and normally would. However, in order to later illustrate manual relationship creation and Copy/Paste import, we will only check the DimGeography table.

z. In the Importing step, wait until PowerPivot imported and prepared the requested data. When the process is completed the pane in Error! Reference

source not found.below results. Note that over 1.3 million rows were imported

Page 15: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 13 of 70

Tasks Detailed Steps

from the FactInternetSalesPowerPivot table.

aa. Click Details link to see a review of what was actually done. Look for the message “Only one direct or indirect relationship can exist between two tables” which will be observed in several places. Referring back to a previous Error! Reference

source not found. you will see that each such case occurs when the same table and column is involved in more than one relationship. There are three distinct relationships between the FactInternetSalesPowerPivot and DimDate tables (these would support “role-playing dimensions” in Analysis Services), and two different tables have a relationship to DimSalesTerritory. For purposes of the lab we can fix these issues but in real scenarios this limitation could be significant. Close the Details pane and then click Close in the Table Import Wizard.

Note: At this point there is no longer a Back button. Some changes can still be made later but others cannot without starting over (see Task 6 of this exercise).

Page 16: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 14 of 70

Tasks Detailed Steps

bb. Explore the UI of PowerPivot for Excel shown in Error! Reference source not

found.below. As previously mentioned, it looks much like Excel. Press ALT+TAB keyboard shortcut to observe that both Excel and PowerPivot for Excel are running as distinct applications. The easiest way to switch between them is via ALT+TAB, because this is symmetric in either direction. You can also switch from within each application, clicking the Switch to Workbook icon ( ) in PowerPivot for Excel title bar, and clicking PowerPivot Window inside PowerPivot ribbon tab in Excel.

Note: Each table that was imported appears as a “worksheet” named after the friendly name mentioned earlier and containing the data that was imported. If some of the worksheet tabs are not being showed, try clicking the down arrow icon ( ) to expose them.

Remember that in spite of appearances these are not “real” Excel worksheets – but they are part of the overall workbook (XLSX) when it is saved. They represent the actual loaded data.

Page 17: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 15 of 70

Tasks Detailed Steps

cc. Switch to the FactInternetSalesPowerPivot worksheet. Observe that its row count is the same seen during import (about 1.3 million). Check the Add Column column on each worksheet when scrolled to the right. These will be explained in Exercise 3 on the DAX expression language.

dd. Save your work. To do this, click Save in PowerPivot for Excel, select the Source folder of this lab and set the document’s name as PowerPivotLab.xlsx.

ee. (Optional) If SQL Server Management Studio is available you can compare the size of your saved workbook and that of the source data in SQL Server to get an idea of the data compression achieved by PowerPivot. The PowerPivotLab.xlsx file should be just around 18 MB, while the size of FactInternetSalesPowerPivot alone in SQL Server is approximately 328 MB. The PowerPivot size is approximately 5.5% of the original source size, a ratio that would be even lower if the other imported tables’ sizes on SQL Server were accounted for.

ff. We excluded four columns from the FactInternetSalesPowerPivot table import so

Page 18: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 16 of 70

Tasks Detailed Steps

the comparison is only approximate, but it shows the compression is better than an order of magnitude in the present case. Compression will be highest when there is high redundancy in most imported columns, as would be typical in the de-normalized business data which is PowerPivot’s “sweet spot” for data sources. Compression (and performance) will not be as high if you import a large number of decimal or float columns, especially if their values have high precision (many decimal places) – or if the data is highly normalized.

Note: The steps of this task could be repeated as desired to import additional tables and views. However, no automatic relationship discovery is supported between tables and views imported in distinct import operations. If tables are in different databases or servers this cannot be avoided, but as it is much easier to have PowerPivot discovering relationships for you (if constraints are available to support this) than to do it manually, use Select Related Tables on each relevant table, in a single import setup, whenever possible – perhaps even select all tables. If more tables are selected than are required simply deselect any unneeded ones afterwards.

2. Managing Relationships Manually

a. Open PowerPivotLab.xlsx file which saved in previous task, if it is not already open. Note that Excel file opens as if the workbook were empty. Even if a workbook contains PowerPivot data, this will not be apparent when it is opened unless and until a PowerPivot PivotTable has been added, as will be done in Exercise 2.

b. Start PowerPivot for Excel again to access the imported data. To do this, in PowerPivot ribbon tab click PowerPivot Window.

c. Repeat the initial steps of Task 1 to get external data into PowerPivot until you arrive again at the previous Error! Reference source not found.. On this step select the DimProductSubcategory table. Click Finish and Close as prompted. You will return to Error! Reference source not found.above with a new worksheet representing the DimProductSubcategory table.

d. In the Design ribbon tab, click Manage Relationships to open the Manage Relationships dialog. Review the relationships recognized so far by PowerPivot, reflecting those automatically recognized in Task 1. Note there is no relationship involving the DimProductSubcategory table.

Page 19: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 17 of 70

Tasks Detailed Steps

e. Create a new relationship between the DimProduct and DimProductSubcategory tables. To do this, in the Manage Relationships dialog click Create. In the ensuing Create Relationship dialog set the following values in the combo boxes:

f. Table to DimProduct

g. Column to ProductSubcategoryKey.

h. Related Lookup Table to DimProductSubcategory.

i. Related Lookup Column correctly defaults to ProductSubcategoryKey since table constraints indicate it is the primary key.

j. Finally click Create in the Create Relationship dialog and then click Close in the Manage Relationships dialog.

Page 20: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 18 of 70

Tasks Detailed Steps

Note: Implicit in the Create Relationship dialog as of this writing are two important caveats: neither parent/child nor multi-column relationships are supported.

k. Referring to Error! Reference source not found.above we observe that DimSalesTerritory table has a PK/FK relationship to both DimGeography and FactInternetSalesPowerPivot tables, and we know from Step 14 of Task 1 that both cannot be supported in PowerPivot. For purposes of the lab we wish to retain the relationship to DimGeography because this forms a convenient hierarchy for analysis. If you have followed the steps in the lab carefully you will find that this relationship has already been set up, and the one to the FactInternetSalesPowerPivot table got an error during import in Step 14 of Task 1 and so does not exist – so there is nothing we need to do. Click Close to exit Manage Relationships. Leave the window resembling Error! Reference source

not found.above open.

Note: If tables were imported in a different order, the result could be that the FactInternetSalesPowerPivot relationship prevailed and the one we want to DimGeography failed. PowerPivot will take the first relationship it finds. If this had occurred we could use the Manage Relationships pane to first delete the relationship between DimSalesTerritory and FactInternetSalesPowerPivot, and then to add the relationship between DimSalesTerritory and DimGeography.

3. Importing via Copy/Paste

Note: In this task, we will use Copy/Paste to import the data from DimProductCategory table, and will then manually relate it to DimProductSubcategory using the technique shown in Task 2.

a. In the Home ribbon tab of PowerPivot for Excel, observe that all the buttons on the Clipboard ribbon option are disabled. The Paste To New Table option will become enabled once some appropriate data is on the clipboard.

Page 21: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 19 of 70

Tasks Detailed Steps

b. Open in a new instance of Excel the AuxiliaryData.xlsx file located under the Assets folder in the Source folder of this lab.

c. Copy the content of the DimProductCategory worksheet to the clipboard. To do this, click the DimProductCategory worksheet, highlight from A1 to E5 and press CTRL+C.

d. Switch back to the PowerPivot for Excel and the data already loaded by pressing ALT+TAB (do not click PowerPivot Window because you are in a new instance of Excel that started when you opened the AuxiliaryData.xlsx file, so this would launch a new instance of PowerPivot for Excel).

e. Once back in PowerPivot for Excel corresponding to PowerPivotLab.xlsx file, click Paste To New Table inside the Home ribbon tab to open the Paste Preview dialog shown in Error! Reference source not found.below. Type DimProductCategory as the Table Name, leave Use first row as column headers checked and click OK. A new worksheet named DimProductCategory will appear containing the copied data.

Note: If the Paste To New Table button is not enable then repeat the procedure, if necessary closing Excel (with save) and reopening. One inadvertent click at the wrong time in this sequence will disrupt it.

Page 22: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 20 of 70

Tasks Detailed Steps

f. Using the technique shown in Task 2 of this exercise, create a relationship between DimProductCategory and DimProductSubcategory based on the ProductCategoryKey column as shown in Error! Reference source not

found.below.

Page 23: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 21 of 70

Tasks Detailed Steps

g. Save your work by clicking the Save button.

4. Creating a Linked Table

Note: In this task, you will create a simple linked table which will later be used in Exercise 2. Linked tables provide a way to get data into PowerPivot for Excel which can be refreshed on the fly. A linked table is an array of data on an Excel worksheet which has been styled as a table and then linked into PowerPivot, where it becomes a PowerPivot table or worksheet. The source linked table and PowerPivot data must reside in the same workbook.

a. Make sure to have the PowerPivotLab.xlsx file open and the PowerPivot window loaded by clicking PowerPivot Window.

Note: Linked tables can be a good way to “adjust” an analysis while developing it in PowerPivot for Excel, since it is a practical mechanism for changing data on the client. The ideal scenario is when the values will converge to a static set before the workbook is shared to Excel Services. This is because workbooks containing PowerPivot data are not editable in Excel Services, thus linked tables do not provide a secondary mechanism, distinct from refreshing data connections, for updating data on the server.

b. Open the AuxiliaryData.xlsx file located under the Assets folder in the Source folder of this lab in a new instance of Excel.

c. Copy the content in the LinkedTable worksheet to the clipboard. To do this, click the LinkedTable worksheet, highlight from A1 to B6 and press CTRL+C.

d. Switch back to the PowerPivotLab.xlsx instance of Excel (not PowerPivot for Excel) by using ALT+TAB, select the A1 cell in an empty worksheet, and press CTRL+V to paste the data copied from AuxilaryData.xlsx.

e. Close AuxilaryData.xlsx file to avoid confusions later.

f. Format the data just pasted as a table. To do this, select all the data and click Format as Table from the Styles ribbon option of the Home ribbon tab. Choose one of the displayed table styles and confirm that the header labels are included by selecting the My table has headers check box. Make sure that the table just created remains selected. The result should appear as in Error! Reference

Page 24: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 22 of 70

Tasks Detailed Steps

source not found.below:

g. In the PowerPivot ribbon tab, click Create Linked Table inside the Excel Data ribbon option. The data in the table created in Excel should appear in PowerPivot for Excel as a new worksheet named Table1. Notice that there is a link icon ( ) next to the worksheet name indicating it comes from a linked table. You can navigate back to the linked table using the Go To Excel Table dialogue in the Linked Table ribbon tab. The aforementioned details are shown in Error!

Reference source not found.below:

Page 25: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 23 of 70

Tasks Detailed Steps

h. Go back to the Excel table and change all the values of the Key column to a value greater than 10 and then press ALT+TAB to switch back to PowerPivot for Excel.

Note: For purposes of Exercise 2, all values of the Key column must be greater than 10 before.

i. Check that the changes you made are reflected in Table1. You can make arbitrary data changes to the linked table including editing cells, deleting and adding rows and columns.

Note: If the changes you made are not reflected in Table1, click Update All inside the Linked Table ribbon tab.

j. Create a lookup relationship between the Table1 table and the FactInternetSalesPowerPivot table. To do this, using the procedures you learned in Task 2 of this Exercise, create a relationship between the SalesTerritoryKey column of FactInternetSalesPowerPivot table and the Key column of Table1 table as shown in Error! Reference source not found.below. The values of SalesTerritoryKey range from 1 to 10, so Step 8 ensures that in

Page 26: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 24 of 70

Tasks Detailed Steps

FactInternetSalesPowerPivot no rows match between these tables.

Note: We are relating Table1 table to FactInternetSalesPowerPivot table also for purposes of Exercise 2. There is in fact no such relationship, but we created one for the following reasons: show that PowerPivot allows this, and to be able to show what happens in a PivotTable in such a case in Exercise 2. This is to simulate the case where your source data lacks referential integrity between two tables.

5. Using a Reporting Services Report as a Data Source (Optional)

a. This task is included in this exercise to emphasize the possibility of using a Reporting Services report as a data source for PowerPivot. However, actually doing so is better left to Exercise 4 which is devoted to PowerPivot for SharePoint, on which this depends. For now simply be aware that when using Microsoft SQL Server 2008 R2 Reporting Services in SharePoint-integrated mode with SharePoint 2010, a Reporting Services report which has been published to SharePoint can be exposed as an Atom 1.0 data feed, which can in turn be consumed as a data source by PowerPivot for Excel. Regular SharePoint lists can also be so exposed and consumed, though this will not be demonstrated in this lab.

b. The report we will demonstrate being imported is created in the Using the New Features of Reporting Services 2008 R2 hands-on lab, which should be completed first if you wish to explore this feature in Exercise 4.

6. Reviewing Options Available After Data Import

a. Make sure to have the PowerPivotLab.xlsx file open and the PowerPivot window loaded by clicking PowerPivot Window.

b. In PowerPivot for Excel, select the DimSalesTerritory worksheet and then click the Home ribbon tab. Click Refresh and review the options available as show in Error! Reference source not found.below.

Page 27: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 25 of 70

Tasks Detailed Steps

Note: The Refresh option fetches the latest data in the data source and replaces the data in the PowerPivot model for the currently select worksheet – in our case the DimSalesTerritory table. The Refresh All option does the same for all the worksheets.

c. Now select the FactInternetSalesPowerPivot worksheet and then click the Design ribbon tab as indicated in Error! Reference source not found.below.

d. The Design ribbon tab exposes several options for manipulating and formatting columns on the selected table. The Error! Reference source not found.below

Page 28: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 26 of 70

Tasks Detailed Steps

shows the effect of clicking Hide and Unhide, which works a bit differently than in Excel. Instead of selecting a column or column range to hide, you pick them from a list; you cannot hide rows; and you can separately hide columns in the PowerPivot model and in the PowerPivot Field List (introduced in Exercise 2).

e. Other option in the Design ribbon is Table Properties. The Error! Reference

source not found.below shows the Edit Table Properties pane after clicking Table Properties which allows you to modify the columns and rows filters that were selected for the table during the data import operations.

Page 29: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 27 of 70

Tasks Detailed Steps

f. In the Home ribbon tab there are some column-specific options that become enabled when you select a particular column. You can adjust column width and set basic data type and formatting options, as well as sort order. Try these but do not save unless you want the effects you set. Finally, the Freeze feature behaves differently than in Excel since the frozen column is moved to the left-hand edge leaving the previously frozen column permanently shifted to the left.

g. In the Error! Reference source not found.above notice that each column header in a table displays a dropdown arrow ( ). When clicked it displays a dialogue that looks much like an Excel filter, though with fewer options. You can sort the

Page 30: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 28 of 70

Tasks Detailed Steps

column and create custom filters.

h. Save your work by clicking the Save button.

Page 31: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 29 of 70

Exercise 2 Creating PowerPivot PivotTables

Scenario PowerPivot data exposed as two-dimensional tables in the PowerPivot for Excel is not very useful by itself. In Exercise 3 we will see additional functionality that can be created in tables, but PowerPivot data really comes to life via PowerPivot PivotTables and Charts. These are created in Excel in similar fashion to the Excel equivalents, but they have an implicit connection to the data in PowerPivot for Excel rather than to an Excel cell range or data source. This exercise will focus exclusively on PowerPivot PivotTables, since the relevant principles are easily extended to the various chart options that are also available.

While PowerPivot PivotTables look and in many ways act similarly to Excel PivotTables, they are not identical. The equivalent of the Excel PivotTable Field List is the PowerPivot Field List which exposes PowerPivot data. The PowerPivot Field List also has more options for controlling structure and filtering than Excel PivotTables do.

Most importantly, PowerPivot PivotTables provide the main environment or context for the DAX expression language which enables true multidimensional calculations in Excel, much as an Analysis Services cube provides the context for leveraging MDX. Because DAX and PowerPivot PivotTables are intimately related we will review in this exercise the basics of PowerPivot PivotTables without DAX first.

Tasks Detailed Steps

Complete the following tasks on:

VEGAS

1. Creating a PowerPivot PivotTable

a. Open PowerPivotLab.xlsx file located under the Source folder of this lab if it is not already open. This should be the file obtained after completing the previous exercise.

b. Create a new PowerPivot PivotTable. To do this, in the PowerPivot ribbon tab click PivotTable icon inside the Report ribbon option. In the Create PivotTable dialog (shown in Error! Reference source not found.below) select the New Worksheet option and click OK. The PowerPivot Field List appears in a new worksheet as shown in Error! Reference source not found.below. Note that this is a true Excel worksheet, and that without any explicit configuration of data source it displays all the PowerPivot tables and columns that were imported.

Page 32: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 30 of 70

Tasks Detailed Steps

Page 33: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 31 of 70

Tasks Detailed Steps

c. Examine the PowerPivot Field List shown in Error! Reference source not

found.below. Like an Excel Field List, it opens whenever you click a cell in the PivotTable and shows fields you can place in the pivot. The PowerPivot Field List shows all the columns of all the tables in your PowerPivot data (except for columns you hid from PivotTables as shown in a Error! Reference source not

found.above in Exercise 1). Also as in the Field List pane of regular PivotTables, there are the Row Labels, Column Labels, Report Filter and Values areas to which you can drag columns from the various tables or select them and they will automatically populate sensible areas which you can then adjust.

Page 34: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 32 of 70

Tasks Detailed Steps

d. Note that the new Slicers Vertical and Slicers Horizontal areas in the PowerPivot Field List. This affords a finer level of control over what data is used in the PowerPivot PivotTable than is possible in an Excel PivotTable. Note though that this is not merely a matter of cosmetics or minimizing screen real estate by filtering – it is a reflection of the truly multidimensional nature of a PowerPivot PivotTable, as will be demonstrated in Exercise 3.

e. Select SalesAmount from FactInternetSalesPowerPivot and EnglishProductCategoryName from DimProductCategory, which should land in the Values and Row Labels areas respectively. As in an Excel PivotTable, when you select a numeric field it automatically appears in the Values section of the PivotTable and by default aggregates by SUM (which can be changed to other simple aggregates).

f. Referring back to the relationships in Figure 1 of Exercise 1, the result also demonstrates PowerPivot is using the multiple indirect relationships linking DimProductCategory and FactInternetSalesPowerPivot tables. The Grand Total shown is the same as would be found running the following query against FactInternetSalesPowerPivot table in the AdventureWorksDW2008R2 database, demonstrating that all PK/FK relationships in the data are valid (no data falls out when PowerPivot follows the relationships).

T-SQL

SELECT sum(SalesAmount)

FROM FactInternetSalesPowerPivot

GO

Page 35: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 33 of 70

Tasks Detailed Steps

Note: Referencing the cells of a PowerPivot PivotTable from outside the PivotTable works the same as with Excel PivotTables. You can make static references by typing them in the formula bar (for example “=C7”) which is generally not very useful since such a reference will not track its target as the pivot changes what is displayed, or you can use the GETPIVOTDATA function, which will track its target (within its limits, which are significant).

g. Save your work by clicking the Save button.

2. Using PowerPivot PivotTables with Slicers

Note: Slicers are a new feature of Microsoft Excel 2010 which is leveraged by both Excel and PowerPivot PivotTables. They provide UI elements aligned with but separate from the PivotTable itself, making for a much improved user experience. In this task, we will use slicers on a PowerPivot PivotTable.

a. With the PivotTable and PowerPivot Field List displayed, drag the Gender field from the DimCustomer table to the Slicers Vertical area (if you select it instead it will go to the Row Labels area, and you can then drag it from there to the Slicers Vertical area). Notice how the position of the PivotTable shifts to the right to make room for a Slicer UI representing the Gender field. When used with a PowerPivot PivotTable, slicers are managed in this way by PowerPivot:

Page 36: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 34 of 70

Tasks Detailed Steps

b. Each value of Gender is represented as a button. You can select each separately to slice by that value. Try it. By default a dark button is both selected and has data in the pivot, and a light button is not selected and/or does not have data in the pivot.

Note: The GETPIVOTDATA function will continue to work as PowerPivot shifts the PivotTable around, as long as the cell it references is still visible.

c. Add the FirstName field from DimCustomer table to the Slicers Vertical area. Be sure to have both genders (F and M) selected and scroll through the FirstName slicer. Observe all names are dark, implying all first names for both genders are selected and have data.

d. Now select only F (female) for the Gender slicer and again scroll through the FirstName slicer. You should see that the only dark selections are female names. Furthermore, all the female names come first, followed by all the male names. The light-colored male names signal that they have no data, which makes sense based on the gender slicer setting.

Note: The source data contains some gender-specific names whose gender value is

Page 37: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 35 of 70

Tasks Detailed Steps

incorrect.

Note: Another feature of slicers is that in addition to affecting the PivotTable, they affect each other when there are multiple slicers on the worksheet. A selection made on one changes not only the pivot data but the data shown in other slicers. The appearance of all member values in all slicers will be based on whether data exists for the intersection of all selections in all slicers.

e. Right-click anywhere on the FirstName slicer and select Slicer Settings to open the Slicer Settings dialog.

f. Review the options available in the Slicer Settings dialog. Here are options for how the slicer buttons are laid out, sized and sorted. Of particular interest are the options involving Visually indicate items with no data. By default that checkbox and both under it are selected, and these selections exactly describe the behavior just observed in the slicer. This is shown in Error! Reference source not

found.below:

Page 38: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 36 of 70

Tasks Detailed Steps

Note: If you uncheck the Visually indicate items with no data checkbox, the dependencies on others slicers will be removed and all the first names will displayed. If you uncheck the Show items with no data last checkbox, all the first names will preserve the original order even if there are others slicers applied.

g. The Slicers Horizontal works very similar to Slicers Vertical. If you place a member in that area, the PivotTable moves down to make room for the Slicer UI, and you can perform the same actions like with a slicer vertical. Error!

Reference source not found.below shows the results of dragging the FirstName field from the Slicers Vertical area to the Slicers Horizontal area.

Page 39: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 37 of 70

Tasks Detailed Steps

h. Clear all the filters by clicking the Clear Filter button ( ).

3. Investigating Faulty Referential Integrity

Note: One final demonstration will show how data lacking referential integrity will act in a PowerPivot PivotTable. That is, what happens if a relationship is declared between two tables which the data in the related columns does not fully (or at all, in this case) support?

To illustrate this we will use Table1 which was set up in Task 4 of Exercise 1 for this purpose. As you recall it was related to a column of the FactInternetSalesPowerPivot table and then populated in such a way that there is no actual relationship – no value in the source column of the FactInternetSalesPowerPivot table appears in the target column of the Table1 table.

a. Return to the PivotTable created earlier in this exercise, and drag the Value field from Table1 to the Column Labels area in PowerPivot Field List. Observe that nothing changes except for the appearance of a Column Labels dropdown list on the PivotTable.

b. Open the Column Labels dropdown as shown in Error! Reference source not

found.below and observe that all values of Value are selected. Deselect the empty value at the button of the tree view list, and click OK. Notice that the PivotTable changes to show no data. This is consistent with the data not supporting the relationship between SalesAmount and Value that was set up. Task 1 also showed that if all values in such a situation are selected, PowerPivot treats the relationship as if it did not exist, i.e. the slicer has no effect. This is conceptually similar to what use of an “All” member of a dimension in a slicer

Page 40: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 38 of 70

Tasks Detailed Steps

would do in an Excel PivotTable connected to an Analysis Services cube.

c. If the data partially supports the relationship, then when existing values are selected, the associated data will appear. You can conveniently try this by modifying the Excel table linked to Table1 to change some values to be in the range 1 to 10, clicking Update All in PowerPivot for Excel, and then selecting these values in the slicer.

Note: This point about referential integrity is stressed because PowerPivot does not alert you if such issues exist in the imported data. If your data partially but not fully supports a declared relationship, you could derive misleading results from the PivotTable because it looks reasonable. Naturally this situation cannot arise if all of your relationships derive from a database and they are enforced by database constraints – but given PowerPivot’s intention to enable end-user data integration from a variety of sources, this could easily not be the case. Users should be sure to run some sanity checks on the data in a PowerPivot PivotTable unless they know referential integrity has been guaranteed by the data source(s). Even then, integrity could still be violated by manual relationship changes, so it is still worth checking.

d. Save your work by clicking the Save button.

Page 41: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 39 of 70

Exercise 3 Using DAX with PowerPivot PivotTables

Scenario In this exercise we will investigate the Data Analysis Expressions (DAX) language, which is another new feature of PowerPivot. DAX is a crucial component in achieving the self-service BI vision discussed in the Overview, because DAX is what allows the user, without learning MDX, to create complex multidimensional calculations which would be difficult or impossible in Excel otherwise. While DAX is in some ways a new language, it is designed to “feel like” an extension to familiar Excel formulae and their functions, modified where necessary to interact with PowerPivot data. We cannot do more than scratch the surface of this important subject here - the goal of the exercise is to give you the background to explore further and deeper yourself, both through experimentation and reading documentation. There are many topics such as DAX data types, data type conversion and so on which are important but beyond our scope.

Tasks Detailed Steps

Complete the following tasks on:

VEGAS

1. Creating a Simple DAX Expression as a Calculated Column

a. Open PowerPivotLab.xlsx file located under the Source folder of this lab if it is not already open. This should be the file obtained after completing the previous exercise.

b. Open PowerPivot for Excel and explore the formula bar. To do this, click PowerPivot Window in the PowerPivot ribbon tab to return to the table view. Select the DimCustomer table and click the label of the Add Column column to select it as shown in Error! Reference source not found.below. Note that the Excel-like formula bar is now enabled. The formula bar is available when the column is selected.

Page 42: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 40 of 70

Tasks Detailed Steps

c. Create a formula that concatenates FirstName, a space and LastName columns from the DimCustomer table as the value of a calculated column. To do this, click the Add Column column label of the DimCustomer table and type the following code in the formula bar.

DAX

= [FirstName] & " " & [LastName]

d. In this context the formula applies to the entire column and is evaluated for each row of the table, immediately upon entering the formula. This is a calculation that has no relevant context except the current row in DimCustomer table, so it is appropriate as a calculated column.

e. Take into account that a calculated column formula must return a scalar value since it will be assigned to a column cell.

Note: Notice as you type that IntelliSense is present. As soon as you type a left square bracket you are presented with a list of columns from DimCustomer table. Observe

Page 43: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 41 of 70

Tasks Detailed Steps

that as in Excel formulae, DAX uses ampersand (&) as the text concatenation operator and text literals use double quotes.

f. Click the check button ( ) in the formula bar to populate the new column with the result for each row.

g. Rename the previously created column to FullName. To do this, right-click the new column, choose Rename Column and type FullName. Notice a new Add Column column appears to the right.

Note: The table name was not used in the previous formula because the default is to assume the current table, so if this is correct then it need not be referenced. The formula above is equivalent to:

= DimCustomer[FirstName] & " " & DimCustomer[LastName] and to

= 'DimCustomer'[FirstName] & " " & 'DimCustomer'[LastName]

In the remainder of this exercise the table name will only be specified when required – though it is never incorrect to specify it.

2. Creating More Complex Calculated Columns

Note: In this task, we will explore calculated columns that make reference to other tables and perform aggregation.

a. Create a new calculated column in the DimCustomer table containing the country and region. To do this, click the Add Column column label of the DimCustomer table and type the following code in the formula bar.

DAX

=RELATED(DimSalesTerritory[SalesTerritoryCountry]) & "- " &

RELATED(DimSalesTerritory[SalesTerritoryRegion])

Note: The Figure 1 of Exercise 1 shows that DimCustomer table has an indirect many-to-one relationship to DimSalesTerritory table. To follow a relationship and, in effect, perform a lookup on a column of a related table, the RELATED function is used as shown. It takes a column reference to the related table and returns the value of that column for the row related to the current row (in DimCustomer table here).

b. Click the check button ( ) in the formula bar to populate the new column with the result for each row. Scroll down to where country is United States to see that region is not always the same as Country.

c. Rename the previously created column to SalesTerritory. To do this, right-click the new column, choose Rename Column and type SalesTerritory.

d. At this point the right-hand end of DimCustomer table appears as shown in Error! Reference source not found.below:

Page 44: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 42 of 70

Tasks Detailed Steps

e. Create a calculated column in DimProductCategory table containing the amount of products of each category. To do this, switch to the DimProductCategory worksheet, click the Add Column column label and type the following code in the formula bar.

DAX

=COUNTROWS(RELATEDTABLE(DimProduct))

Note: The Figure 1 of Exercise 1 shows that DimProductCategory table has an indirect, one-to-many relationship to DimProduct table. Whereas the RELATED function returns the value in a related column for the related row, RELATEDTABLE returns a reference to the entire related table named. The COUNTROWS function counts the rows in a table. So the effect of the formula is to count the number of products in the DimProduct table that exist for each value in the DimProductCategory table – i.e. how many products are in each category. This also illustrates the use of nested DAX functions. RELATEDTABLE is a DAX function that can

Page 45: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 43 of 70

Tasks Detailed Steps

only be used as an argument to another function, and in particular an argument that expects a table.

f. Click the check button ( ) in the formula bar to populate the new column with the result for each row.

g. Rename the previously created column to ProductCount. To do this, right-click the new column, choose Rename Column and type ProductCount. Error!

Reference source not found.below shows the results:

h. Finally, create a calculated column in the DimPromotion table containing a flag about the amount to discount. To do this, switch to the DimPromotion worksheet, click the Add Column column label and type the following code in the formula bar.

DAX

=IF(DimPromotion[DiscountPct] <

0.1,"Miserly",IF(DimPromotion[DiscountPct] >= 0.1 &&

DimPromotion[DiscountPct] < 0.3,"Reasonable","Excessive"))

Page 46: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 44 of 70

Tasks Detailed Steps

Note: The previous formula should be self-explanatory for Excel users. It uses the DAX version of the Excel IF function to perform conditional logic in a calculated column.

i. Click the check button ( ) in the formula bar to populate the new column with the result for each row.

j. Rename the previously created column to DiscountPctFlag. To do this, right-click the new column, choose Rename Column and type DiscountPctFlag. Error!

Reference source not found.below shows the results:

Note: During this task you have seen that a variety of functionality can be added directly to PowerPivot tables, some of which is similar to what could be done in Excel alone and some of which is considerably more powerful, especially when you consider what would happen when source data is refreshed into PowerPivot. For example, because formulae apply to entire columns of tables there would not need to be any tiresome adjustment of cell ranges when the number of rows in a table or a related table changes, as there would be in Excel. Furthermore, the values of calculated

Page 47: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 45 of 70

Tasks Detailed Steps

columns can be used in PowerPivot PivotTables in all the same ways imported columns can be.

Nevertheless, there are limits to the usefulness of calculated columns within PowerPivot for Excel given the limited UI provided and their static nature. As will be shown in the remainder of this exercise, PowerPivot PivotTables are where Calculated Columns and their brethren, Measures, really become useful and show the power of DAX.

3. Creating a Placeholder Table for Measures

Note: As you have seen, DAX expressions in calculated columns are by definition associated with particular PowerPivot tables. It turns out that this how it appears in the PowerPivot Field List for measures as well. If you have used Excel PivotTables against Analysis Services cubes this will feel strange, since in Analysis Services measures are treated as distinct entities (Measure Groups), and are presented as such by client tools such as Excel PivotTables.

An important difference between Calculated Columns and Measures is that, as we have seen, the values of a Calculated Column are immediately calculated for all rows of a table, while a measure is not calculated unless and until it is placed on a PowerPivot PivotTable.

The PowerPivot Field List displays all (non-hidden) columns in all tables, including Calculated Columns. Since Measures also (at least appear to) belong to a particular table, they are not collected in a separate UI grouping as they are in an Excel PivotTable. In a sense it does not matter from a functional perspective in which table a measure appears, since any table or column can be addressed from any other table. But having measures scattered across various tables could make it difficult for uses of your worksheet to find them once it is shared to PowerPivot for SharePoint. For this reason we will start with a little trick to gather measures together in the PowerPivot Field List.

a. Create an empty table to act as a placeholder for measures created in the PivotTable. To do this, switch to Excel by clicking Switch to Workbook ( ), select an empty cell and press CTRL+C to copy it to the clipboard. Then press ALT+TAB to switch back to the PowerPivot for Excel tool and click Paste To New Table from the Home ribbon tab. Uncheck the Use first row as column headers option in the Paste Preview dialog, type CalculatedMeasures as the Table Name and click OK.

Page 48: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 46 of 70

Tasks Detailed Steps

b. This will create a new table called CalculatedMeasures with an empty column named Column1.

c. Rename Column1 to EmptyColumn. To do this, right-click Column1 column, choose Rename Column and type EmptyColumn. We now have an empty table in which to create measures in the following tasks.

Page 49: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 47 of 70

Tasks Detailed Steps

Note: Tables that do not have any columns are not included in the PowerPivot Field List. That is why we are leaving the EmptyColumn column in the CalculatedMeasures table. However, this column could be removed after adding the first Measure in the following Task.

4. Creating Measures Part 1

a. Switch back to Excel and select the worksheet with the PowerPivot PivotTable created in Exercise 2.

b. If necessary, click Refresh in the PowerPivot Field List to update the list.

c. Remove Value from the Column Labels area of the PowerPivot Field List so that it no longer restricts the data in the pivot. To do this, right-click over the Value field in the Column Labels area of the PowerPivot Field List and click Remove Field. Also collapse the row labels if they are not already collapsed.

Page 50: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 48 of 70

Tasks Detailed Steps

Note: If the PowerPivot Field List is not showing and the PivotTable is not showing any data, then to do these steps you must first open the Column Labels filter on the

PivotTable by clicking the down-arrow to its right ( ), selecting “(Select All)” so that the pivot shows data, and finally clicking on a PivotTable cell to open the PowerPivot Field List.

d. Scroll the PowerPivot Field List and observe the calculated columns created in Tasks 1 and 2 in the relevant tables. Right-click on one and observe there is no option to see the DAX expression of the column (you must switch back to PowerPivot for Excel to see that). Then scroll to the end and observe the CalculatedMeasures table at the end of the list.

Note: It is not possible to reorder the tables in the PowerPivot Field List – they appear in the order they were imported or created. If you want the CalculatedMeasures table to appear at the start of the list, as would clearly be more useful, be sure to create it first before importing any other data.

e. Drag the DiscountPctFlag calculated column created in DimPromotion table (Task 2) to the Column Labels area on the PowerPivot Field List and examine the

Page 51: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 49 of 70

Tasks Detailed Steps

PivotTable. This shows how useful even relatively simple Calculated Columns can be. Notice there is no data for “Excessive”. You can confirm by querying the PromotionKey column of FactInternetSalesPowerPivot in SQL Server Management Studio that no values corresponding to “Excessive” are used.

f. Choose an arbitrary table in the PowerPivot Field List and right-click to expose the Add New Measure option. This shows how Measures can be created in any table, and indeed must be created in a table. Once created, measures can also be edited, renamed and deleted by right-clicking on them. Escape or cancel this option.

Note: In the remainder of this exercise we will only create measures in the CalculatedMeasures table we created for the purpose.

g. Scroll down the PowerPivot Field List, right-click the CalculatedMeasures table and click Add New Measure. The Measure Settings dialog appears allowing you to name the measure and type the DAX formula for it in a formula bar, as shown in Error! Reference source not found.below. IntelliSense is available in this dialogue and should be used as much as possible. Leave this open for the next step.

Page 52: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 50 of 70

Tasks Detailed Steps

h. Suppose we wish to construct a measure which gives SalesAmount as a fraction of the total sales in FactInternetSalesPowerPivot table. Exploring how to construct such a measure will illustrate many features of how to think in DAX terms. The measure should have the grand total value shown in the PivotTable in all contexts.

i. In the Measure Settings dialog from the prior step, type TotalSales in both Measure Name and Custom Name textboxes and enter the following formula in Formula textbox. Finally click OK.

DAX

= SUM(FactInternetSalesPowerPivot[SalesAmount])

j. This seems straightforward – we want the sum of the SalesAmount column for the whole table, and the help for the SUM function, which states that it sums all the values in a column, implies that is what we will get.

Note: As you may notice, you can choose two different names for the measure: the Measure Name that will used in all PivotTables available in the document, and the Custom Name that will be available only in the context of the current PivotTable.

k. Observe that when you create a measure it automatically appears in the PivotTable. Error! Reference source not found.below shows the result, and we see that our TotalSales measure is not giving what we want – instead of invariantly returning the grand total it is returning the same value on each row as the existing PivotTable aggregation of SalesAmount, regardless of slicer settings and expansion of row labels. In point of fact our Measure is equivalent to the Sum of SalesAmount measure that the PivotTable automatically provides when SalesAmount is placed on the pivot.

Page 53: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 51 of 70

Tasks Detailed Steps

Note: If a new measure does not appear in the PivotTable upon creation, or if you are unable to select or drag an existing measure to the PivotTable, it means there is something wrong with it. If the error is syntactical it is usually reported in the formula editor, and having such an error does not prevent saving it. If the error is semantic and/or contextual, then PowerPivot will simply refuse to place it on the PivotTable.

l. Save the changes in the worksheet before continuing by clicking the Save button.

m. Switch to PowerPivot for Excel by clicking PowerPivot Window in the PowerPivot ribbon tab. Examine the CalculatedMeasures table. You will not see the TotalSales measure we just created there. Creation of a measure in the PowerPivot Field List “feels like” you are creating another Calculated Column, but this shows that that is not the case.

Note: The DAX expression behind a Measure can only be seen in the PowerPivot Field List, and the DAX expression behind a Calculated Column can only be seen in PowerPivot for Excel.

n. Modify the formula of the TotalSales measure to return the value we want. To do this, right-click over the TotalSales measure in the PowerPivot Field List and click Edit Formula. Replace the formula for the following one and click OK.

DAX

=SUMX(ALL(FactInternetSalesPowerPivot),[SalesAmount])

Note: Measures in PivotTables are dynamic by default – that is, are evaluated in the context of both the PivotTable structure (row and column labels) and Slicer settings. When you need a calculation which is not affected by these contexts such as the grand total we are striving for you must use additional DAX functions which are provided for the purpose.

o. In Error! Reference source not found.below we see that the measure now gives the same grand total amount that we wish on every row. Try changing Slicer settings to see they have no effect on the measure value.

p. We have changed SUM to SUMX, a function which takes a table as its first argument and an expression as its second argument. The expression is evaluated for each row of the supplied table and the values summed. Here is how this formula works:

q. The ALL function which we have nested can take a table argument and return another table. When given a table it returns all rows from it regardless of any slicer settings or other context (row and column labels) that may exist

r. The [SalesAmount] expression is evaluated on each row of the returned table (and it need not be qualified by table name because that is inferred from the

Page 54: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 52 of 70

Tasks Detailed Steps

table argument).

s. The SUMX function effectively sums all the values of SalesAmount and thereby provides the grand total we want.

t. Once you understand this concept you will be a long way to understanding how to construct DAX formulae to get the effect you need.

Note: DAX function documentation uses the term “filter” in multiple ways, which can make it difficult to understand how to use some functions or get the effect you need. In some cases “filter” is an all-encompassing term referencing the full context of any given cell in a PivotTable. By default each cell’s context is defined by its row and column labels plus any slicers that are active – just as in a regular Excel PivotTable. But some calculations need only a subset of this context, for example row and column labels should be respected but slicers should be ignored or an entirely different context than provided by row and column labels plus slicers is required. The TotalSales formula is an example of the latter. The term “filter” is also used to refer to these alternate contexts. When creating DAX expressions one must take into account the structure of the PivotTable, the slicers that may exist, and the required result of the expression.

u. Create another measure in the CalculatedMeasures table which calculates the sales ratio. To do this, right-click over the CalculatedMeasures table in PowerPivot Field List and click Add New Measure. In the Measure Settings dialog type ShareOfTotal in both Measure Name and Custom Name textboxes and enter the following formula. Finally click OK.

DAX

=(SUM(FactInternetSalesPowerPivot[SalesAmount])/Calculated

Measures[TotalSales])*100

v. Here the very dynamism of SUM(FactInternetSalesPowerPivot[SalesAmount]) noted earlier is exactly what we do want – we want that value calculated in the context of the row and column labels and slicer settings.

w. Remove TotalSales from the PivotTable. To do this, right-click over the TotalSales field in the Sum of Values area of the PowerPivot Field List and click Remove Field. The final result looks like Error! Reference source not found.below. Drill into the PivotTable and make selections on the slicers to verify that the new measure behaves as it should. Notice that the Grand Total of the Total ShareOfTotal column becomes less than 100% once slicer selections are made, since TotalSales is insensitive to all contexts.

Page 55: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 53 of 70

Tasks Detailed Steps

5. Creating Measures Part 2

Note: In this task, we will create a measure to calculate the share-of-product-category value for each product subcategory. To build this ratio we need the total sales filtered by product category in the denominator. This can be achieved by using the ALL function again but this time with a column instead of a table argument (this function is polymorphic). When ALL is provided with a column it effectively removes any filter on that column only – where “filter” here means the context that would otherwise apply from row and column labels and slicers. We need totals which are invariant by product category. Given the structure of our PivotTable, which has product category and subcategory on the rows, this is the same as saying “ignore the product subcategory filter”. In other words, even when the rows are showing product subcategories because we drilled down on a category, the category total should remain the same.

The FILTER function, which will be used in this task, is very important in many calculations. It takes a table and a filter condition and returns a new table consisting of only the rows meeting the filter condition. In this case “filter” has yet another meaning – a boolean condition. This function allows you to create subsets of tables over which to aggregate or perform other functions, for example, all the rows of the FactInternetSalesPowerPivot table where the product category is “Accessories”.

a. To make the results easier to see, remove the DiscountPctFlag field from Column Labels and the ShareOfTotal field from the Values area.

b. Drag the TotalSales measure from the CalculatedMeasure table to the Values area.

c. Drag the EnglishProductSubcategoryName field from the DimProductSubcategory table to the Row Labels area. The resulting PowerPivot Field List and PowerPivot PivotTable should look like in Error! Reference source

not found.below.

Page 56: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 54 of 70

Tasks Detailed Steps

d. Create a new measure in the CalculatedMeasures table which calculates the total by product category. To do this, right-click over the CalculatedMeasures table in PowerPivot Field List and click Add New Measure. In the Measure Settings dialog type TotalSalesProdFiltered in both Measure Name and Custom Name textboxes and enter the following formula. Finally click OK.

DAX

=CALCULATE(SUM(FactInternetSalesPowerPivot[SalesAmount]),ALL(

DimProductSubcategory[EnglishProductSubcategoryName]))

e. The result is shown in Error! Reference source not found.below. You can see that the total by product category remains invariant even when sliced by product subcategory, which is exactly what we need for our denominator. Also notice that because we removed any filtering on product subcategory, all values of product subcategory appear under each product category, with only those belonging to the category showing data for Sum of Sales Amount. This is not very useful but of course our TotalSalesProdFiltered measure is designed to be part of another measure (to be created in the next step), not be on the PivotTable. If this measure were not on the PivotTable then the usual PivotTable behavior of not showing rows with no values would prevail again.

Page 57: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 55 of 70

Tasks Detailed Steps

f. Unfortunately the overall calculation we are aiming for, which intuitively one might expect to require only a minor change to the formula for TotalSales, looks quite different. We must use the CALCULATE function to force a recalculation of the SUM of SalesAmount in the context of ignoring the filter on product subcategory that exists when you drill down on product category in the pivot table. This would seem to make sense, except then one might ask why CALCULATE is not necessary in the TotalSales formula. Presumably the reason has to do with the fact that all filters instead of only some are being removed in the latter case, but it is not clear.

g. Remove the TotalSales and TotalSalesProdFilter fields from the PivotTable.

h. Create a new measure in the CalculatedMeasures table which calculates the share of the products sales by each product subcategory. To do this, right-click over the CalculatedMeasures table in PowerPivot Field List and click Add New Measure. In the Measure Settings dialog type ShareOfProdTotal in both Measure Name and Custom Name textboxes and enter the following formula. Finally click OK.

DAX

=(SUM(FactInternetSalesPowerPivot[SalesAmount])/CalculatedMea

sures[TotalSalesProdFiltered])*100

i. The (correct) result is shown in Error! Reference source not found.below:

Page 58: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 56 of 70

Tasks Detailed Steps

j. Add the ProductCount Calculated Column from DimProductCategory to the PivotTable. Recall this was a count of how many products existed under each product category, found by following a relationship and counting rows. As with the TotalSalesProdFiltered measure, this calculation is not that useful directly on the pivot – notice how it too causes every value of product subcategory to appear under every value of product category. And for the same reason: it does indeed have a value in every case! Collapse all the product category values and notice a fourth category has appeared: Components. Evidently this category has products but no sales.

k. A more useful measure might be what percent of the total number of products exist under each product category and each product subcategory. You now know enough to create such a measure and doing so is left as an optional exercise.

l. Remove ProductCount and ShareOfProdTotal fields from the PivotTable.

Note: At the start of this Exercise we mentioned that it is unfortunate that DAX functions which return tables can only be used as arguments to other DAX functions and we are now in a position to explain why. If you are familiar with Analysis Service’s MDX language you may have already thought that such derived tables seemed like the DAX equivalent of MDX sets. At the risk of oversimplifying, they are. The limitation is that in MDX, sets can be used, not only as inputs to calculations, but also as the members on axes (rows and columns) and as slicers (in the WHERE clause), while in DAX they cannot, since functions returning tables can only figure as inputs to other functions. To help visualize this, imagine if new tables could be created in the

Page 59: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 57 of 70

Tasks Detailed Steps

PowerPivot Field List, defined by a table-valued DAX expression, and then used like any other table. That would be a DAX equivalent of an MDX named set and would be very useful.

m. Create a new measure to see how sales of product categories other than “Accessories” compare to those of Accessories. This requires the total sales for the Accessories category in the denominator, and invariant across other category values. The FILTER function can be used to derive this, and the ALL function once again used to ensure the value is invariant across all product categories.

n. To do this, right-click over the CalculatedMeasures table in PowerPivot Field List and click Add New Measure. In the Measure Setting dialog type RatioToAccessories in both Measure Name and Custom Name textboxes and enter the following formula. Finally click OK.

DAX

=SUM(FactInternetSalesPowerPivot[SalesAmount])/CALCULATE(SUMX

(FILTER(FactInternetSalesPowerPivot,RELATED(DimProductCategor

y[EnglishProductCategoryName])="Accessories"),FactInternetSal

esPowerPivot[SalesAmount]),ALL(DimProductSubcategory[EnglishP

roductSubcategoryName]),ALL(DimProductCategory[EnglishProduct

CategoryName]))

Note: The FILTER function is used to create a version of the FactInternetSalesPowerPivot table containing only rows associated with the product category “Accessories”. It is again necessary to use the RELATED function to navigate this relationship. Then, as with the TotalSalesByCategory measure, we need to wrap the calculation in the CALCULATE function to override the calculation context with the one we want. The SUMX function will sum the SalesAmount column for our filtered table, getting the sum for “Accessories”, but then, to ensure the value exists in all contexts on the PivotTable (i.e. for all subcategories and categories which are not “Accessories”) we must remove those filters using ALL in its column form. Notice that CALCULATE takes a variable number of filter references – in this case, we use ALL twice to remove filtering on product category and product subcategory. Finally, this value is our denominator and the numerator is the same as for the ShareOfProdTotal measure.

o. Format the values of the RatioToAccessories column of the PivotTable as Number. To do this, select the entire RatioToAccessories column, point to the Home ribbon tab and pick the Number option in the combo box located in the Number ribbon option.

Page 60: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 58 of 70

Tasks Detailed Steps

p. Review the outcome of the RatioToAccessories measure. This measure shows the ratio of the sales of various product categories and subcategories to the total sales for Accessories. For example, Bikes sales are over 40 times those of Accessories. The sum of these ratios is of course not meaningful. The result is shown in Error! Reference source not found.below.

q. Save your work by clicking the Save button.

Page 61: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 59 of 70

Exercise 4 Collaborating with PowerPivot for SharePoint

Scenario Once in-memory cubes are created, other client tools such as SQL Server Management Studio, Business Intelligence Development Studio and Report Builder can connect to them, query them using MDX, and in general interact with them just like “regular” cubes. The only difference is that the connection string is a URL to the Excel document on SharePoint rather than to the Analysis Services provider. All of this means that IT can see the details of what the user has built and can re-architect it if appropriate, and that PowerPivot models can become data sources themselves.

Tasks Detailed Steps

Complete the following tasks on:

VEGAS

1. Changing the Excel Services Authentication Technique (Optional)

Note: In this task, you will see how to change the Excel Services authentication settings to None. This will allow us to use the stored credentials in the SharePoint's Secure Store Service to perform data refresh jobs once the Excel document is published to Excel Services.

You will need an unattended service account to perform a data refresh after setting the authentication technique to None. The Unattended Service Account is a single account that all documents can use to refresh data. It is required when connecting to data sources external to SharePoint, such as SQL. Without a valid Unattended Service Account Application ID, services such as Visio Graphics and Excel Calculation services will not be able to refresh their data that are connected to external data sources.

You can create a new Unattended Service Account Application ID from SharePoint Central Administration -> Manage service applications -> Secure Store Service.

For more detailed information, see http://msdn.microsoft.com/es-ar/library/ee210671(SQL.105).aspx.

a. Open PowerPivotLab.xlsx file located under the Source folder of this lab if it is not already open. This should be the file obtained after completing the previous exercise

b. Select the Data ribbon tab and click Connections ( ) to open the Workbook Connections dialog shown in Error! Reference source not found.below.

Page 62: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 60 of 70

Tasks Detailed Steps

c. In the Workbook Connections dialog, select the PowerPivot Data connection and then click Properties to open the Connection Properties dialog as shown in Error! Reference source not found.below.

Page 63: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 61 of 70

Tasks Detailed Steps

d. In the Connections Properties dialog, select the Definition tab and click Authentication Settings at the bottom to open the Excel Service Authentication Settings dialog.

e. In the Excel Service Authentication Settings dialog, select the None and then OK. Click OK again in the Connections Properties dialog and finally click Close in the Workbook Connections dialog.

Page 64: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 62 of 70

Tasks Detailed Steps

Note: If you prefer, you can leave Windows Authentication as the authentication method. In this way you will not need to create and configure an Unattended Service Account.

2. Sharing a PowerPivot Workbook to SharePoint

a. Click the File menu and select the Save & Send tab.

b. Click the Save to SharePoint option under the Save & Send section and then click the Save As button. This will open the Save As dialog shown in Error! Reference

source not found.below.

Page 65: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 63 of 70

Tasks Detailed Steps

Page 66: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 64 of 70

Tasks Detailed Steps

c. Save the excel document in the PowerPivot Gallery of a SharePoint site created with the PowerPivot Site template. To do this, in the Save As dialog type a full or partial URL to PowerPivot Gallery and click Save. Because the Analysis Services cube must be created, this operation may take up to several minutes to complete.

Note: To get the URL of your PowerPivot Gallery, enter your server name (http://localhost/) in the Address Bar of the Save As dialog and press ENTER. This will open a connection to the server and you will be able to browse to your PowerPivot site. Finally, double-click PowerPivot Gallery on the Document Libraries list to select this location as shown in Error! Reference source not found. below.

Page 67: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 65 of 70

Tasks Detailed Steps

Note: The PowerPivot Gallery is a special-purpose SharePoint document library that provides rich preview and document management for published Excel workbooks that contain PowerPivot data as well as other document types. You can use PowerPivot Gallery to share and access workbooks or Reporting Services reports, preview a large file without having to open it first, schedule data refresh, or create an entirely new workbook or report based on an item you already published.

For more information, see http://msdn.microsoft.com/library/ee210607(SQL.105).aspx.

d. When the upload completes, navigate to the PowerPivotLab workbook in the PowerPivot Gallery. To do this, open Internet Explorer and navigate to the PowerPivot Gallery of your SharePoint site (e.g. http://localhost/PowerPivot Gallery/) and open the PowerPivotLab workbook as shown in Error! Reference

source not found.below.

Page 68: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 66 of 70

Tasks Detailed Steps

Note: You must have Contribute or higher entitlement on PowerPivot Gallery to be able to upload. In addition, Excel Services enforces a configurable maximum size for a saved workbook. PowerPivotLab.xlsx is approximately 19 MB, which may exceed the default limit in your SharePoint installation. If you get an error to this effect when saving, contact your SharePoint administrator to get the limit raised to at least 20 MB. Or, if you have administrator permissions you can navigate to settings page at SharePoint Central Administration -> Application Management -> Manage Service Applications and double-click Excel Services from the list. In Manage Excel Service page click Trusted File Locations, click the linked name of the location (http://localhost/) and select Edit to open the Edit Trusted File Location page. Finally set the Maximum Workbook Size property to 20 MB and then click OK.

Page 69: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 67 of 70

Tasks Detailed Steps

e. Navigate back to the PowerPivot Gallery document library on your SharePoint 2010 instance as shown in Error! Reference source not found.below.

f. Notice how “PowerPivotLab” is shown with previews of each worksheet. Now that the workbook is in Excel Services, any other entitled users can access it and interact with any PivotTables and charts it may contain. Also notice the “Top Sales Orders by Item Count” document. This is a Reporting Services report which will be present if you have carried out the Using the New Features of Reporting Services 2008 R2 hands-on lab, and will be used in the next task.

Note: PowerPivot Gallery provides three Silverlight-based skins named Gallery, Theater and Carousel, each of which provides a more visual and interactive preview of saved documents than a standard SharePoint list. Gallery is the default view shown in Error! Reference source not found.below. The Theater and Carousel views can be selected using the Current View dropdown list available in the Library sub-tab of the Library Tools ribbon tab.

Note: You can upload PowerPivot workbooks to pre-SharePoint 2010 libraries though obviously the new R2 and SharePoint 2010 features will not work. You can then download such files and open them with any version of Excel, getting a browse-only experience if PowerPivot for Excel is not installed and a full experience if it is.

g. Save your work by clicking the Save button and close both Excel and PowerPivot

Page 70: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 68 of 70

Tasks Detailed Steps

for Excel.

3. Managing a Shared Workbook’s Data Refresh

Note: In this task, we will refresh the data in worksheet on PowerPivot for SharePoint. As noted in Exercise 1, the data in a PowerPivot workbook can be refreshed on a schedule or on demand from its data sources once uploaded to SharePoint. These features are accessible after the workbook is uploaded.

a. In the PowerPivot Gallery, switch to the Library sub tab of the Library Tools ribbon tab and change the view to All Documents using the Current View dropdown list. The result is as shown in Error! Reference source not

found.below:

b. Hover over “PowerPivotLab” and click the checkbox at its left end. Then click the dropdown enabled on the Name column and select Manage PowerPivot Data Refresh.

Page 71: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 69 of 70

Tasks Detailed Steps

c. Explore the options available in the Manage Data Refresh page shown in Error!

Reference source not found.below. As you can see there are many options, and individual data sources from the workbook can be selected for refresh, although as of this writing all must be on the same refresh schedule.

Page 72: PowerPivotinSQLServer2008R2

PowerPivot in SQL Server 2008 R2

Page 70 of 70

Tasks Detailed Steps

Note: For more information related on how to configure data refresh in PowerPivot for SharePoint, see http://msdn.microsoft.com/library/ee210651(en-us,SQL.105).aspx.