Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | debra-james |
View: | 214 times |
Download: | 0 times |
Budgeting Solution Built with PowerPivotAlberto FerrariBI Architect and ConsultantSQLBI.COM
SESSION CODE: BIU02
Who am I?
[email protected] Independent consultant10+ years on SQL Server5+ years on BI & OLAPBook author
Expert Cube Development with Microsoft SQL Server Analysis ServicesPowerPivot for Excel 2010:Give your data a meaning
Founder of SQLBI.COM
Agenda
From the user point of view…Focus on common PowerPivot data models
Ratios and PercentagesBanding (discretization)Simulations (Courier Simulations)Non additive measures (Current account balance)Dynamic Sets
In the meantimeSome insights into the DAX programming language
Ratios and PercentagesHow to compute ratios and percentages
What is a filter context?
Each cell, in a pivot table, defines a filter context
During cell evaluation, the whole database is filtered with the filter context, rows outside it are not considered.
(Australia, 2001)
The background
A ratio need to «exit» from the filter context
Ratio = divide the «part» over the «whole»But… The filter context hides the «whole»…
(Australia, 2001) (2001)
The solution: change the filter context with ALL
We need to define a new measureBased on CALCULATE
We create e new context for a specific dimensionIn order to make it consider all the values for that dimension
Perc = SUM(FactInternetSales[SalesAmount]) / CALCULATE ( SUM (FactInternetSales[SalesAmount]), ALL (DimSalesTerritory[SalesTerritoryCountry]) )
Ratios and PercentagesExcel sheet with ratios and percentages
BandingHow to handle analysis by discretization
Analysis of product sell price
Product price changes over time, due toDiscountsPrice variations
Price is a continuous dimensionLeading to a very high fragmentationClassical solution: BANDING
From 0 to 100 USDFrom 101 to 500…
Very common pattern, even with SSAS
Banding in PowerPivot: the quick and dirty solution= 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"))))
Even if this works… a better data modelwould be welcome!
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.ToPriceGROUP BY P.BandName
BandName FromPrice ToPrice
VERY LOW 0 5
LOW 5 30
MEDIUM 30 100
HIGH 100 500
VERY HIGH 500 9999
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.PriceGROUP BY P.BandName
Price BandName
1 VERY LOW
2 VERY LOW
3 VERY LOW
4 VERY LOW
5 VERY LOW
6 LOW
7 LOW
… …
PowerPivot first solution: Band Expansion
PowerPivot has no BETWEEN… Thus we need Band ExpansionWe need…
A configuration sheet with band configurationScript to expand the configuration tableCreate a new linked tableConvert prices into integer valuesSetup the relationship
Very simple solution, probably the first one
Band ExpansionBand Expansion, first trial
Banding with DAX
We can leverage DAX to implement BETWEENNo need to perform table expansionUsage of FILTER to find the correct bandUsage of MAXX to convert a table into a value
BandName=MAXX ( FILTER ( PriceBands, FactResellerSales[DiscountedPrice] >= PriceBands[MinPrice] && FactResellerSales[DiscountedPrice] < PriceBands[MaxPrice] ), [PriceBand])
MAXX does not work with strings…
MAXX works only with dates and numbers, no strings!Compatibility with Excel???
Add a new numeric codeUse MAXX to retrieve the numeric codeCreate a calculated column with the codeSet relationship on the code to find the band name
Tricky… yet it works fine and does not need VB scripts
Banding with DAXFirst DAX solution
Banding with CALCULATEThe key is CALCULATEChange the filter contextWorks with any expressionLeverages CALCULATE, VALUES and FILTER together
= CALCULATE( VALUES (PriceBands[PriceBand]), FILTER ( PriceBands, FactResellerSales[DiscountedPrice] >= PriceBands[MinPrice] && FactResellerSales[DiscountedPrice] < PriceBands[MaxPrice] ))
SimulationWhat if we want to choose a new courier?
The background
We have some courier proposalsParameters are
WeightDestination
Result is «Freight»We need to choose the best oneWe carry on a simulation based on the past shipmentsTwo problems
Determine the final weight of a shipmentCreate a good simulation environment
Analysis of the dataLet us take a brief tour on the source data
Compute the shipment weight
For each shipmentSum up the detail rowsUsing a standard many to one relationshipAnd the function RELATEDTABLE
= SUMX(RELATEDTABLE(OrderDetails),OrderDetails[TotalWeight])
Freight for each courier
For each courierNew calculated columnLeverage of FILTER to find the right freightAnd of MAXX to convert a table into a scalar value(yes, it works with numbers! )
=MAXX( FILTER( 'Couriers', [Country] = 'Orders'[Country] && [MinWeight] <= 'Orders'[Weight] &&'Orders'[Weight] < [MaxWeight] && [Courier] = "SpeedyMail"), 'Couriers'[Freight])
Using CALCULATE we get a more elegant solution
Using CALCULATEIf configuration sheet contains duplicates We get an errorCleaner solution, yet more complicated to understand
=CALCULATE( VALUES( 'Couriers'[Freight] ), FILTER( 'Couriers', [Country] = 'Orders'[Country] && [MinWeight] <= 'Orders'[Weight] &&'Orders'[Weight] < [MaxWeight] && [Courier] = "Blu Express") )
SimulationFirst solution to the simulation pattern
What if we have many couriers?
One calculated column for each courierMany columnsData model needs to be updated based on dataSoon we will introduce errors
Let us create a new solutionNew data modelBased on many to many relationship
ProblemsPowerPivot does not handle many to many relationshipsLess intuitive, yet very powerful
Data model with Many to ManyBridge table
With the calculation parametersCountryWeight
Two relationshipsWith OrdersWith PriceList
ProblemsPowerPivot does not handle many to many relationshipsRelationship based on two columnsNeed to create the bridgeAnd to expand the configuration
Bridge creation and expansion
Similar to the banding solutionEasily solved with some VbScript codeNo support in DAX or PowerPivot for this
Relationship with two columns
PowerPivot handles relationships based on one column onlyEven if we live in 2010…And the relational model is 41 years old
SolutionCreate a new calculated column to hold the relationshipOn all tables.
The DAX FormulaFar from being easyUses evaluation contexts:
Filter ContextRow Context
Uses CALCULATE to transform a ROW Context into a FILTER Context
=SUMX( Orders, CALCULATE ( IF( COUNT (PriceList[Freight]) = 1, VALUES (PriceList[Freight]), BLANK () ) ) )
Simulation with many to manySolution of the simulation pattern with Many To Many relationships
Semi Additive MeasuresHow to handle semi additive measures with PowerPivot
Semi Additive Measure - DefinitionAdditive Measure
Aggregates with SUM over all dimensionsNon Additive Measure
Aggregates with different function over all dimensionsExample: average of the sale price
Semi Additive MeasureAggregates with SUM over some dimensionsAggregates with different function over other dimensionsTime is the standard exception for aggregationsExamples
Warehouse stockingCurrent account balance
Example: current account balance
Month level correctQuarter level wrongYear level wrong
Non additive measures
Change the aggregation functionDirectly inside PowerPivotWe can do it
With the user interfaceDefining a new measure
We cannot model the aggregation function inside the database
Semi additive measuresAggregation function depend on the slicerIn the example
We should aggregate for LastChild over timeBut we should use SUM for the other dimensions
Semi Additive Measures: Data ModelWe need to define a time dimension
It is not required by defaultEven if attribute consolidation comes from its adoptionIn this situation it is mandatory
New measure:CALCULATE: to set the filterLASTDATE: to find the last child
LastBalance = CALCULATE ( SUM(Balances[Balance]), LASTDATE(Date[Date]))
Semi Additive MeasuresExcel sheet implementing semi additive measures
Dynamic SetsHere we discover the underlying SSAS cube in PowerPivot.
What is a set
Handled by ExcelStatic Set
Set of tuples (i.e. filter contexts)Uses standard MDX code or User InterfaceUser interface… works, but it could have been more user friendly
Dynamic SetNeed to be written with MDXAnd doing it…You will discover the SSAS cube inside PowerPivot
Dynamic SetsExcel sheet working with static and dynamic sets
LinksPowerPivothttp://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
Resources
www.microsoft.com/teched
Sessions On-Demand & Community Microsoft Certification & Training Resources
Resources for IT Professionals Resources for Developers
www.microsoft.com/learning
http://microsoft.com/technet http://microsoft.com/msdn
Learning
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
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.