Professional Documents
Culture Documents
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
SQLselects from a table.
MDX selects from a cube.
Dimensionality
SQLreturns one or more columns and 0 or more rows.
MDX can return data for 0, 1, 2, 3 or more axes.
Where
SQLhas 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
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?
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
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
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
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
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
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
PrevMember
NextMember
Lag()
Lead()
Examples of navigating in a level
Parent
Children, FirstChild, LastChild
Descendants
Ancestor, Ascendants
Siblings, FirstSibling, LastSibling
Cousin
Examples of family functions
Multiplication * Crossjoin
Addition + Union
Subtraction - Except
Intersection Intersect
Example of set multiplication
[Date].[Fiscal].[Month].members
*
[Product].[Product].members
Crossjoin
(
[Date].[Fiscal].[Month].members,
[Product].[Product].members
)
Practical Applications of MDX
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
Strtomember
(
"[Date].[Fiscal].[Date].&["
+ cstr(year(now()) - 6)
+ right("0" + cstr(month(now())), 2)
+ right("0" + cstr(day(now())), 2)
+ "]"
)
The Generate function
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: