SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
What All Microsoft BI Developers
  Need to Know About MDX
         Speaker: Nathan Peterson
           Solid Quality Mentors


      Silicon Valley SQL Server User Group
                January 19, 2009




         Mark Ginnebaugh, User Group Leader,
               mark@designmind.com
Nathan Peterson

 Solid Quality Mentors
   Currently working on an Analysis Services project
   for the US Army

 Lead developer of a local cube generating utility
 called CubeSlice
What do all developers need to know about
MDX?

 You should be able to:
   Create and debug Calculated Members.
   Create and debug Named Sets.
 What you need to know to accomplish this:
   Understand MDX Concepts.
   Learn MDX Syntax.
   Learn some MDX functions.
   Learn some practical applications of MDX.
What Is MDX?

 Multi-Dimensional eXpressions
 SQL is used for relational queries, while MDX is used
 for multidimensional queries.
 Calculated members allow you to incorporate
 multidimensional logic into your cube.
 Named sets allow you to pre-define dynamic groups
 of members to be displayed on columns or rows.
Sample calculated member

with member
measures.[Last Year Sales Amount]
as
(
measures.[Sales Amount],
ParallelPeriod
   (
   [Date].[Calendar].[Calendar Year],
   1,
   [Date].[Calendar].CurrentMember
   )
)
Sample named set


with
set [Top 5 Products]
as
topcount
   (
   [Product].[Product].Members,
   5,
   Measures.[Internet Sales Amount]
   )
Comparing MDX to SQL

 From
   SQL selects from a table.
   MDX selects from a cube.
 Dimensionality
   SQL returns one or more columns and 0 or more rows.
   MDX can return data for 0, 1, 2, 3 or more axes.
 Where
   SQL has a filter.
   MDX has a slicer.
MDX query with two axes and two slicers


select
Measures.AllMembers on columns,
[Date].[Calendar].&[2003].Children on rows
from [Adventure Works]
where
(
[Customer].[Customer Geography].[France],
[Product].[Product Categories].[Bikes]
)
Dimensions and Measures
Same query, using axis numbers instead of axis
names


select
Measures.AllMembers on 0,
[Date].[Calendar].&[2003].Children on 1
from [Adventure Works]
where
(
[Customer].[Customer Geography].[France],
[Product].[Product Categories].[Bikes]
)
Using MDX requires multidimensional thinking

 In a cube
   Dimensions have hierarchies
   Hierarchies have levels
   Levels have members
 In a cellset
   Each axis contains a set
   Each set consists of a group of members or a group of
   tuples
   Tuples have members from different hierarchies
Multidimensional thinking – Where are you?

 Every member is in a level.
 Every level is in a hierarchy.
 Every hierarchy is a part of a dimension.
 For every cell of the cellset, there’s (almost) always
 a Current Member for each hierarchy.
 The Current Member may be set from the column,
 the row, the slicer, or the default member.
Create a set by listing members

 The simplest set is a list of members.
 The boundaries of the set are indicated with curly
 braces {}.
  select
  {
  [Product].[Category].[All Products],
  [Product].[Category].[Accessories],
  [Product].[Category].[Bikes],
  [Product].[Category].[Clothing],
  [Product].[Category].[Components]
  } on 0
  from [Adventure Works]
Create a set with a function

 There are many MDX functions that create sets.
 This query uses the .Members function to return all
 the members from a hierarchy – and gives the same
 result as the previous query.
 You don’t have to use {} when you create a set with
 a function.
  select
  [Product].[Category].Members on 0
  from [Adventure Works]
Tuples

 Each cell in a cellset shows data for one particular
 member from each hierarchy.
 A tuple is a way of specifying which member should
 be used from each of the hierarchies.
 Tuples are written like points in a geometric grid:
   (6,3) identifies point 6 on the x-axis and point 3 on the
   y-axis.
   ([2009],[US],[Bikes],[Internet Sales Amount]) identifies
   Internet Sales for Bikes in the US in 2009.
2 Dimensional Tuple


        6
        5       2 Dimensional Tuple
        4
        3
        2                           (6, 2)
        1



            1   2   3   4   5   6     7      8
Create a set by listing tuples

   select
   {
     (
     [Date].[Calendar Year].&[2002],
     [Product].[Category].[Accessories]
       ),
       (
       [Date].[Calendar Year].&[2002],
       [Product].[Category].[Bikes]
       ),
       (
       [Date].[Calendar Year].&[2003],
       [Product].[Category].[Accessories]
       ),
       (
       [Date].[Calendar Year].&[2003],
       [Product].[Category].[Bikes]
       )
   }
   on 0
   from [Adventure Works]
Listing Tuples


          [Accessories]
             [Clothing]         2 Dimensional Tuple
        [Touring Bikes]
          [Road Bikes]
                                ([2006], [Mountain Bikes])
       [Mountain Bikes]
         [All Products]



                          2     2     2     2     2     2     2     2
                          0     0     0     0     0     0     0     0
                           0     0     0     0     0     0     0     0
                            1     2     3     4     5     6     7     8
