480 likes | 699 Views
Required Slide. Required Slide. SESSION CODE: BIU02. Budgeting Solution Built with PowerPivot. Alberto Ferrari BI Architect and Consultant SQLBI.COM. Who am I?. alberto.ferrari@sqlbi.com Independent consultant 10+ years on SQL Server 5+ years on BI & OLAP Book author
E N D
Required Slide SESSION CODE: BIU02 Budgeting Solution Built with PowerPivot Alberto Ferrari BI Architect and Consultant SQLBI.COM
Whoam I? • alberto.ferrari@sqlbi.com • Independent consultant • 10+ years on SQL Server • 5+ years on BI & OLAP • Book author • Expert Cube Development with Microsoft SQL Server Analysis Services • PowerPivot for Excel 2010:Give your data a meaning • Founder of SQLBI.COM
Agenda • From the userpoint of view… • Focus on common PowerPivotdata models • Ratios and Percentages • Banding (discretization) • Simulations (CourierSimulations) • Non additive measures (Current account balance) • Dynamic Sets • In the meantime • Some insightsinto the DAX programminglanguage
Ratios and Percentages How to compute ratios and percentages
Whatis a filtercontext? • Eachcell, in a pivot table, defines a filtercontext Duringcellevaluation, the whole database isfiltered with the filtercontext, rowsoutsideit are notconsidered. (Australia, 2001)
The background • A ratio need to «exit» from the filtercontext Ratio = divide the «part» over the «whole» But…The filtercontexthides the «whole»… (Australia, 2001) (2001)
The solution: change the filtercontext with ALL • Weneed to define a new measure • Based on CALCULATE • We create e new context for a specificdimension • In order to makeitconsiderall the values for thatdimension Perc = SUM(FactInternetSales[SalesAmount]) / CALCULATE (SUM (FactInternetSales[SalesAmount]),ALL (DimSalesTerritory[SalesTerritoryCountry]) )
Ratios and Percentages Excel sheet with ratios and percentages
Banding How to handleanalysis by discretization
Analysis of product sell price • Product pricechanges over time, due to • Discounts • Price variations • Price is a continuousdimension • Leading to a very high fragmentation • Classicalsolution: BANDING • From 0 to 100 USD • From 101 to 500 • … • Very common pattern, even with SSAS
Banding in PowerPivot: the quick and dirtysolution Evenifthisworks… a better data model would be welcome! = IF ( FactResellerSales[DiscountedPrice] <= 5, "01 LOW", IF ( FactResellerSales[DiscountedPrice] <=30, "02 MEDIUM", IF ( FactResellerSales[DiscountedPrice] <=100, "03 MEDIUM", IF ( FactResellerSales[DiscountedPrice] <= 500, "04 HIGH", "05 VERY HIGH"))))
Banding: a data driven model SELECT P.BandName, SUM (S.ExtendedAmount) FROM dbo.FactResellerSales S JOIN PriceBands P ON S.UnitPrice BETWEEN P.FromPrice AND P.ToPrice GROUP BY P.BandName
Banding data model with PowerPivot We can leverage BAND Expansion with a different data model SELECT P.BandName, SUM (S.ExtendedAmount) FROM dbo.FactResellerSales S JOIN PriceBands P ON S.UnitPrice = P.Price GROUP BY P.BandName
PowerPivot first solution: Band Expansion • PowerPivothas no BETWEEN… • Thusweneed Band Expansion • Weneed… • A configurationsheet with band configuration • Script to expand the configurationtable • Create a new linkedtable • Convertpricesintointegervalues • Setup the relationship • Verysimplesolution, probably the first one
Band Expansion Band Expansion, first trial
Banding with DAX • We can leverage DAX to implement BETWEEN • No need to performtableexpansion • Usage of FILTER to find the correct band • Usage of MAXX to convert a tableinto a value BandName=MAXX (FILTER (PriceBands, FactResellerSales[DiscountedPrice] >= PriceBands[MinPrice] && FactResellerSales[DiscountedPrice] < PriceBands[MaxPrice] ), [PriceBand])
MAXX doesnot work with strings… • MAXX worksonly with dates and numbers, no strings! • Compatibility with Excel??? • Adda new numeric code • Use MAXX to retrieve the numeric code • Create a calculatedcolumn with the code • Set relationship on the code to find the band name • Tricky… yetitworks fine and doesnotneed VB scripts
Banding with DAX First DAX solution
Banding with CALCULATE • The keyis CALCULATE • Changethe filtercontext • Works with anyexpression • Leverages CALCULATE, VALUES and FILTER together = CALCULATE(VALUES (PriceBands[PriceBand]),FILTER (PriceBands, FactResellerSales[DiscountedPrice] >= PriceBands[MinPrice] && FactResellerSales[DiscountedPrice] < PriceBands[MaxPrice] ))
Simulation Whatifwewant to choose a new courier?
The background • Wehave some courierproposals • Parameters are • Weight • Destination • Resultis «Freight» • Weneed to choose the best one • Wecarry on a simulationbased on the pastshipments • Twoproblems • Determine the finalweight of a shipment • Create a goodsimulationenvironment
Analysis of the data Letus take a brief tour on the source data
Compute the shipmentweight • For eachshipment • Sum up the detailrows • Using a standard many to onerelationship • And the function RELATEDTABLE = SUMX(RELATEDTABLE(OrderDetails),OrderDetails[TotalWeight])
Freight for eachcourier • For eachcourier • New calculatedcolumn • Leverage of FILTER to find the right freight • And of MAXX to convert a tableinto a scalar value(yes, itworks with numbers! ) =MAXX( FILTER( 'Couriers', [Country] = 'Orders'[Country] && [MinWeight] <= 'Orders'[Weight] &&'Orders'[Weight] < [MaxWeight] && [Courier] = "SpeedyMail"), 'Couriers'[Freight])
Using CALCULATE weget a more elegantsolution • Using CALCULATE • Ifconfigurationsheetcontainsduplicates Weget an error • Cleanersolution, yet more complicated to understand =CALCULATE( VALUES( 'Couriers'[Freight] ), FILTER( 'Couriers', [Country] = 'Orders'[Country] && [MinWeight] <= 'Orders'[Weight] &&'Orders'[Weight] < [MaxWeight] && [Courier] = "Blu Express") )
Simulation First solution to the simulation pattern
Whatifwehavemanycouriers? • Onecalculatedcolumn for eachcourier • Manycolumns • Data model needs to be updatedbased on data • Soonwewill introduce errors • Letus create a new solution • New data model • Based on many to manyrelationship • Problems • PowerPivotdoesnothandlemany to manyrelationships • Less intuitive, yetverypowerful
Data model with Many to Many • Bridge table • With the calculationparameters • Country • Weight • Tworelationships • With Orders • With PriceList • Problems • PowerPivotdoesnothandlemany to manyrelationships • Relationshipbased on twocolumns • Need to create the bridge • And to expand the configuration
Bridge creation and expansion • Similar to the bandingsolution • Easilysolved with some VbScript code • No support in DAX or PowerPivot for this
Relationship with twocolumns • PowerPivothandlesrelationshipsbased on onecolumnonly • Evenifwe live in 2010… • And the relational model is 41 yearsold • Solution • Create a new calculatedcolumn to hold the relationship • On alltables.
The DAX Formula • Far from being easy • Usesevaluationcontexts: • FilterContext • RowContext • Uses CALCULATE to transform a ROW Contextinto a FILTER Context =SUMX( Orders, CALCULATE ( IF( COUNT (PriceList[Freight]) = 1, VALUES (PriceList[Freight]), BLANK () ) ) )
Simulation with many to many Solution of the simulation pattern with Many To Manyrelationships
Semi Additive Measures How to handle semi additive measures with PowerPivot
Semi Additive Measure - Definition • Additive Measure • Aggregates with SUM over alldimensions • Non Additive Measure • Aggregates with differentfunction over alldimensions • Example: average of the sale price • Semi Additive Measure • Aggregates with SUM over some dimensions • Aggregates with differentfunction over otherdimensions • Time is the standard exception for aggregations • Examples • Warehousestocking • Current account balance
Example: current account balance • Monthlevelcorrect • Quarterlevelwrong • Yearlevelwrong
Non additive measures • Change the aggregationfunction • Directly inside PowerPivot • We can do it • With the userinterface • Defining a new measure • Wecannot model the aggregationfunction inside the database
Semi additive measures • Aggregationfunctiondepend on the slicer • In the example • Weshould aggregate for LastChild over time • Butweshould use SUM for the otherdimensions
Semi Additive Measures: Data Model • Weneed to define a time dimension • Itisnotrequired by default • Evenifattributeconsolidationcomes from itsadoption • In this situation itismandatory • New measure: • CALCULATE: to set the filter • LASTDATE: to find the last child LastBalance = CALCULATE (SUM(Balances[Balance]), LASTDATE(Date[Date]))
Semi Additive Measures Excel sheetimplementing semi additive measures
Dynamic Sets Here wediscover the underlying SSAS cube in PowerPivot.
Whatis a set • Handled by Excel • Static Set • Set of tuples (i.e. filtercontexts) • Uses standard MDX code or User Interface • User interface… works, butitcouldhavebeen more userfriendly • Dynamic Set • Need to be written with MDX • And doingit… • Youwilldiscover the SSAS cube inside PowerPivot
Dynamic Sets Excel sheetworking with static and dynamic sets
Links • PowerPivothttp://www.powerpivot.com • DAX Introductionhttp://blogs.msdn.com/powerpivot/archive/2010/04/05/dax-data-analysis-expressions-measures-in-powerpivot.aspx • Marco Russo’s Bloghttp://www.sqlblog.com/Blogs/marco_russo/default.aspx
Required Slide Resources Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers • http://microsoft.com/technet • http://microsoft.com/msdn
Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Related Content • BIU302 – Enriching Microsoft PowerPivot for Microsoft Excel Applications Using DAX • BIU05-INT – DAX Patterns in PowerPivot • BIU02-HOL – Defining DAX Calculations with MS PowerPivot for MS Excel 2010 • Product Demo Stations
Required Slide Complete an evaluation on CommNet and enter to win!
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.