Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Reading OLAP Cube using MDX: Part 2

4.61/5 (6 votes)
1 Aug 2014CPOL4 min read 21.2K   217  
Reading OLAP Cube using MDX: Part 2

Sample Image - maximum width is 600 pixels

Introduction

This article contains calculation process used in MDX. I have already covered basic MDX query, Filter process in MDX query and different Slicing feature in MDX query in my previous article Reading OLAP cube using MDX query Part 1. Calculation process in MDX includes arithmetical and logical values process with existing measures values, specify range based calculation with measure value and date wise calculation with measure value.

Background

OLAP cube contains multiple different measures. These measures can be queried through different available dimensions. Dimensions available in OLAP cube are collection of attributes. OLAP cube contains definition of attribute relationship of dimension. This article will cover calculation based on attribute relationship available in dimension. If you require basics of MDX query, please do read my article Reading OLAP cube using MDX query Part 1. I personally assume, strength in MDX query makes you more comfortable to work on OLAP cube. More different way, you will write MDX query more comfortable you will be on OLAP cube. Writing complex OLAP cube helps you to prepare business solution easily.

Calculation in MDX Query

After writing basic MDX query and getting familiar with syntax of MDX, we can start for complex MDX query. Till now, we have used existing measures and dimensions of OLAP cube in MDX query. We may require some time calculated measure or calculated dimension for our business process. This can be achieved through With Member clause in MDX.

With Member clause allow to create scope member in MDX

SQL
With member [Measures].[My Member] as "Datawarehouse Consultant"
Select 
{
[measures].[My Member] 
} ON Columns
From [Adventure works]

The above MDX query will create scope member [My Member] in current scope.

Create alias of existing member using With Member

SQL
With 
Member [measures].[customer sales] as [measures].[internet sales amount]
Member [measures].[retailer sales] as [measures].[reseller sales amount]
Select 
{
[measures].[customer sales],
[measures].[retailer sales]
} on columns,
{
[date].[calendar].[calendar year]
} on rows
from [adventure works]

The above query creates alias [Customer Sales] and [Retailer Sales] for existing member. This step actually helps when required member is available in multiple level of measure group. Alias helps to access this long name with some small name.

Calculation using With Member

SQL
With 
Member [measures].[customer sales] as [measures].[internet sales amount]
Member [measures].[retailer sales] as [measures].[reseller sales amount]
Member [measures].[Total Sales] as [measures].[internet sales amount]+ [measures].[reseller sales amount]
Select 
{
[measures].[customer sales],
[measures].[retailer sales],
[measures].[total sales]
} on columns,
{
[date].[calendar].[calendar year]
} on rows
from [adventure works]

In the above MDX query example, [Total sales] is a new member calculated using With Member clause.

Formatting of existing member using With Member

SQL
With 
Member [measures].[customer sales] as [measures].[internet sales amount],format_string="#,###.00€"
Member [measures].[retailer sales] as [measures].[reseller sales amount],format_string="#,###.00€"
Member [measures].[Total Sales] as [measures].[internet sales amount]+ _
    [measures].[reseller sales amount],format_string="#,###.00€"
Select 
{
[measures].[customer sales],
[measures].[retailer sales],
[measures].[total sales]
} on columns,
{
[date].[calendar].[calendar year]
} on rows
from [adventure works]

In the above example, all sales figure will display in format.

Named set create using With Set clause

Long MDX expression can alias with Set name using With Set clause.

SQL
With 
Set [Not 2008] as [Date].[Calendar].[Calendar Year].[CY 2001] : [Date].[Calendar].[Calendar Year].[CY 2007]
Member [measures].[customer sales] as [measures].[internet sales amount],format_string="#,###.00€"
Member [measures].[retailer sales] as [measures].[reseller sales amount],format_string="#,###.00€"
Member [measures].[Total Sales] as [measures].[internet sales amount]+ _
    [measures].[reseller sales amount],format_string="#,###.00€"
Select 
{
[measures].[customer sales],
[measures].[retailer sales],
[measures].[total sales]
} on columns,
{
[Not 2008]
} on rows
from [adventure works]

In the above example, I have create [Not 2008] as named set. This named set contains all calendar years from [CY 2001] to [CY 2007].

Create Member in MDX to create member or set for entire session

SQL
Create 
Member [adventure works].[measures].[customer sales] as [measures].[internet sales amount],format_string="#,###.00€"
Member [adventure works].[measures].[retailer sales] as [measures].[reseller sales amount],format_string="#,###.00€"
Member [adventure works].[measures].[Total Sales] as [measures].[internet sales amount]+ _
[measures].[reseller sales amount],format_string="#,###.00€"