Create a set of tuples by multiplying two sets of
members

 select
 {
   [Date].[Calendar Year].&[2002],
   [Date].[Calendar Year].&[2003]
 }
 *
 {
   [Product].[Category].[Accessories],
   [Product].[Category].[Bikes]
 }
 on 0
 from [Adventure Works]
3 Dimensional Tuple


       [Accessories]
          [Clothing]         3 Dimensional Tuple
                             Difficult to Show in 3 Axes –
     [Touring Bikes]         Where to place the point?
       [Road Bikes]
                        ([2006], [Mountain Bikes], [Red])
    [Mountain Bikes]
      [All Products]


               [Red]
           [Blue]
                       2     2     2     2     2     2     2     2
       [Pink]          0     0     0     0     0     0     0     0
                        0     0     0     0     0     0     0     0
                         1     2     3     4     5     6     7     8
3 Dimensional Tuple
Combining 2 Hierarchies on 1 Axis


        ([Blue], [Road Bikes])
        ([Red], [Road Bikes])           3 Dimensional Tuple
    ([Blue], [Mountain Bikes])
    ([Red], [Mountain Bikes])

       ([Blue], [All Products])
                                  ([2006], [Mountain Bikes], [Red])
       ([Red], [All Products])



                                  2     2     2     2     2     2     2     2
                                  0     0     0     0     0     0     0     0
                                   0     0     0     0     0     0     0     0
                                    1     2     3     4     5     6     7     8
The Where clause

 The Where clause is an optional part of an MDX
 query.
 The Where clause is a slicer, not a filter.
   You can’t use the Where clause for limiting the value to
   a threshold (Sales over $10,000).
   Use the Filter function for filtering.
 The Where clause often looks like a tuple and
 actually is a tuple – but it doesn’t have to be.
Where clause using a tuple – one member from
each referenced hierarchy


  select
  Measures.[Internet Sales Amount] on 0
  from [Adventure Works]
  where
  (
  [Customer].[Customer Geography].[France],
  [Product].[Product Categories].[Bikes]
  )
Where clause with slicing on multiple members
from one hierarchy


select
Measures.[Internet Sales Amount] on 0
from [Adventure Works]
where
(
[Customer].[Customer Geography].[France],
  {
  [Product].[Product Categories].[Bikes],
  [Product].[Product Categories].[Clothing]
  }
)
The With clause

 The With clause is used for creating both calculated
 members and named sets.
 These calculated members and named sets are only
 valid for the life of the query.
 You can also create calculated members and named
 sets that are persistent:
   Lasting for the life of a session.
   Created permanently in the cube.
Creating calculated member in the With clause


with
member Measures.[Average Internet Sales]
as
[Measures].[Internet Sales Amount]
/
[Measures].[Internet Order Count],
format_string = "currency"
select
{Measures.[Average Internet Sales]} on 0
from [Adventure Works]
Members, Sets, Tuples


                ([Blue], [Road Bikes])
                ([Red], [Road Bikes])              Members, Sets, Tuples
            ([Blue], [Mountain Bikes])
Set of
(Partial)   ([Red], [Mountain Bikes])
Tuples
               ([Blue], [All Products])
                                              ([2006], [Mountain Bikes], [Red])
               ([Red], [All Products])
                                                               (Partial) Tuple
              Member

                                             2     2     2      2     2     2     2     2
                                             0     0     0      0     0     0     0     0
                                              0     0     0      0     0     0     0     0
                                               1     2     3      4     5     6     7     8


                                          Set of Members
Creating named set in the With clause

with
set [Products - High Selling on Internet]
as
filter
   (
   [Product].[Product].[Product].Members,
   [Measures].[Internet Sales Amount]
      > 50000
   )
select
{Measures.[Internet Sales Amount]} on 0,
[Products - High Selling on Internet] on 1
from [Adventure Works]
Full Tuple


       ([Blue], [Road Bikes])    ([2006], [Mountain Bikes], [Red], [All
        ([Red], [Road Bikes])    Promotions], [All Gender],
                                 [Measures].[Reseller Sales Amount],
   ([Blue], [Mountain Bikes])
                                 etc…)
    ([Red], [Mountain Bikes])
      ([Blue], [All Products])
      ([Red], [All Products])



                                 2     2     2     2     2     2     2     2
                                 0     0     0     0     0     0     0     0
                                  0     0     0     0     0     0     0     0
                                   1     2     3     4     5     6     7     8
MDX Punctuation


 Use {} for sets.
 Use () for tuples.
 Use [] for names of cubes, dimensions, hierarchies,
 levels, and members.
 Use commas all over the place (except not between
 multiple calculated members or named sets defined
 in the same query).
Where to write your MDX

 In a client application like PerformancePoint, which
 allows you to write code for calculated measures
 and sets.
 In a query editor like the SQL Server Management
 Studio.
 In the MDX Script of a cube.
 TIP: It’s often easier to write the code in a query editor
   first and then put it into the MDX Script.
Writing MDX for a KPI


 You can write MDX to create the values for a KPI,
 whether you’re working with Analysis Services KPIs
 or PerformancePoint Services KPIs.
 For both tools, it’s easier to create calculated
 members in the MDX script for the values first.
 TIP: When building KPIs in the BIDS, use a template
 for assigning the Status and the Trend.
