Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Power excel 2016 with mrexcel Master Pivot Tables, Subtotals, Charts, VLOOKUP, IF, Data Analysis in Excel 2010–2013 (Bill Jelen) (z-lib.org).pdf
Скачиваний:
4
Добавлен:
14.08.2022
Размер:
49.75 Mб
Скачать

PART 3: WRANGLING DATA

379

 

 

Right-click on the heading, choose Rename, and type a meaningful field name such as Year.

Seems easy, right? The function is the same as in Excel. Now - there are 81 functions you can use here, and

79 of them are identical to Excel. One that is different: The TEXT function in Excel is FORMAT in DAX.

So, in the image below, the month name uses FORMAT.

Figure 969 DAX uses FORMAT instead of TEXT

It turns out that FORMAT offers choices for Quarter and Week, thus is more robust than TEXT.

Gotcha: each column can have only one formula. Every row in the column has to have an identical for- mula. You cannot refer to cells in other rows. Therefore, the concept of cell addresses like A2 is not relevant in Power Pivot.

Gotcha: As you are building your formula, you can click a field with the mouse to refer to that field. You cannot use the arrow keys to select a field.

 

REFER TO A RELATED TABLE IN A FORMULA

 

Problem: I am entering a formula in the Fact table. I need to lookup a value from Product table. I’ve al-

 

 

ready defined a relationship between the tables.

 

Strategy: The promise of Power Pivot is that you won’t have to do VLOOKUPs anymore. When you are

 

building a calculated field, you will have to use a simpler lookup function called RELATED. In the Fact

 

table, you can enter the following:

 

3

=[Quantity]*Related(Products[ListPrice])

 

That is a simple one-argument lookup function. This function tells Power Pivot to follow the defined rela-

 

tionship and retrieve the value from the other table.

 

Here is the easy way to build the formula: Type the equals sign, click Quantity, and then type the asterisk.

 

Type the first few letters of the Product table. You can then choose a field from the list. Highlight the field.

 

Press Tab to insert the field. Type the closing parentheses. Press Enter. Right-click the header to rename.

 

Figure 970 Formula AutoComplete helps so you don’t have to remember the syntax.

Gotcha: When you define a calculated column in the Power Pivot window, that value is calculated for every row in the table. This can be a lot of overhead for a 100 million row dataset. In contrast, the DAX measures are calculated only once for each cell in the pivot table.

380

 

 

 

POWER EXCEL WITH MR EXCEL

 

 

 

 

 

 

 

 

 

 

 

CREATING THE POWER PIVOT TABLE

 

 

The Pivot Table dropdown offers 8 choices and most of them

 

 

 

 

are fairly silly. PivotTable and Pivot Charts are obviously

 

 

 

 

needed. Pivot table pros will love the Flattened PivotTable.

 

 

 

 

But all of those choices in the middle are redundant. They

 

 

 

 

are trying to tell you that you can combine a pivot table and

 

 

 

 

a chart on the same worksheet. You already know this.

 

 

 

 

Plus, this menu might make someone think that these are

 

 

 

 

the only options. It is fine to have three charts in a horizon-

 

 

 

 

tal row and a pivot table vertically below those.

 

 

 

 

In real life, build your pivot tables and charts one at a time.

 

 

 

 

The first can go on a new worksheet. The others can go on

 

 

 

 

the existing worksheet.

 

 

 

 

The eighth choice is cool; a flattened pivot table is one

 

 

 

 

where the row labels automatically repeat, and the outer

Figure 971 One, two, or four tables and

 

 

row fields don’t have subtotals. This is great for creating a

 

 

summary table that will be used for future analysis

charts.

BUILDING THE PIVOT TABLE

Choose fields from any table and drag them to the four drop zones at the bottom of the field list. Since you’ve created relationships, you are free to use fields from any table.

The pivot table below has region from the Geography table, Excel version from the Product table, Revenue from the Fact table, and a Year slicer from the Date table.

Figure 972 Reporting from four worksheets without doing a VLOOKUP.

FEATURE X WON’T WORK IN POWER PIVOT

This is not a regular pivot cache pivot table that you’ve been using for the last 10 years. That data in your

Excel worksheets is now in the Data Model. Even though the Data Model is stored inside of your Excel file, it gets treated like external data.

A lot of features that you might love in regular pivot table are not available when the data is stored externally. This was particularly bad in version 1 of Power Pivot. The later versions of Power Pivot now in Excel

2013 and Excel 2016 have mitigated some of the problems. But, you will still run into problems:

PART 3: WRANGLING DATA

381

 

 

You can not group data. Excel 2016 adds the ability to group Dates. But if you want to create a strati- fication of invoice amounts, you can not group like a regular pivot table.

● Double-clicking to drill-down will only return 1000 rows

Pivot tables and Slicers are not automatically sorted by custom list. If you have access to the Power

Pivot tab in the ribbon, you can use the Manage icon, then Design, and specify that a column such as Month Name should be sorted by another column such as Month Number.

