0% found this document useful (1 vote)
687 views2 pages

Time MDX Cheat Sheet

This document provides a cheat sheet of common MDX queries for time intelligence and calculations in Analysis Services cubes. It includes examples of queries to return the last month, previous year to date values, calculate averages over months or years, and filter on execution date parameters. The cheat sheet is a useful reference for common MDX patterns and functions.

Uploaded by

Suman Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
687 views2 pages

Time MDX Cheat Sheet

This document provides a cheat sheet of common MDX queries for time intelligence and calculations in Analysis Services cubes. It includes examples of queries to return the last month, previous year to date values, calculate averages over months or years, and filter on execution date parameters. The cheat sheet is a useful reference for common MDX patterns and functions.

Uploaded by

Suman Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Time MDX Cheat Sheet

How do you get Last month in the time dimension Need a MDX query that returns list of months from start of year up to specified
SELECT ClosingPeriod([Date].[Calendar].[Month], month.
[Date].[Calendar].DefaultMember) ON 0 SELECT YTD([Date].[Calendar].[Month].&[2003]&[8])
FROM [Sales Summary] ON 0
WHERE ([Measures].[Sales Amount]); FROM [Sales Summary];
I Need an MDX statement to show the first day of the last month in the How in the report can I order date dimension members in descending order?
cube SELECT {[Measures].[Reseller Order Quantity]} ON 0 ,
SELECT OpeningPeriod([Date].[Calendar].[Date] ORDER(Tail([Date].[Calendar].[Calendar Year].Members, 3)
, ClosingPeriod([Date].[Calendar].[Month], , [Date].[Calendar].CurrentMember.Member_Key, DESC ) ON
[Date].[Calendar].DefaultMember) ) ON 0 1
FROM [Sales Summary] FROM [Adventure Works]
I Need an MDX statement to get the last Month loaded into a cube I Need an MDX statement to get the first month of the last year loaded into a
SELECT ClosingPeriod([Date].[Calendar].[Month], cube
[Date].[Calendar].DefaultMember) ON 0 SELECT OpeningPeriod([Date].[Calendar].[Month]
FROM [Sales Summary]; , ClosingPeriod([Date].[Calendar].[Calendar Year],
[Date].[Calendar].DefaultMember) ) ON 0
FROM [Sales Summary];
How do you write MDX query that uses execution date/time as a
parameter?
SELECT {[Measures].[Internet Order Count]} ON 0
, {StrToMember("[Date].[Date].["
+ Format(now(), "MMMM dd, yyyy") + "]")
} ON 1
FROM [Direct Sales];
Need MDX Query to get latest months and previous years same months I need an MDX query to show year level data for all years except the last one, and
data month level data for the last year.
SELECT {ClosingPeriod([Date].[Calendar].[Month], SELECT {NULL:
[Date].[Calendar].DefaultMember) ClosingPeriod([Date].[Calendar].[Calendar Year],
, ParallelPeriod([Date].[Calendar].[Calendar Year] , [Date].[Calendar].DefaultMember).PrevMember
1 , DESCENDANTS(ClosingPeriod([Date].[Calendar].[Calendar
, ClosingPeriod([Date].[Calendar].[Month], Year], [Date].[Calendar].DefaultMember)
[Date].[Calendar].DefaultMember) ) } ON 0 , [Date].[Calendar].[Month]) } ON 0
FROM [Sales Summary]; FROM [Sales Summary];
How to create calculated member for AVG sales over last 3 years based on How do I calculate sales for 12 Month to date in MDX?
NOW()? WITH MEMBER [Measures].[Last 12 Mth Order Count] AS
CREATE MEMBER CurrentCube.Measures.[Avg3Years] AS SUM(
Avg( {ParallelPeriod( [Date].[Date].[Date Yr], 3, ClosingPeriod([Date].[Calendar].[Month],
StrToMember("[Date].[Date].&[" + Format(now(), [Date].[Calendar].[All Periods]).Lag(12)
"yyyyMMdd") + "]")) : ClosingPeriod([Date].[Calendar].[Month],
: StrToMember("[Date].[Date].&[" + Format(now(), [Date].[Calendar].[All Periods])
"yyyyMMdd") + "]")} , [Measures].[Order Count])
, [Measures].[Sales Qty]) ; SELECT [Measures].[Last 12 Mth Order Count] ON 0
FROM [Adventure Works]
How can I get Last (Previous) Year to Date (YTD) values? MDX query to get count of months with sales amount > 0 in defined period
WITH MEMBER [Measures].[Current YTD] AS WITH Member [Measures].[Months With Above Zero Sales] AS
SUM(YTD([Date].[Calendar].CurrentMember), COUNT(FILTER( DESCENDANTS({[Date].[Calendar].[Calendar
[Measures].[Internet Order Quantity]) Year].&[2003]: [Date].[Calendar].[Calendar
MEMBER [Measures].[Last YTD] AS Year].&[2004]}
SUM(YTD(ParallelPeriod([Date].[Calendar].[Calendar , [Date].[Calendar].[Month]) , [Measures].[Sales Amount]
Year] > 0 ) )
, 1 , [Date].[Calendar].CurrentMember)) SELECT {[Measures].[Sales Amount], [Measures].[Months
, [Measures].[Internet Order Quantity] ) With Above Zero Sales]} ON 0
SELECT {[Measures].[Current YTD] , [Product].[Product Model Lines].[Product Line].Members
, [Measures].[Last YTD] } ON 0 on 1
FROM [Adventure Works] FROM [Adventure Works]
WHERE ([Date].[Calendar].[Date].[March 22, 2004]) WHERE ([Date].[Calendar].[Calendar Year].&[2003]:
[Date].[Calendar].[Calendar Year].&[2004]);
How do you calculate monthly average of a year? How do you calculate monthly average of a year including empty months?
WITH MEMBER [Measures].[AvgVal] AS WITH MEMBER [Measures].[AvgVal] AS
Avg( Descendants([Date].[Calendar].[Calendar Avg( Descendants([Date].[Calendar].[Calendar
Year].&[2004] Year].&[2004]
, [Date].[Calendar].[Month]) , [Date].[Calendar].[Month])
, [Measures].[Internet Order Count] ) , CoalesceEmpty([Measures].[Internet Order Count], 0)
SELECT {[Measures].[AvgVal]} ON 0 )
FROM [Adventure Works] SELECT {[Measures].[AvgVal]} ON 0
WHERE ([Product].[Product Model Lines].[Model FROM [Adventure Works]
Name].&[Classic Vest]) WHERE ([Product].[Product Model Lines].[Model
Name].&[Classic Vest])

You can find latest version of this document at http://www.ssas-info.com/ssas-cheat-sheets.


Contributors: Vidas Matelis, Thomas Ivarsson Page 1
You can find latest version of this document at http://www.ssas-info.com/ssas-cheat-sheets.
Contributors: Vidas Matelis, Thomas Ivarsson Page 2

You might also like