Functions – Navigating within a level


 PrevMember
 NextMember
 Lag()
 Lead()
Examples of navigating in a level

 Navigation functions are often used with Dates.
 Note: PrevMember, Lag(1), and Lead(-1) all return
 the same member.
 [1997].PrevMember
                [1996]
 [March 2010].NextMember [April 2010]
 [10 April 2009].Lag(2)        [8 April
 2009]
 [December 2009].Lead(12) [December 2010]
Functions – All in the Family


  Parent
  Children, FirstChild, LastChild
  Descendants
  Ancestor, Ascendants
  Siblings, FirstSibling, LastSibling
  Cousin
Examples of family functions

  Some of the family functions return individual
  members, while other return sets.
  If a function returns an individual member, you can
  use another function with it.
[Mar 1997].Parent                   [1997]
[Q1 2009].Children             [Jan 09]:[Mar
09]
[2009].FirstChild                   [Q1 2009]
Descendants([2009], 2)    [Jan 09]:[Dec 09]
Descendants([2009],[Date])[1Jan09]:[31Dec09]
Ancestor([Mar 2008], 3)   [2008]
FirstSibling([Mar 2007]) [Jan 2007]
Functions – Multiplying, adding, subtracting,
intersecting sets

  Set multiplication, addition, and subtractions can
  be indicated with mathematical symbols or with
  functions.

  Multiplication         *            Crossjoin
  Addition               +            Union
  Subtraction            -            Except
  Intersection                        Intersect
Example of set multiplication

 These two sets are equivalent.

  [Date].[Fiscal].[Month].members
  *
  [Product].[Product].members

  Crossjoin
    (
    [Date].[Fiscal].[Month].members,
    [Product].[Product].members
    )
Practical Applications of MDX


 Comparing with a previous time period.
 Comparing with the same time period in the
 previous year.
 Year-To-Date.
 Rolling Average.
 Percent contribution.
Comparing with a previous time period

with member Measures.[Last Month Sales]
as
(
Measures.[Internet Sales Amount],
[Date].[Calendar].PrevMember
)
select
{
Measures.[Internet Sales Amount],
Measures.[Last Month Sales]
} on 0,
[Date].[Calendar].[Month].members on 1
from [Adventure Works]
Comparing with the same period in the previous
year

with member
measures.[Last Year Sales Amount]
as
(
measures.[Sales Amount],
ParallelPeriod
   (
   [Date].[Calendar].[Calendar Year],
   1,
   [Date].[Calendar].CurrentMember
   )
)
Year-To-
Year-To-Date

 with
 member measures.[YTD Sales]
 as
 sum
   (
   ytd([Date].[Calendar].CurrentMember),
   Measures.[Internet Sales Amount]
   )
 select
 {
 Measures.[Internet Sales Amount],
 Measures.[YTD Sales]
 } on 0,
 [Date].[Calendar].[Month].members on 1
 from [Adventure Works]
Rolling Average

With
member measures.[Previous 6 Month Rolling AVG]
as
sum
  (
    [Date].[Calendar].lag(6):
    [Date].[Calendar].lag(1),
  Measures.[Internet Sales Amount]
  ) / 6
select
{
Measures.[Internet Sales Amount],
Measures.[Previous 6 Month Rolling AVG]
} on 0,
[Date].[Calendar].[Month].members on 1
from [Adventure Works]
Percent Contribution

With member Measures.[Percent of Parent Sales]
as
Measures.[Internet Sales Amount]
/
  (
  Measures.[Internet Sales Amount],
  [Product].[Product Categories].Parent
  )
select
{
Measures.[Internet Sales Amount],
Measures.[Percent of Parent Sales]
} on 0,
[Product].[Product Categories]. members on 1
from [Adventure Works]
Additional topics – if there’s time


   Dealing with exceptions
   String manipulation
   Generate
   Recursion