You can’t use the Show Report Filter Pages command to replicate the pivot table for every customer. ●The GETPIVOTDATA formula is wacky, which makes it significantly harder to parameterize your GETPIVOTDATA formulas. Where a regular pivot table would generate an argument pair of ““, the OLAP version of GETPIVOTDATA uses “[Sales].[Customer]”,”[Sales].[Customer].&[Astonishing Shoe Inc.]”. This means that you have to concatenate “““[Sales].[Customer]””,””[Sales].[Custom-

er].&[“ before the customer name and then “]” after the customer name.

REPLACE CALCULATED FIELDS WITH DAX

The DAX formula language really shines when you use it to create a new measure for your pivot table.

DAX measures are in the same genre as calculated fields, but are infinitely more powerful.

To create a new measure, select New Measure from the Power Pivot tab in Excel.

Figure 973 Define a new measure.

3

 

Build the measure in the Measure Settings dialog. Use the Check Formula button to check the syntax.

 

Figure 974 Define a new measure.

Excel will calculate the measure once for every value cell in the pivot table. In the figure below, this means that the calculation will happen 39 times. This is faster than adding the calculation to a million rows of source data.

382

POWER EXCEL WITH MR EXCEL

 

 

Figure 975 Excel calculates the formula 39 times in this pivot table.

A complete DAX reference can be found online, and I won’t attempt to replicate that guide here. However, the following topics are the a-ha moments in my power pivot learning curve.

CALCULATE() IS LIKE SUMIFS()

As you get started with DAX, you are going to find yourself using the CALCULATE function. This func- tion will perform a calculation while applying any number of filters. =CALCULATE(Sum(Field),Filter1,​ Filter2, Filter3).

PerhapsyouwanttocalculatesalesonSaturdaysinJanuary.Youmightthinkthatyouwouldhavetodo:=CA LCULATE(SUM(Sales[Revenue]),Sales[Weekday]=”Saturday”,Sales[Month]=”Jan”,​Sales[Year]=2016).

However, in the pivot table below, cell F4 already has filters applied to it. Cell F4 is limited to January by the month label in D4. Cell F4 is limited to 2016 by the slicer.

Figure 976 Cell F4 is already filtered to Month=Jan, Year-2016.

This simplifies your formula. You don’t have to specify a filter for Month or for Year, because those are already being handled by the pivot table. The formula for the measure in column F is =CALCULATE(​­ SUM(Sales[Revenue]),Sales[Weekday]=”Saturday”).

Rule #1: Calculate() respects the filters already applied to each cell in a pivot table. Those filters can come from slicers, report filters, row labels, or column labels.

UNAPPLY A FILTER USING DAX

Contrast SUMIFS and Calculate.

With SUMIFS, you go through a data set, finding rows that match all of the criteria.

PART 3: WRANGLING DATA

383

 

 

With Calculate, you go through a data set, calculating values that match the filters in Calculate. BUT...you also have an external outside force that is forcing other filters to be applied. Those filters might be coming from the slicers or even from the row and column labels. When Power Pivot goes about calculating cell F4 in the figure above, it has to respect the weekday=Saturday in the Calcu- late function, but it also has to respect Month=Jan caused by the row label in D4 and Year=2016 caused by the slicer.

Ready for something amazing? The filters in Calculate have the power to tell the external outside force to not apply a certain filter. If that formula up in F4 used a filter of Month=”Feb”, the filter in the Calculate formula would override the filter from the row label in D4. Let me show you an example.

Consider this figure.

Figure 977 These DAX Measures unapply a filter.

 

Column E, Sum of Revenue is a regular old field where I took the Revenue field from the field list

 

 

and put it in the values drop zone. Column E respects the filters in the slicer and the filters of the

3

 

row labels in column D.

Column F is a DAX Measure where I used Calculate to override the filter on sector. No matter what

 

 

 

label is over in column D, the DAX measure in column F will filter sector to Apparel. Column F still

 

 

continues to respect the month slicer, though. The formula for the measure in F is =CALCULATE(​

 

SUM(Sales[Revenue]),Sector[Sector]=”Apparel”)

 

Column G is a DAX Measure where I wiped out the Sector filter by using ALL. Every row in column

 

 

G is going to show the total for all sectors, even though the row label in D5 says that this row is for

 

 

Appliance. The formula for the measure in G is =CALCULATE(SUM(Sales[Revenue]),All(Sector)).

 

Note that this formula still respects the filter applied in the month slicer.

 

Column H is the actual useful field. It takes the revenue for this sector and divides it by the rev-

 

 

enue for the Apparel sector. The formula here re-uses the existing DAX measure from column F:

 

 

=sum(Sales[Revenue])/Sales[ApparelSector]. Of course, this formula still respects the month filter

 

 

applied from the slicer.

 

As you change the filters other than sector, all of the formulas update. Here is the same pivot table filtered to June, July, and August.

Figure 978 Change any filters other than Sector to recalculate.