Business Intelligence Fundamentals
Objectives
In this session, you will learn to:
Create MDX queries
Use MDX functions for analysis
Slide 1 of 27
Ver 1.0
Business Intelligence Fundamentals
Difference Between SQL and MDX
The MDX query language is similar to Structured Query
Language (SQL).
It enables you to query multidimensional objects, such as
OLAP cubes.
The following table shows some of the common differences
between SQL and MDX.
SQL Queries
MDX Queries
Work with two dimensions
Use the SELECT statement to define
the column layout and the WHERE
clause to define the row layout
Work with multiple dimensions
Use the SELECT clause to define
multiple axes and the WHERE clause to
restrict the data to a specific dimension
or measure
Use the WHERE clause to identify a
portion of data from a different dimension
Use the WHERE clause to filter data
Return a subset of two-dimensional
data from tables
Populate a predefined
two-dimensional rowset if they are
created on one or more tables
Access the SQL Server database that
can be connected from client
applications
Return a subset of multidimensional data
from cubes
Populate a multidimensional dataset if
they are created on a single cube
Access the OLAP cube data that can be
connected with the cube browser using
ADOMD and OLE DB for OLAP
Slide 2 of 27
Ver 1.0
Business Intelligence Fundamentals
Difference Between SQL and MDX (Contd.)
The following figure shows the difference in connectivity
between the SQL Server database and the OLAP cube.
Slide 3 of 27
Ver 1.0
Business Intelligence Fundamentals
Key Concept in MDX
An OLAP cube consists of dimensions and measures.
In the figure below, there are two dimensions: Time and
Geography. The Geography dimension has five members: Africa,
Asia, Australia/Pacific, Europe, and North America. There is
one measure, QuantitySold, that is the number of items sold in
each continent by year.
QuantitySold
Time
Geography
Africa
Asia
Australia/Pacific
Europe
North America
4
1998
1999
2000
2001
2002
10
1429
1188
1242
1162
2133
23654
27184
31359
26476
31069
6726
8484
11032
8616
8343
Slide 4 of 27
Ver 1.0
Business Intelligence Fundamentals
Key Concepts in MDX (Contd.)
A cell is the summarized values contained in the intersection of two
or more levels.
In a two-dimensional view of a cube, each cell can be described in
terms of one member from each dimension. For example, the
number of items sold in North America in 2002 was 8,343.
QuantitySold
Time
Geography
Africa
Asia
Australia/Pacific
Europe
5
North America
1998
1999
2000
2001
2002
10
1429
1188
1242
1162
2133
23654 27184 31359 26476 31069
6726
8484 11032
Slide 5 of 27
8616
8343
Ver 1.0
Business Intelligence Fundamentals
Key Concepts in MDX (Contd.)
Dimensions in a cube are hierarchical. Values are derived by adding
or aggregating the original data in the cube to produce a
presummarized report.
QuantitySold
Africa Asia Australia Europe North
/
America
Pacific
2002 Q1
358
4772
1261
Q2
392
8564
2307
Q3
380
9811
2653
Oct
182
1395
412
Nov
257
1865
556
Dec
564
4663
1154
Q4 Total
1003
7922
2122
Total
27
2 Slide 6 of 2133
31069
8343
Q4
2002
Ver 1.0
Business Intelligence Fundamentals
Tuples
To identify and extract data, MDX uses a reference system named
tuples. Tuples list dimensions and members to identify individual
cells, as well as a larger section of cells in the cube. Because each
cell is an intersection of all the dimensions of the cube, tuples
uniquely
identify
every
Product
(Jun 2001, United States, Clothes)
cell in
Dimension
the cube.
Geography
Dimension
Clothes
United
States
Time Dimension
7
Jun 2001
Slide 7 of 27
Ver 1.0
Business Intelligence Fundamentals
Tuples (Contd.)
Tuples that encompass more than one cell are named slices.
1st Dimension:
Geography
Continent
Country
Region
Geography
Geography==Europe
Europe
Product
Product==All
AllProducts
Products
Time
Time==All
AllTime
Time
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Cell
Slide 8 of 27
2nd Dimension:
Product
Line
Category
Group
3rd Dimension:
Time
Year
Quarter
Month
Ver 1.0
Business Intelligence Fundamentals
Naming Conventions
When writing MDX code, you need a way to identify the specific
members with which you want the code to work. You can identify a
member by working downwards from the dimension name and
specifying the members at each level until you reach the required
member. For example,
[Geography].[All Geo].[Africa].[Benin]
is used to indicate the Benin member.
All
Continent
All Geo
North
America
Africa
Country Benin
Morocco
Slide 9 of 27
Canada
United
States
Ver 1.0
Business Intelligence Fundamentals
MDX Syntax
[ ] The square brackets encircle cube names,
dimension names, hierarchy names, and member
names.
For example:
[Geography].[All Geo].[Africa].
[Benin]
.
10
The dot separates the member names and the
dimension names.
Slide 10 of 27
Ver 1.0
Business Intelligence Fundamentals
Sets
A set is a collection of tuples that were defined using the same
dimension. For example:
([Geography].[Benin], [Time].[2001].[Q1], [Product].[Clothes])
([Geography].[Canada], [Time].[2001].[Q1], [Product].[Clothes])
11
Both have exactly one member from the Geography, Time, and Product
dimensions. Therefore, these two tuples form a set when taken together.
Slide 11 of 27
Ver 1.0
Business Intelligence Fundamentals
MDX Syntax
( ) Parentheses encircle tuples. For example:
([Geography].[All Geo].[Africa].[Benin])
and
([Geography].[All Geo].[North America].[Canada])
{ } Curly brackets encircle sets. For example:
{([Geography].[All Geo].[Africa].[Benin]),
([Geography].[All Geo].[North America].[Canada])
}
12
The comma separates members within tuples and separates
tuples within sets.
The colon defines a range. For example:
{ [Time].[2001].[Q1]:[Time].[2001].[Q4] }
Slide 12 of 27
Ver 1.0
Business Intelligence Fundamentals
Axis and Slicer Dimensions
When you formulate an MDX query, an
Axis
determine the
edges at
of athe
multidimensional
application typically
looks
cubes and
dimensions
result set. Data
divides the set
of dimensions
into two subsets:
is retrieved
for multiple members.
Slicer
dimensions
13
filter multidimensional data. Data
is retrieved for a single member.
Slide 13 of 27
Ver 1.0
Business Intelligence Fundamentals
Slicers
14
Implicit
Slicers
are dimensions not explicitly assigned
to an axis. It is assumed that you want the
default hierarchy's default member for any
dimension that remains as an implicit slicer.
Explicit
Slicers
are derived by using the WHERE clause to
restrict multidimensional data, which limits the
data returned by a query.
Slide 14 of 27
Ver 1.0
Business Intelligence Fundamentals
Basic MDX Query Structure
The components that an MDX query needs in order
to extract the requested information are as follows:
a component to specify the column headers
a component to specify the row headers
a pointer to the cube being accessed
General form of the SELECT statement:
SELECT
SELECT
{set
{setdefining
definingthe
thecolumn
columnheaders}
headers} ON
ONCOLUMNS,
COLUMNS,
{set
{setdefining
definingthe
therow
rowheaders}
headers} ON
ONROWS
ROWS
FROM
FROM [cube
[cubename]
name]
15
Slide 15 of 27
Ver 1.0
Business Intelligence Fundamentals
Basic MDX Query Structure (Contd.)
SELECT
For
example, the following MDX query:
{ [Time].[YQM].[All YQM] } ON COLUMNS,
{ [Geography].[All Geography] } ON ROWS
FROM
[OLAP_GeoTime]
produces a table similar to the table shown
below:
16
Slide 16 of 27
Ver 1.0
Business Intelligence Fundamentals
Basic MDX Query Structure
(Contd.)
The WHERE clause is used to restrict the returned data to specific
dimension and member criteria. This is referred to as a slicer dimension. A
slicer dimension is expected to return data for a single member.
General form of the query with the addition of the WHERE clause:
SELECT
SELECT
{set
{setdefining
definingthe
thecolumn
column headers}
headers} ON
ONCOLUMNS,
COLUMNS,
{set
{setdefining
definingthe
therow
rowheaders}
headers} ON
ONROWS
ROWS
FROM
FROM[cube
[cubename]
name]
WHERE
WHERE(slicer)
(slicer)
17
Slide 17 of 27
Ver 1.0
Business Intelligence Fundamentals
Basic MDX Query Structure
(Contd.)
The following MDX query uses the WHERE clause to select the
measure:
SELECT
{ [Time].[YQM].[All YQM] } ON COLUMNS ,
{ [Geography].[All Geography] } ON ROWS
FROM
[OLAP_GeoTime]
WHERE
([Measures].[QuantitySUM])
18
and produces a table similar to the table shown below:
Slide 18 of 27
Ver 1.0
Business Intelligence Fundamentals
19
Named Sets and Calculated
Measures
You can use MDX to perform the following tasks:
specify a name for set expressions (named sets)
define formulas and treat each formula as a new member
Slide 19 of 27
Ver 1.0
Business Intelligence Fundamentals
Named Sets
20
A set can be a lengthy and complex declaration, and difficult to
follow or understand. A named set is an alias for a defined set. The
use of named sets helps to accomplish the following tasks:
clarify the logic by using an alias to simplify
the MDX code
potentially increase the efficiency of query execution because
the set is only executed once at the beginning, stored, and then
reused each time that it
is encountered
increase the ease of maintenance
Slide 20 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating Named Sets for a Query
Use the WITH keyword as part of the SELECT statement to create a
named set as part of an MDX query.
General form for creating a named set using the WITH keyword:
WITH
WITHSET
SETset_name
set_nameAS
AS'set'
'set'
SELECT
SELECT
{set
{setdefining
definingthe
thecolumn
columnheaders}
headers} ON
ONCOLUMNS,
COLUMNS,
{set
{setdefining
definingthe
therow
rowheaders}
headers} ON
ONROWS
ROWS
FROM
FROM[cube
[cubename]
name]
WHERE
WHERE(slicer)
(slicer)
21
Slide 21 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating Named Sets for a Query
The following MDX query uses the WITH keyword to create the
[North America] named set in the current query:
WITH SET [OLAP_CustProd].[North America] AS
'{[Demographic].[All CntryAgeGender].[Canada],
[Demographic].[All CntryAgeGender].[Canada].Children,
[Demographic].[All CntryAgeGender].[United States],
[Demographic].[All CntryAgeGender].[United States].Children
}'
SELECT
{ [Product].[All Product] } ON COLUMNS,
{ [OLAP_CustProd].[North America] } ON ROWS
FROM
[OLAP_CustProd]
22
Slide 22 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating Named Sets for a Session
The CREATE SET statement creates a named set that exists for the
lifetime of the current SAS OLAP Server session. For example, the
following code creates the
[North America] named set:
CREATE SET [OLAP_CustProd].[North America] AS
'{[Demographic].[All CntryAgeGender].[Canada],
[Demographic].[All CntryAgeGender].[Canada].Children,
[Demographic].[All CntryAgeGender].[United States],
[Demographic].[All CntryAgeGender].[United States].Children
}'
The [North America]
named set is available
for use in future queries
in the current session.
23
Slide 23 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating Named Sets Permanently
The CREATE GLOBAL SET statement creates a named set that
exists in the current and future SAS OLAP Server sessions. For
example, the following code creates the [North America] named set:
CREATE GLOBAL SET [OLAP_CustProd].[North America] AS
'{[Demographic].[All CntryAgeGender].[Canada],
[Demographic].[All CntryAgeGender].[Canada].Children,
[Demographic].[All CntryAgeGender].[United States],
[Demographic].[All CntryAgeGender].[United States].Children
}'
The [North America]
named set is available
for use in future queries
in the current session
and beyond.
24
Slide 24 of 27
Ver 1.0
Business Intelligence Fundamentals
Calculated Members
25
Calculated members have the following characteristics:
based on evaluated expressions in MDX
computed at run time
based on data that already exists
in the cube
Slide 25 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating a Calculated Member for a Query
Similar to the way it is used in named sets, the WITH keyword is used to
describe calculated members.
General form for creating a calculated member using
the WITH keyword:
WITH
WITHMEMBER
MEMBER member-name
member-nameAS
AS 'value-expression'
'value-expression'
SELECT
SELECT
{set
{set defining
definingthe
thecolumn
columnheaders}
headers}ON
ONCOLUMNS,
COLUMNS,
{set
{set defining
definingthe
therow
rowheaders}
headers}ON
ONROWS
ROWS
FROM
FROM [cube
[cubename]
name]
WHERE
WHERE(slicer)
(slicer)
26
Slide 26 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating a Calculated Member for
a Query (Contd.)
The following MDX query uses the WITH
keyword
WITH MEMBER [Measures].[QuantityYTD] AS
'(toSUM(YTD(
), QuantityYTD
Measures.[QuantitySUM])
create the
calculated)'measure
in
SELECT
CROSSJOIN
(
the current
query:
{[Time].[All Time].[1998].Children},
{ [Measures].[QuantitySUM],[Measures].[QuantityYTD]}
) ON COLUMNS ,
{[Product].[All Product].[Clothes & Shoes].Children}
ON ROWS
FROM
[OLAP_PTCStar]
27
Slide 27 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating a Calculated Member for
a Session
The CREATE MEMBER statement creates a calculated member
that exists for the lifetime of the current SAS OLAP Server session.
For example, the following code creates the QuantityYTD member:
CREATE MEMBER
[OLAP_PTCStar].[Measures].[QuantityYTD]
AS '( SUM(YTD( ), Measures.[QuantitySUM]) )'
28
Slide 28 of 27
Ver 1.0
Business Intelligence Fundamentals
Creating a Calculated Member
Permanently
The CREATE GLOBAL MEMBER statement creates a calculated
member that exists in the current and future SAS OLAP Server
sessions. For example, the following code creates the QuantityYTD
member:
CREATE GLOBAL MEMBER
[OLAP_PTCStar].[Measures].[QuantityYTD]
AS '( SUM(YTD( ), Measures.[QuantitySUM]) )'
29
Slide 29 of 27
Ver 1.0