Post on 27-Dec-2015
Solving Business Problems Solving Business Problems in OLAP Services Using in OLAP Services Using MDX MDX - Part II- Part II
Amir Netz – Dev Manager & ArchitectAmir Netz – Dev Manager & ArchitectAriel Netz – Program ManagerAriel Netz – Program Manager
SQL Server OLAP ServicesSQL Server OLAP ServicesMicrosoft CorporationMicrosoft Corporation
Yes,Yes,We Are Brothers.We Are Brothers.
Still
AgendaAgenda Exception floatingException floating Market basket analysisMarket basket analysis Working with virtual cubesWorking with virtual cubes Currency conversionCurrency conversion Using UDF in MDXUsing UDF in MDX
Knowing MDXKnowing MDX
Functionality Functionality
KnowledgeKnowledge RequiredRequired
Part I
For Each CurrentMember is the set:
Yes
No
CustomerCustomer Sales Sales (Printer)(Printer)
AmirAmir 150150
ArielAriel 4040
BillBill 7070
CoreyCorey 6565
Filter(Customers.Members,(Sales,Printers)>50)
{Amir,Ariel,Bill,Corey}
(Amir,Sales,Printer)>50IF
Amir
Filtering Sets
For Each CurrentMember is the set:
Yes
No
CustomerCustomer Sales Sales (Printer)(Printer)
AmirAmir 150150
ArielAriel 4040
BillBill 7070
CoreyCorey 6565
Filter(Customers.Members,(Sales,Printers)>50)
{Amir,Ariel,Bill,Corey}
(Ariel,Sales,Printer)>50IF
Amir
For Each CurrentMember is the set:
Yes
No
CustomerCustomer Sales Sales (Printer)(Printer)
AmirAmir 150150
ArielAriel 4040
BillBill 7070
CoreyCorey 6565
Filter(Customers.Members,(Sales,Printers)>50)
{Amir,Ariel,Bill,Corey}
(Bill,Sales,Printer)>50IF
AmirBill
For Each CurrentMember is the set:
Yes
No
CustomerCustomer Sales Sales (Printer)(Printer)
AmirAmir 150150
ArielAriel 4040
BillBill 7070
CoreyCorey 6565
Filter(Customers.Members,(Sales,Printers)>50)
{Amir,Ariel,Bill,Corey}
(Corey,Sales,Printer)>50IF
AmirBillCorey
CustomerCustomer Sales Sales (Printer)(Printer)
AmirAmir 150150
ArielAriel 4040
BillBill 7070
CoreyCorey 6565
Filter(Customers.Members,(Sales,Printers)>50)
{Amir,Bill,Corey}
Filter({Amir,Ariel,Bill,Corey},(Sales,Printers)>50)
Amir Netz:
Demo of filter – show set before and after
Amir Netz:
Demo of filter – show set before and after
Exception FloatingException Floating
Possible answer: 51% of the “Food” products sold
in 1998 did less then in 1997
For each product group find the the percentage For each product group find the the percentage
of the group’s products that had their sales of the group’s products that had their sales
decreased from the previous time perioddecreased from the previous time period
Product GroupProduct Group % Dropped% Dropped
FoodFood 51%51%DrinkDrink 27%27%Non-Consumable.Non-Consumable. 22%22%
Business Problem:Business Problem:
For each product group find the the For each product group find the the percentage of the group’s products that had percentage of the group’s products that had their sales decreased from the previous time their sales decreased from the previous time periodperiod
The number of products contained in the group
The number of products contained in the group that had their sales dropped from the previous time period
Phase I:Phase I:
M e at S . D ish C h ee se
D e li M e at
C a n dy C . B ar P o p C o rn
S n a cks
F o od
A lco h o lic S o ft D a iry
D rin k
A ll
The number of products contained in the current group
Count( products contained in the current group)
Count(Descendants(current group, [Product Name]))
Count(Descendants( Products.CurrentMember, [Product Name]))
Amir Netz:
Demo phase 1
Amir Netz:
Demo phase 1
Phase IIPhase II
The number of products contained in the current group that had their sales dropped from the previous time period
Count( products contained in the current group that had their sales dropped from the previous time period)
Count(Filter( products contained in the current group, sales < sales of the previous time period)
Count(Filter( Descendants( Products.CurrentMember, [Product Name]),
sales < sales of the previous time period)
Phase II - ContinuePhase II - Continue
Count(Filter( Descendants( Products.CurrentMember, [Product Name]),
([Sales],Time.CurrentMember)<([Sales],Time.CurrentMember.PrevMember)
Amir Netz:
Demo phase 2
Amir Netz:
Demo phase 2
Putting It All Together…Putting It All Together…
Count(Descendants( Products.CurrentMember, [Product Name]))
/Count(Filter( Descendants(
Products.CurrentMember, [Product Name]), ([Sales],Time.CurrentMember)<([Sales],Time.CurrentMember.PrevMember)
Gee…
Amir Netz:
demo
Amir Netz:
demo
Guided Drill DownGuided Drill Down
“There are so many numbers in my cube, can I have the system highlight the “interesting” numbers?”
Concept:Have the system guide the user to look at the “interesting” numbers. Guidance is given in the format of a textual massage.
Business Problem:
% Drop% Drop GuideGuide
Product GroupProduct Group 50%50% Drill HereDrill Here
FoodFood 51%51% Drill HereDrill Here
DrinkDrink 27%27%
NonNon
Consumable Consumable
22%22%
IIF(Measures.[% Dropped] >0.5,”Drill Here”,” ”)
Just like Exception Floating + one “IIF”Just like Exception Floating + one “IIF”
Amir Netz:
demo
Amir Netz:
demo
Distinct Count In The OLAP Space.Distinct Count In The OLAP Space.Business Problem:Sales Manager wants to know: “How many customers are buying Computers? ““How many active customers do I have?”
SalesSales Number of Number of CustomersCustomers
All productsAll products 80008000 200200 HardwareHardware 33003300 8080 ComputersComputers 20002000 7070 MonitorsMonitors 800800 6060 PrintersPrinters 500500 3030SoftwareSoftware 47004700 150150 HomeHome 15001500 100100 BusinessBusiness 25002500 100100 GamesGames 700700 8080
Distinct Count is not a CountDistinct Count is not a Count
First thing to know about Distinct Count :
Count( Filter (Customers.[Name].Members, Sales> 0))
Count( Filter (Descendants( Customers.CurrentMember, Customers.[Name]), Sales> 0))
Business Problem:Business Problem::“How many different customers had sales for the :“How many different customers had sales for the current product/time/geography/promotion…?”current product/time/geography/promotion…?”
Number of customers that had salesCount( customers that had sales)Count( Filter (customers , Sales of current product/time/geo…> 0))Count( Filter (customers , Sales > 0))
Amir Netz:
Demo with bug – discover the bug during demo and fix
Amir Netz:
Demo with bug – discover the bug during demo and fix
Market Basket Analysis Market Basket Analysis
SalesSales Number of Number of CustomersCustomers
Number Of Number Of Customers Which Customers Which
AlsoAlso Bought Bought ComputerComputer
All productsAll products 80008000 200200 3030HardwareHardware 33003300 8080 3030 PrintersPrinters 20002000 7070 2020 MonitorsMonitors 800800 6060 2525 ComputersComputers 500500 3030 3030SoftwareSoftware 47004700 150150 1515 HomeHome 15001500 100100 77 BusinessBusiness 25002500 100100 1010 GamesGames 700700 8080 55
Business Problem:Sales Manager wants to know: “It will be very interesting to know how many of my customers bought both product X and a computer. Maybe a bundle is in place?”
Market Basket Analysis Market Basket Analysis
PrinterPrinter ComputersComputers
WAWA Total WATotal WA
SeattleSeattle Total SeattleTotal Seattle 190190 2020
AmirAmir 150150 2020
ArielAriel 4040
TacomaTacoma Total TacomaTotal Tacoma 255255 140140
BillBill 7070 8080
CoreyCorey 6565 6060
SueSue 120120
Market Basket Analysis Market Basket Analysis
PrinterPrinter ComputersComputers Yes/NoYes/No
WAWA Total WATotal WA
SeattleSeattle Total SeattleTotal Seattle 190190 2020
AmirAmir 150150 2020
ArielAriel 4040
TacomaTacoma Total TacomaTotal Tacoma 255255 140140
BillBill 7070 8080
CoreyCorey 6565 6060
SueSue 120120
Market Basket Analysis Market Basket Analysis
PrinterPrinter ComputersComputers Yes/NoYes/No
WAWA Total WATotal WA
SeattleSeattle Total SeattleTotal Seattle 190190 2020
AmirAmir 150150 2020
ArielAriel 4040
TacomaTacoma Total TacomaTotal Tacoma 255255 140140
BillBill 7070 8080
CoreyCorey 6565 6060
SueSue 120120
11
00
11
11
00
11
22
33XX
Market Basket Analysis Market Basket Analysis
PrintPrinterer
CompuComputersters
Yes/Yes/NoNo
WWAA
Total WATotal WA
SeatSeattletle
Total Total SeattleSeattle
190190 2020
AmirAmir 150150 2020 11
ArielAriel 4040
TacTacomaoma
Total Total TacomaTacoma
255255 140140
BillBill 7070 8080 11
CoreyCorey 6565 6060 11
SueSue 120120
Concept:If a customer bought a Printer and a Computer, “flag” the customer with the value of “1”, then sum all the “1” to get a final count.
Count for each product the customers who bought it together with a Computer)
Sum(Descendents(Customers.CurrentMember,Customer.Name), IIf((Sales,Products.CurrentMember)>0 AND (Sales,Computers)>0 ,1,0))
Sum(Customers in the current region, IIf( Bought both the current product and a Computer, 1,0))Sum(Descendents(Customers.CurrentMember,Customer.Name), IIf(Bought current product AND a Computer,1,0))
Amir Netz:
demo
Amir Netz:
demo
Market Basket Analysis Market Basket Analysis PrintPrinterer
CompuComputersters
Yes/Yes/NoNo
WWAA
Total WATotal WA
SeatSeattletle
Total Total SeattleSeattle
190190 2020
AmirAmir 150150 2020 11
ArielAriel 4040
TacTacomaoma
Total Total TacomaTacoma
255255 140140
BillBill 7070 8080 11
CoreyCorey 6565 6060 11
SueSue 120120
Could we do it differently ?
Yes …
Count(Filter (Descendants(Customers.CurrentMember.Customer.Name), (Sales>0) AND (Sales,Computers)>0) ))
But …
Not efficient as using the SUM function.
Analyzing the PropertiesAnalyzing the PropertiesExample for Member Properties:Gender is a property of a customer.Education Level Property of an employee.Store size is a property of a Store.
Amir Netz:
Demo, show where member properties are being defined, how they look in the dimension browser
Amir Netz:
Demo, show where member properties are being defined, how they look in the dimension browser
Analyzing the PropertiesAnalyzing the PropertiesExample for Member Properties:Gender is a property of a customer.Education Level Property of an employee.Store size is a property of a Store.
Misconception:Member properties are only used to provide additional informationon a dimension member.
The truth about member properties:Member properties are being constantly used in data analysis.
Two ways of using Member Properties in data analysis:1. Virtual Dimensions2. MDX
Amir Netz:
Virtual Dimensions demo
Amir Netz:
Virtual Dimensions demo
Business Problem:What is the sales average per SQFT ?
Analyzing the PropertiesAnalyzing the Properties
Remember: The Store Size (SQFT) is not a Measure.
SalesTotal SQFT in Location X
Sales/Sum(Descendants(Store.CurrentMember,Store.[Store Name]),Val(Store.CurrentMember.Properties(“SQFT”)))
Amir Netz:
Show demo and show another example for finding the perfect wife
Amir Netz:
Show demo and show another example for finding the perfect wife
Virtual CubeVirtual CubeLike a View in a relational database but yet different
Time
Customers Warehouse
Time
Products
Products
Virtual Cube and Virtual Cube and Calculated MembersCalculated Members
Customers Warehouse
Time
Products
Common Dimensions
Note: Cubes joined in a Virtual Cube, are not required to have the same number of Dimensions
Amir Netz:
Show virtual cube, different views, what is valid and what is not
Amir Netz:
Show virtual cube, different views, what is valid and what is not
Currency Conversion Currency Conversion
Business Problem:The sales database is holding sales transactions in US Dollars. The company needs to show the sales figures in number of currencies.
Solution:Two approaches:1. Present converted sales figures based on the
exchange rate at the time of the sales transaction.2. Sales figures will be converted to currency X based on today's
exchange rate.
Currency Conversion Currency Conversion Solution:Two approaches:1. Present converted sales figures based on the
exchange rate at the time of the sales transaction.2. Sales figures will be converted to currency X based on today's
exchange rate.
Steps:1. Create a “Currency” cube - Currency cube holds Currency exchange
rate history.2. Create Virtual Cube joining the “Currency” cube with the “Sales” cube.3. Create “Calculated Member” for currency conversion.
Ariel:
Following this slide will have a demo. Demo will show how to create the currency cube, and the virtual cube
Ariel:
Following this slide will have a demo. Demo will show how to create the currency cube, and the virtual cube
Currency Conversion Currency Conversion The “Currency” cube:
Dimensions:•Time•Currency
Measures:•Index - Currency exchange rate for US Dollar
Currency Conversion Currency Conversion The Calculated Member: SalesSales IndexIndex C. MemberC. Member
19981998
180180 66
Q1Q1 180180 66
JanJan 3030 1.51.5 4545
FebFeb 5050 22 100100
MarchMarch 100100 2.52.5 250250
????????X10801080
Currency Conversion Currency Conversion The Calculated Member: SalesSales IndexIndex C. MemberC. Member
19981998
180180 66
Q1Q1 180180 66
JanJan 3030 1.51.5 4545
FebFeb 5050 22 100100
MarchMarch 100100 2.52.5 250250
395395
Currency Conversion Currency Conversion The Calculated Member: SalesSales IndexIndex C. MemberC. Member
19981998
180180 66
Q1Q1 180180 66 395395
JanJan 3030 1.51.5 4545
FebFeb 5050 22 100100
MarchMarch 100100 2.52.5 250250
Sum (Descendents(Time.CurrentMember,Month), ValidMeasure([Unit Sales]) *ValidMeasure( Index))
Sum (each month sales in a given period * Index)Sum (Descendents(Time.CurrentMember,Month), [Unit Sales] * Index)
Amir Netz:
Demo.
Amir Netz:
Demo.
Currency Conversion Currency Conversion Solution:Two approaches:1. Present converted sales figures based on the
exchange rate at the time of the transaction.2. Sales figures will be converted to currency X based on today's
exchange rate.
Steps:• Create a “Currency” dimension in the “Sales” cube.• Create a calculated member for Currency conversion• Make it smarter by using a UDF.
Amir Netz:
Demo, show with fix value first then suggest to go to the web
Amir Netz:
Demo, show with fix value first then suggest to go to the web
UDF - User Defined Function in MDXUDF - User Defined Function in MDX
UDF allows extension of the MDX UDF allows extension of the MDX languagelanguage
Any Automation library can be used Any Automation library can be used for UDF (VB, VC…)for UDF (VB, VC…) Just compile your VB class into an Just compile your VB class into an
ActiveX DLL and register the DLL in the ActiveX DLL and register the DLL in the OLAP ManagerOLAP Manager
Make sure to deploy the DLL on the Make sure to deploy the DLL on the client side!client side!
VBA and Excel are two libraries that VBA and Excel are two libraries that are automatically registered are automatically registered
Creating a UDFCreating a UDF
Public Function Currency_Convert(Currency_Name As String) As Double Dim strHTML As String Dim URL As String Dim istart As Integer Dim iend As Integer Dim CurrencyRate As Double Select Case Currency_Name Case "German Mark" URL = "http://quote.yahoo.com/m5?a=1&s=USD&t=DEM" Case "British Pound" URL = "http://quote.yahoo.com/m5?a=1&s=USD&t=GBP" Case "Italian Lira" URL = "http://quote.yahoo.com/m5?a=1&s=USD&t=ITL" Case "French Franc" URL = "http://quote.yahoo.com/m5?a=1&s=USD&t=FRF" End Select strHTML = Form1.Inet1.OpenURL(URL) iend = InStr(1, strHTML, "</b></td></tr></table><", vbTextCompare) istart = InStr(1, strHTML, "</td><td><b>", vbTextCompare) CurrencyRate = Val(Replace(Mid(strHTML, istart + Len("</td><td><b>"), iend - istart - Len("</td><td><b>")), ",", "", 1)) Currency_Convert = CurrencyRate
End Function
Functionality: •Currency Covert ( Input: Currency Name to be converted)•Based on “Currency Name” go to exchange rate URL.•Download HTML•Parse HTML and extract exchange rate for “Currency Name”.•Return Exchange rate value.