In the above example, I have created three members for the entire session. Now, we can use these members in the entire session.
Note: We must require to provide cube name before specifying member name in Create Member.

SQL
Select 
{
[measures].[customer sales],
[measures].[retailer sales],
[measures].[total sales]
} on columns,
{
[Date].[Calendar].[Calendar Year]
} on rows
from [adventure works]

In the above example, I have consumed all new members created in the previous example through Create Member clause.

Drop member created using Create Member

SQL
drop member [adventure works].[measures].[customer sales]
go
drop member [adventure works].[measures].[retailer sales]
go
drop member [adventure works].[measures].[Total Sales]

In the above example, I have dropped all members created using Create Member clause.

Working with time dimension in MDX query

SQL
Select
{
[Date].[Fiscal].[Fiscal Year].[FY 2007]
} ON Columns
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above query will display Sales figure of financial year 2007.

Previous and Next member access in MDX

SQL
Select
{
[Date].[Fiscal].[Fiscal Year].[FY 2007].Prevmember,
[Date].[Fiscal].[Fiscal Year].[FY 2007],
[Date].[Fiscal].[Fiscal Year].[FY 2007].Nextmember
} ON Columns
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above example is using PrevMember and NextMember property to access previous and next member of any dimension.

ParallelPeriod function in MDX also returns prev/next member based on given parameter.

SQL
Select
{
ParallelPeriod([Date].[Fiscal].[Fiscal Year],1,[Date].[Fiscal].[Fiscal Year].[FY 2007])
} ON Columns
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above example returns previous year member of [FY 2007] because parameter value given to ParallelPeriod function is 1.

OpeningPeriod function and ClosingPeriod function.

SQL
Select
{
OpeningPeriod([Date].[Fiscal].[Fiscal Year]),
ClosingPeriod([Date].[Fiscal].[Fiscal Year])
} ON Columns
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

Openingperiod function returns first member of given dimension and Closingperiod function returns last member of given dimension.

Specify range in member using : (colon) operator

SQL
Select
{
OpeningPeriod([Date].[Fiscal].[Fiscal Year]):
ClosingPeriod([Date].[Fiscal].[Fiscal Year])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above example is using : operator to specify beginning member to end member of dimension. I have used Openingperiod function for first member and Closingperiod function for last member.

Starting Range in MDX can be specify using NULL value

SQL
Select
{
NULL:
ClosingPeriod([Date].[Fiscal].[Fiscal Year])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

In the above example, initial range is specified using value NULL.

PeriodsToDate function to specify range in member

SQL
--display financial year wise sales
Select
{
PeriodsToDate([Date].[Fiscal].[(All)],[Date].[Fiscal].[Fiscal Year].[FY 2008])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]
--display month wise sales of given financial year
Select
{
PeriodsToDate([Date].[Fiscal].[Fiscal Year],[Date].[Fiscal].[Month].[DECEMBER 2006])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

In the first example, PeriodtoDate function displays sales value from first available financial year to financial year 2008. While in second example when we have provided financial month in range, it will show sales of first financial month of year upto given financial month of year.

Specify range in MDX using LastPeriod function

SQL
--display previous 3 member
Select
{
LastPeriods(3,[Date].[Fiscal].[Fiscal Year].[FY 2008])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]
--display next 3 member
Select
{
LastPeriods(-3,[Date].[Fiscal].[Fiscal Year].[FY 2008])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above example displays that positive value in LastPeriods function shows last member and negative parameter value in LastPeriods function shows next member in given dimension.

Specify date range in dimension using YTD,QTD and MTD function. YTD covers Year till date members of dimension, QTD covers Quarter till date members and MTD covers Month till date members of given dimension.

SQL
--YTD for Year till date
Select
{
YTD([Date].[Calendar].[Calendar Year].[CY 2008])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]
--QTD for quarter till date
Select
{
QTD([Date].[Calendar].[Month].[DECEMBER 2006])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]
--MTD for month till date
Select
{
MTD([Date].[Calendar].[Date].[July 2,2006])
} ON Columns,
{
[Product].[Category].[Category]
}ON rows
From [Adventure Works]
Where [Measures].[Internet Sales Amount]

The above example displays date range specification using YTD, QTD and MTD function.

Points of Interest

This article is very interesting in learning of MDX query. Once the basics of MDX are known, this article will help you to extend your knowledge in calculation using MDX. My next article will cover MDX advance calculation process, decision making in MDX and KPI defining using MDX.

History

  • 31st July, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)