Dealing with exceptions

  A calculation needs to be valid everywhere
 with
 member [Measures].[% Change # of Customers]
 as
 case
   when [Date].[Fiscal].CurrentMember.Level.Ordinal = 0 then "NA”
   when [Date].[Fiscal].CurrentMember.Level.Ordinal = 5 then Null
   when isempty
        (
          ([Date].[Fiscal].CurrentMember.PrevMember, [Measures].[Customer Count])
        ) then null
   else
 (
   ([Date].[Fiscal].CurrentMember, [Measures].[Customer Count])
   -
   ([Date].[Fiscal].PrevMember, [Measures].[Customer Count])
 )
 /
   ([Date].[Fiscal].PrevMember,[Measures].[Customer Count])
 end
Functions - String manipulation

 Turning the current date into a member reference

Strtomember
  (
    "[Date].[Fiscal].[Date].&["
    + cstr(year(now()) - 6)
    + right("0" + cstr(month(now())), 2)
    + right("0" + cstr(day(now())), 2)
    + "]"
  )
The Generate function

 What are the five top products most purchased by
 people living in my top five cities?
  generate
    (
    topcount
      (
      [Customer].[Customer Geography].[City].members,
      5,
      [Measures].[Internet Sales Amount]
      ),
    crossjoin
      (
      [Customer].[Customer Geography].CurrentMember,
      topcount
        (
        [Product].[Product].[Product].members,
        5,
        [Measures].[Internet Sales Amount]
    ) ) )
Recursion

 Finding the most recent purchase

with
member measures.[Most Recent Purchase]
as
iif
   (
   [Measures].[Internet Sales Amount] > 0,
   [Measures].[Internet Sales Amount],
   [Date].[Calendar].PrevMember
   )
Thank you
npeterson@solidq.com

http://www.databasejournal.com/article.php/1459531/

http://msdn.microsoft.com/en-us/library/ms145970.aspx

http://www.sqlserveranalysisservices.com

http://www.ssas-info.com/analysis-services-faq/27-mdx

http://www.mosha.com/msolap/mdx.htm

http://cwebbbi.spaces.live.com
To learn more or inquire about speaking opportunities, please contact:

                Mark Ginnebaugh, User Group Leader
                      mark@designmind.com

Más contenido relacionado

Destacado

Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
Doug Armantrout
 
Multidimensional expressions mdx - reference
Multidimensional expressions   mdx - referenceMultidimensional expressions   mdx - reference
Multidimensional expressions mdx - reference
Steve Xu
 
rmoore.smartquery KScope15
rmoore.smartquery KScope15rmoore.smartquery KScope15
rmoore.smartquery KScope15
Ron Moore
 
Moore Advanced Calculations in Calc Manager OW 20151015
Moore Advanced Calculations in Calc Manager  OW 20151015Moore Advanced Calculations in Calc Manager  OW 20151015
Moore Advanced Calculations in Calc Manager OW 20151015
Ron Moore
 
Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010
Ron Moore
 

Destacado (20)

Introduction to mdx query ppt
Introduction to mdx query pptIntroduction to mdx query ppt
Introduction to mdx query ppt
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Ssis sql ssrs_sp_ssas_mdx_hb_li
Ssis sql ssrs_sp_ssas_mdx_hb_liSsis sql ssrs_sp_ssas_mdx_hb_li
Ssis sql ssrs_sp_ssas_mdx_hb_li
 
Multidimensional expressions mdx - reference
Multidimensional expressions   mdx - referenceMultidimensional expressions   mdx - reference
Multidimensional expressions mdx - reference
 
Mdx university dubai courses
Mdx university dubai coursesMdx university dubai courses
Mdx university dubai courses
 
SQL Server Analysis Services and MDX
SQL Server Analysis Services and MDXSQL Server Analysis Services and MDX
SQL Server Analysis Services and MDX
 
Mdx complex-queries-130019
Mdx complex-queries-130019Mdx complex-queries-130019
Mdx complex-queries-130019
 
Enhancing Dashboard Visuals with Multi-Dimensional Expressions (MDX)
Enhancing Dashboard Visuals with Multi-Dimensional Expressions (MDX)Enhancing Dashboard Visuals with Multi-Dimensional Expressions (MDX)
Enhancing Dashboard Visuals with Multi-Dimensional Expressions (MDX)
 
2012 Acura MDX Brochure | DCH Acura of Temecula
2012 Acura MDX Brochure | DCH Acura of Temecula2012 Acura MDX Brochure | DCH Acura of Temecula
2012 Acura MDX Brochure | DCH Acura of Temecula
 
Citrix MDX Technologies Feature Brief
Citrix MDX Technologies Feature BriefCitrix MDX Technologies Feature Brief
Citrix MDX Technologies Feature Brief
 
MDX 2015-2019 Work Program Overview presentation, October 22, 2014
MDX 2015-2019 Work Program Overview presentation, October 22, 2014MDX 2015-2019 Work Program Overview presentation, October 22, 2014
MDX 2015-2019 Work Program Overview presentation, October 22, 2014
 
IBM Cognos Dimensional Dashboarding Techniques
IBM Cognos Dimensional Dashboarding TechniquesIBM Cognos Dimensional Dashboarding Techniques
IBM Cognos Dimensional Dashboarding Techniques
 
rmoore.smartquery KScope15
rmoore.smartquery KScope15rmoore.smartquery KScope15
rmoore.smartquery KScope15
 
Moore Advanced Calculations in Calc Manager OW 20151015
Moore Advanced Calculations in Calc Manager  OW 20151015Moore Advanced Calculations in Calc Manager  OW 20151015
Moore Advanced Calculations in Calc Manager OW 20151015
 
Ssis sql ssrs_ssas_sp_mdx_hb_li
Ssis sql ssrs_ssas_sp_mdx_hb_liSsis sql ssrs_ssas_sp_mdx_hb_li
Ssis sql ssrs_ssas_sp_mdx_hb_li
 
Tutorial olap4j
Tutorial olap4jTutorial olap4j
Tutorial olap4j
 
SSRS for DBA's
SSRS for DBA'sSSRS for DBA's
SSRS for DBA's
 
Big Data MDX with Mondrian and Apache Kylin
Big Data MDX with Mondrian and Apache KylinBig Data MDX with Mondrian and Apache Kylin
Big Data MDX with Mondrian and Apache Kylin
 
Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010
 
SSAS and MDX
SSAS and MDXSSAS and MDX
SSAS and MDX
 

Similar a MDX - What BI Developers Need To Know

R graphics260809
R graphics260809R graphics260809
R graphics260809
lizbethfdz
 
Python simplecv
Python simplecvPython simplecv
Python simplecv
UFPA
 
AutoCAD - 3D Notes
AutoCAD - 3D NotesAutoCAD - 3D Notes
AutoCAD - 3D Notes
Vj NiroSh
 
Calculation Groups - color 1 slide per page.pdf
Calculation Groups - color 1 slide per page.pdfCalculation Groups - color 1 slide per page.pdf
Calculation Groups - color 1 slide per page.pdf
PBIMINERADC
 
DMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure WorksDMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure Works
Johannes Hoppe
 

Similar a MDX - What BI Developers Need To Know (20)

Einführung in mdx
Einführung in mdxEinführung in mdx
Einführung in mdx
 
Mdx basics
Mdx basicsMdx basics
Mdx basics
 
R graphics260809
R graphics260809R graphics260809
R graphics260809
 
3D Design with OpenSCAD
3D Design with OpenSCAD3D Design with OpenSCAD
3D Design with OpenSCAD
 
Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction
Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction
Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction
 
Python simplecv
Python simplecvPython simplecv
Python simplecv
 
Oracle SQL Model Clause
Oracle SQL Model ClauseOracle SQL Model Clause
Oracle SQL Model Clause
 
340project Final
340project Final340project Final
340project Final
 
3 d autocad
3 d autocad3 d autocad
3 d autocad
 
AutoCAD - 3D Notes
AutoCAD - 3D NotesAutoCAD - 3D Notes
AutoCAD - 3D Notes
 
3D_AutoCAD.pdf
3D_AutoCAD.pdf3D_AutoCAD.pdf
3D_AutoCAD.pdf
 
3 d autocad
3 d autocad3 d autocad
3 d autocad
 
TUTORIAL AUTO CAD 3D
TUTORIAL AUTO CAD 3DTUTORIAL AUTO CAD 3D
TUTORIAL AUTO CAD 3D
 
Calculation Groups - color 1 slide per page.pdf
Calculation Groups - color 1 slide per page.pdfCalculation Groups - color 1 slide per page.pdf
Calculation Groups - color 1 slide per page.pdf
 
DMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure WorksDMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure Works
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
 
VDC
VDC VDC
VDC
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
 
It ready dw_day4_rev00
It ready dw_day4_rev00It ready dw_day4_rev00
It ready dw_day4_rev00
 

Más de Mark Ginnebaugh

Más de Mark Ginnebaugh (20)

Automating Microsoft Power BI Creations 2015
Automating Microsoft Power BI Creations 2015Automating Microsoft Power BI Creations 2015
Automating Microsoft Power BI Creations 2015
 
Platfora - An Analytics Sandbox In A World Of Big Data
Platfora - An Analytics Sandbox In A World Of Big DataPlatfora - An Analytics Sandbox In A World Of Big Data
Platfora - An Analytics Sandbox In A World Of Big Data
 
Microsoft SQL Server Relational Databases and Primary Keys
Microsoft SQL Server Relational Databases and Primary KeysMicrosoft SQL Server Relational Databases and Primary Keys
Microsoft SQL Server Relational Databases and Primary Keys
 
DesignMind Microsoft Business Intelligence SQL Server
DesignMind Microsoft Business Intelligence SQL ServerDesignMind Microsoft Business Intelligence SQL Server
DesignMind Microsoft Business Intelligence SQL Server
 
San Francisco Bay Area SQL Server July 2013 meetings
San Francisco Bay Area SQL Server July 2013 meetingsSan Francisco Bay Area SQL Server July 2013 meetings
San Francisco Bay Area SQL Server July 2013 meetings
 
Silicon Valley SQL Server User Group June 2013
Silicon Valley SQL Server User Group June 2013Silicon Valley SQL Server User Group June 2013
Silicon Valley SQL Server User Group June 2013
 
Microsoft SQL Server Continuous Integration
Microsoft SQL Server Continuous IntegrationMicrosoft SQL Server Continuous Integration
Microsoft SQL Server Continuous Integration
 
Hortonworks Big Data & Hadoop
Hortonworks Big Data & HadoopHortonworks Big Data & Hadoop
Hortonworks Big Data & Hadoop
 
Microsoft SQL Server Physical Join Operators
Microsoft SQL Server Physical Join OperatorsMicrosoft SQL Server Physical Join Operators
Microsoft SQL Server Physical Join Operators
 
Microsoft PowerPivot & Power View in Excel 2013
Microsoft PowerPivot & Power View in Excel 2013Microsoft PowerPivot & Power View in Excel 2013
Microsoft PowerPivot & Power View in Excel 2013
 
Microsoft Data Warehouse Business Intelligence Lifecycle - The Kimball Approach
Microsoft Data Warehouse Business Intelligence Lifecycle - The Kimball ApproachMicrosoft Data Warehouse Business Intelligence Lifecycle - The Kimball Approach
Microsoft Data Warehouse Business Intelligence Lifecycle - The Kimball Approach
 
Fusion-io Memory Flash for Microsoft SQL Server 2012
Fusion-io Memory Flash for Microsoft SQL Server 2012Fusion-io Memory Flash for Microsoft SQL Server 2012
Fusion-io Memory Flash for Microsoft SQL Server 2012
 
Microsoft Data Mining 2012
Microsoft Data Mining 2012Microsoft Data Mining 2012
Microsoft Data Mining 2012
 
Microsoft SQL Server PASS News August 2012
Microsoft SQL Server PASS News August 2012Microsoft SQL Server PASS News August 2012
Microsoft SQL Server PASS News August 2012
 
Business Intelligence Dashboard Design Best Practices
Business Intelligence Dashboard Design Best PracticesBusiness Intelligence Dashboard Design Best Practices
Business Intelligence Dashboard Design Best Practices
 
Microsoft Mobile Business Intelligence
Microsoft Mobile Business Intelligence Microsoft Mobile Business Intelligence
Microsoft Mobile Business Intelligence
 
Microsoft SQL Server 2012 Cloud Ready
Microsoft SQL Server 2012 Cloud ReadyMicrosoft SQL Server 2012 Cloud Ready
Microsoft SQL Server 2012 Cloud Ready
 
Microsoft SQL Server 2012 Master Data Services
Microsoft SQL Server 2012 Master Data ServicesMicrosoft SQL Server 2012 Master Data Services
Microsoft SQL Server 2012 Master Data Services
 
Microsoft SQL Server PowerPivot
Microsoft SQL Server PowerPivotMicrosoft SQL Server PowerPivot
Microsoft SQL Server PowerPivot
 
Microsoft SQL Server Testing Frameworks
Microsoft SQL Server Testing FrameworksMicrosoft SQL Server Testing Frameworks
Microsoft SQL Server Testing Frameworks
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

MDX - What BI Developers Need To Know

  • 1. What All Microsoft BI Developers Need to Know About MDX Speaker: Nathan Peterson Solid Quality Mentors Silicon Valley SQL Server User Group January 19, 2009 Mark Ginnebaugh, User Group Leader, mark@designmind.com
  • 2. Nathan Peterson Solid Quality Mentors Currently working on an Analysis Services project for the US Army Lead developer of a local cube generating utility called CubeSlice
  • 3. What do all developers need to know about MDX? You should be able to: Create and debug Calculated Members. Create and debug Named Sets. What you need to know to accomplish this: Understand MDX Concepts. Learn MDX Syntax. Learn some MDX functions. Learn some practical applications of MDX.
  • 4. What Is MDX? Multi-Dimensional eXpressions SQL is used for relational queries, while MDX is used for multidimensional queries. Calculated members allow you to incorporate multidimensional logic into your cube. Named sets allow you to pre-define dynamic groups of members to be displayed on columns or rows.
  • 5. Sample calculated member with member measures.[Last Year Sales Amount] as ( measures.[Sales Amount], ParallelPeriod ( [Date].[Calendar].[Calendar Year], 1, [Date].[Calendar].CurrentMember ) )
  • 6. Sample named set with set [Top 5 Products] as topcount ( [Product].[Product].Members, 5, Measures.[Internet Sales Amount] )
  • 7. Comparing MDX to SQL From SQL selects from a table. MDX selects from a cube. Dimensionality SQL returns one or more columns and 0 or more rows. MDX can return data for 0, 1, 2, 3 or more axes. Where SQL has a filter. MDX has a slicer.
  • 8. MDX query with two axes and two slicers select Measures.AllMembers on columns, [Date].[Calendar].&[2003].Children on rows from [Adventure Works] where ( [Customer].[Customer Geography].[France], [Product].[Product Categories].[Bikes] )
  • 10. Same query, using axis numbers instead of axis names select Measures.AllMembers on 0, [Date].[Calendar].&[2003].Children on 1 from [Adventure Works] where ( [Customer].[Customer Geography].[France], [Product].[Product Categories].[Bikes] )
  • 11. Using MDX requires multidimensional thinking In a cube Dimensions have hierarchies Hierarchies have levels Levels have members In a cellset Each axis contains a set Each set consists of a group of members or a group of tuples Tuples have members from different hierarchies
  • 12. Multidimensional thinking – Where are you? Every member is in a level. Every level is in a hierarchy. Every hierarchy is a part of a dimension. For every cell of the cellset, there’s (almost) always a Current Member for each hierarchy. The Current Member may be set from the column, the row, the slicer, or the default member.
  • 13. Create a set by listing members The simplest set is a list of members. The boundaries of the set are indicated with curly braces {}. select { [Product].[Category].[All Products], [Product].[Category].[Accessories], [Product].[Category].[Bikes], [Product].[Category].[Clothing], [Product].[Category].[Components] } on 0 from [Adventure Works]
  • 14. Create a set with a function There are many MDX functions that create sets. This query uses the .Members function to return all the members from a hierarchy – and gives the same result as the previous query. You don’t have to use {} when you create a set with a function. select [Product].[Category].Members on 0 from [Adventure Works]
  • 15. Tuples Each cell in a cellset shows data for one particular member from each hierarchy. A tuple is a way of specifying which member should be used from each of the hierarchies. Tuples are written like points in a geometric grid: (6,3) identifies point 6 on the x-axis and point 3 on the y-axis. ([2009],[US],[Bikes],[Internet Sales Amount]) identifies Internet Sales for Bikes in the US in 2009.
  • 16. 2 Dimensional Tuple 6 5 2 Dimensional Tuple 4 3 2 (6, 2) 1 1 2 3 4 5 6 7 8
  • 17. Create a set by listing tuples select { ( [Date].[Calendar Year].&[2002], [Product].[Category].[Accessories] ), ( [Date].[Calendar Year].&[2002], [Product].[Category].[Bikes] ), ( [Date].[Calendar Year].&[2003], [Product].[Category].[Accessories] ), ( [Date].[Calendar Year].&[2003], [Product].[Category].[Bikes] ) } on 0 from [Adventure Works]
  • 18. Listing Tuples [Accessories] [Clothing] 2 Dimensional Tuple [Touring Bikes] [Road Bikes] ([2006], [Mountain Bikes]) [Mountain Bikes] [All Products] 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
  • 19. Create a set of tuples by multiplying two sets of members select { [Date].[Calendar Year].&[2002], [Date].[Calendar Year].&[2003] } * { [Product].[Category].[Accessories], [Product].[Category].[Bikes] } on 0 from [Adventure Works]
  • 20. 3 Dimensional Tuple [Accessories] [Clothing] 3 Dimensional Tuple Difficult to Show in 3 Axes – [Touring Bikes] Where to place the point? [Road Bikes] ([2006], [Mountain Bikes], [Red]) [Mountain Bikes] [All Products] [Red] [Blue] 2 2 2 2 2 2 2 2 [Pink] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
  • 21. 3 Dimensional Tuple Combining 2 Hierarchies on 1 Axis ([Blue], [Road Bikes]) ([Red], [Road Bikes]) 3 Dimensional Tuple ([Blue], [Mountain Bikes]) ([Red], [Mountain Bikes]) ([Blue], [All Products]) ([2006], [Mountain Bikes], [Red]) ([Red], [All Products]) 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
  • 22. The Where clause The Where clause is an optional part of an MDX query. The Where clause is a slicer, not a filter. You can’t use the Where clause for limiting the value to a threshold (Sales over $10,000). Use the Filter function for filtering. The Where clause often looks like a tuple and actually is a tuple – but it doesn’t have to be.
  • 23. Where clause using a tuple – one member from each referenced hierarchy select Measures.[Internet Sales Amount] on 0 from [Adventure Works] where ( [Customer].[Customer Geography].[France], [Product].[Product Categories].[Bikes] )
  • 24. Where clause with slicing on multiple members from one hierarchy select Measures.[Internet Sales Amount] on 0 from [Adventure Works] where ( [Customer].[Customer Geography].[France], { [Product].[Product Categories].[Bikes], [Product].[Product Categories].[Clothing] } )
  • 25. The With clause The With clause is used for creating both calculated members and named sets. These calculated members and named sets are only valid for the life of the query. You can also create calculated members and named sets that are persistent: Lasting for the life of a session. Created permanently in the cube.
  • 26. Creating calculated member in the With clause with member Measures.[Average Internet Sales] as [Measures].[Internet Sales Amount] / [Measures].[Internet Order Count], format_string = "currency" select {Measures.[Average Internet Sales]} on 0 from [Adventure Works]
  • 27. Members, Sets, Tuples ([Blue], [Road Bikes]) ([Red], [Road Bikes]) Members, Sets, Tuples ([Blue], [Mountain Bikes]) Set of (Partial) ([Red], [Mountain Bikes]) Tuples ([Blue], [All Products]) ([2006], [Mountain Bikes], [Red]) ([Red], [All Products]) (Partial) Tuple Member 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 Set of Members
  • 28. Creating named set in the With clause with set [Products - High Selling on Internet] as filter ( [Product].[Product].[Product].Members, [Measures].[Internet Sales Amount] > 50000 ) select {Measures.[Internet Sales Amount]} on 0, [Products - High Selling on Internet] on 1 from [Adventure Works]
  • 29. Full Tuple ([Blue], [Road Bikes]) ([2006], [Mountain Bikes], [Red], [All ([Red], [Road Bikes]) Promotions], [All Gender], [Measures].[Reseller Sales Amount], ([Blue], [Mountain Bikes]) etc…) ([Red], [Mountain Bikes]) ([Blue], [All Products]) ([Red], [All Products]) 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
  • 30. MDX Punctuation Use {} for sets. Use () for tuples. Use [] for names of cubes, dimensions, hierarchies, levels, and members. Use commas all over the place (except not between multiple calculated members or named sets defined in the same query).
  • 31. Where to write your MDX In a client application like PerformancePoint, which allows you to write code for calculated measures and sets. In a query editor like the SQL Server Management Studio. In the MDX Script of a cube. TIP: It’s often easier to write the code in a query editor first and then put it into the MDX Script.
  • 32. Writing MDX for a KPI You can write MDX to create the values for a KPI, whether you’re working with Analysis Services KPIs or PerformancePoint Services KPIs. For both tools, it’s easier to create calculated members in the MDX script for the values first. TIP: When building KPIs in the BIDS, use a template for assigning the Status and the Trend.
  • 33. Functions – Navigating within a level PrevMember NextMember Lag() Lead()
  • 34. Examples of navigating in a level Navigation functions are often used with Dates. Note: PrevMember, Lag(1), and Lead(-1) all return the same member. [1997].PrevMember [1996] [March 2010].NextMember [April 2010] [10 April 2009].Lag(2) [8 April 2009] [December 2009].Lead(12) [December 2010]
  • 35. Functions – All in the Family Parent Children, FirstChild, LastChild Descendants Ancestor, Ascendants Siblings, FirstSibling, LastSibling Cousin
  • 36. Examples of family functions Some of the family functions return individual members, while other return sets. If a function returns an individual member, you can use another function with it. [Mar 1997].Parent [1997] [Q1 2009].Children [Jan 09]:[Mar 09] [2009].FirstChild [Q1 2009] Descendants([2009], 2) [Jan 09]:[Dec 09] Descendants([2009],[Date])[1Jan09]:[31Dec09] Ancestor([Mar 2008], 3) [2008] FirstSibling([Mar 2007]) [Jan 2007]
  • 37. Functions – Multiplying, adding, subtracting, intersecting sets Set multiplication, addition, and subtractions can be indicated with mathematical symbols or with functions. Multiplication * Crossjoin Addition + Union Subtraction - Except Intersection Intersect
  • 38. Example of set multiplication These two sets are equivalent. [Date].[Fiscal].[Month].members * [Product].[Product].members Crossjoin ( [Date].[Fiscal].[Month].members, [Product].[Product].members )
  • 39. Practical Applications of MDX Comparing with a previous time period. Comparing with the same time period in the previous year. Year-To-Date. Rolling Average. Percent contribution.
  • 40. Comparing with a previous time period with member Measures.[Last Month Sales] as ( Measures.[Internet Sales Amount], [Date].[Calendar].PrevMember ) select { Measures.[Internet Sales Amount], Measures.[Last Month Sales] } on 0, [Date].[Calendar].[Month].members on 1 from [Adventure Works]
  • 41. Comparing with the same period in the previous year with member measures.[Last Year Sales Amount] as ( measures.[Sales Amount], ParallelPeriod ( [Date].[Calendar].[Calendar Year], 1, [Date].[Calendar].CurrentMember ) )
  • 42. Year-To- Year-To-Date with member measures.[YTD Sales] as sum ( ytd([Date].[Calendar].CurrentMember), Measures.[Internet Sales Amount] ) select { Measures.[Internet Sales Amount], Measures.[YTD Sales] } on 0, [Date].[Calendar].[Month].members on 1 from [Adventure Works]
  • 43. Rolling Average With member measures.[Previous 6 Month Rolling AVG] as sum ( [Date].[Calendar].lag(6): [Date].[Calendar].lag(1), Measures.[Internet Sales Amount] ) / 6 select { Measures.[Internet Sales Amount], Measures.[Previous 6 Month Rolling AVG] } on 0, [Date].[Calendar].[Month].members on 1 from [Adventure Works]
  • 44. Percent Contribution With member Measures.[Percent of Parent Sales] as Measures.[Internet Sales Amount] / ( Measures.[Internet Sales Amount], [Product].[Product Categories].Parent ) select { Measures.[Internet Sales Amount], Measures.[Percent of Parent Sales] } on 0, [Product].[Product Categories]. members on 1 from [Adventure Works]
  • 45. Additional topics – if there’s time Dealing with exceptions String manipulation Generate Recursion
  • 46. Dealing with exceptions A calculation needs to be valid everywhere with member [Measures].[% Change # of Customers] as case when [Date].[Fiscal].CurrentMember.Level.Ordinal = 0 then "NA” when [Date].[Fiscal].CurrentMember.Level.Ordinal = 5 then Null when isempty ( ([Date].[Fiscal].CurrentMember.PrevMember, [Measures].[Customer Count]) ) then null else ( ([Date].[Fiscal].CurrentMember, [Measures].[Customer Count]) - ([Date].[Fiscal].PrevMember, [Measures].[Customer Count]) ) / ([Date].[Fiscal].PrevMember,[Measures].[Customer Count]) end
  • 47. Functions - String manipulation Turning the current date into a member reference Strtomember ( "[Date].[Fiscal].[Date].&[" + cstr(year(now()) - 6) + right("0" + cstr(month(now())), 2) + right("0" + cstr(day(now())), 2) + "]" )
  • 48. The Generate function What are the five top products most purchased by people living in my top five cities? generate ( topcount ( [Customer].[Customer Geography].[City].members, 5, [Measures].[Internet Sales Amount] ), crossjoin ( [Customer].[Customer Geography].CurrentMember, topcount ( [Product].[Product].[Product].members, 5, [Measures].[Internet Sales Amount] ) ) )
  • 49. Recursion Finding the most recent purchase with member measures.[Most Recent Purchase] as iif ( [Measures].[Internet Sales Amount] > 0, [Measures].[Internet Sales Amount], [Date].[Calendar].PrevMember )
  • 51. To learn more or inquire about speaking opportunities, please contact: Mark Ginnebaugh, User Group Leader mark@designmind.com