![]() |
|
|
|
By: Ray Barley | Read Comments (8) | Related Tips: > Reporting Services Analysis Services |
I have a requirement to develop a report that uses a SQL Server Analysis Services Cube as a data source. The report must allow the user to select the month from a dropdown list in a report parameter. Based on the selected month the report should show sales for the selected month, sales year to date for the selected month, last year's sales for the selected month, and last year's sales year to date for the selected month. Can you give me some ideas on how to do this?
Based on the requirements, you need to add some calculated members to your dataset query. There are built-in MDX functions that will do exactly what you need. To restate your problem, you would like to build a report that looks like this:
The following are the details on the report columns (assume the selected month is September, 2007):
In an earlier tip titled How to Customize the Dropdown List in a SSRS Report that Uses a SSAS Cube Data Source I showed how to customize the dropdown list for a report parameter (called CurrentMonth) for a SQL Server Analysis Services data source. The requirement there was to populate the dropdown list with 24 months based on a current month as retrieved from a table in a relational database. I'm going to leverage the sample report from the earlier tip and just focus on building the dataset query to render the sample report as shown above.
I'll use the Adventure Works cube that was published for SQL Server 2008 R2 as the basis for the report. I'll use Report Builder 3.0 to create the sample report.
The MDX syntax for a defining a calculated member within an MDX query is:
WITH MEMBER [MEMBER NAME GOES HERE] AS [CALCULATION GOES HERE]
You can define one or more calculations and the scope of the calculations is limited to the MDX query that follows. You start the calculations by specifying WITH.
The calculated member we need for Current Year sales is as follows:
MEMBER [Measures].[Current Year Sales] AS
AGGREGATE (
YTD (
STRTOMEMBER( @CurrentMonth )
),
( [Measures].[Internet Sales Amount] )
)
The components of the calculation are as follows:
The calculated member we need for Last Year Current Month is as follows:
MEMBER [Measures].[Last Year Sales Current Month] AS
( ParallelPeriod (
[Date].[Calendar].[Calendar Year],
1,
STRTOMEMBER( @CurrentMonth )
),
[Measures].[Internet Sales Amount]
)
The components of the calculation are as follows:
The calculated member we need for Last Year is as follows:
MEMBER [Measures].[Last Year Sales] AS
AGGREGATE(
YTD (
PARALLELPERIOD (
[Date].[Calendar].[Calendar Year],
1,
STRTOMEMBER( @CurrentMonth )
),
( [Measures].[Internet Sales Amount] )
)
)
The above calculation is the same as Current Year Sales with a twist; the first parameter to the YTD MDX function is the PARALLELPERIOD expression we used in the Last Year Sales Current Month calculated member.
NOTE: The above calculated members use MDX functions that rely on the fact that your Date (or Time) dimension has been decorated with the Business Intelligence type information. When you create a Date dimension you need to specify the dimension Type as Time and also specify the attribute Type for your attributes; e.g. Year, Month, etc. Please refer to our earlier tip Using the Built-In Time Dimension and Time Intelligence in SQL Server Analysis for the details.
In this last step we need to create a dataset that defines our calculated members, retrieves the data for our report, and filters the data based on the month selected by the user. Right click on Datasets in the Report Data section, select Add Dataset, and fill in the dialog as shown below:
The above MDX query has to be entered as text in order to use our custom CurrentMonth parameter and the calculated members that also use the custom CurrentMonth parameter. The full query is shown below:
WITH
MEMBER [Measures].[Current Month Sales] AS
[Measures].[Internet Sales Amount]
MEMBER [Measures].[Current Year Sales] AS
AGGREGATE (
YTD (
STRTOMEMBER( @CurrentMonth )
),
( [Measures].[Internet Sales Amount] )
)
MEMBER [Measures].[Last Year Sales Current Month] AS
( ParallelPeriod (
[Date].[Calendar].[Calendar Year], 1, STRTOMEMBER( @CurrentMonth )
) ,
( [Measures].[Internet Sales Amount] )
)
MEMBER [Measures].[Last Year Sales] AS
AGGREGATE (
YTD (
ParallelPeriod (
[Date].[Calendar].[Calendar Year], 1, STRTOMEMBER( @CurrentMonth )
)
),
( [Measures].[Internet Sales Amount] )
)
SELECT
{
[Measures].[Current Month Sales] ,
[Measures].[Current Year Sales] ,
[Measures].[Last Year Sales Current Month] ,
[Measures].[Last Year Sales]
} ON COLUMNS,
{
[Product].[Category].MEMBERS
} ON ROWS
FROM [Adventure Works]
WHERE STRTOMEMBER( @CurrentMonth )
Click the Query Designer button on the Dataset Properties dialog to show the query designer and associate the CurrentMonth parameter with the @CurrentMonth placeholder in the MDX query. Click the Query parameters icon on the toolbar (the icon is shown in the window title of the screen shot below) and fill in the dialog as shown:
The following is the output shown when running the report:
| Friday, January 04, 2013 - 4:16:20 AM - Neo | Read The Tip |
|
I have a requirement to develop a report on SSRS 2008. The report must allow the user to select the month from a dropdown list in a report parameter. Based on the selected month the report should show sales for that selected month, sales for the previous month and last year's sales for the selected month. Can you give me some ideas on how to do this? |
|
| Friday, January 04, 2013 - 9:33:27 AM - Raymond Barley | Read The Tip |
|
If you're reporting against a cube then to get the previous month sales you could use a calculated member somthing like this: MEMBER [Measures].[Current Year Sales Previous Month] AS
( ParallelPeriod (
[Date].[Calendar].[<<PUT MONTH MEMBER HERE>>], 1, STRTOMEMBER( @CurrentMonth )
) ,
( [Measures].[Internet Sales Amount] )
)
|
|
| Wednesday, February 20, 2013 - 5:56:32 PM - Adrian | Read The Tip |
|
How can this be done with a fiscal year instead? |
|
| Thursday, February 21, 2013 - 5:24:54 AM - Ray Barley | Read The Tip |
|
You need to build your time dimension to include both calendar year and fiscal year hierarchies. For example in this tip I refer to [Date].[Calendar].[Calendar Year]. You need something like [Date].[Calendar].[Fiscal Year]. Take a look at this tip: http://www.mssqltips.com/sqlservertip/1454/built-in-time-dimension-and-time-intelligence-in-sql-server-analysis/ You can specify that you want calendar year and fiscal year when you create the dimension |
|
| Sunday, February 24, 2013 - 8:10:21 PM - atif yasin | Read The Tip |
|
Very good article, One question if there is a selected month and it gives current YTD sales and last year YTD sales it includes the whole month. Let's say it is Sept 15 and user select month of september all sales for current YTD will include all sales happened until Sept 15 and Last Year YTD will include the whole month of September. Is it possible to include only last year YTD based on current month date....I know there is a way of doing it using the date parameter where user can select a single date sep 15, it'll include current YTD and last YTD until sep 15. Is there a way to do it with month parameter. Thanks |
|
| Monday, February 25, 2013 - 5:49:25 AM - Ray Barley | Read The Tip |
|
I found an example in the book MDX with Microsoft SQL Server 2008 R2 Analysis Services Cookbook. It shows a calculated member for "current period" sales and previous period sales which is a range of dates; a range of dates is just 2 dates delimited by a colon. In your case you could use the first day of the year and the date selected for the report as the date range.
WITH
MEMBER [Internet Sales CP] AS
Sum( { [Date].[Calendar].[Date].&[20071201] :
[Date].[Calendar].[Date].&[20071224] },
[Measures].[Internet Sales Amount] )
MEMBER [Internet Sales PP] As
Sum({ [Date].[Calendar].[Date].&[20071201] :
[Date].[Calendar].[Date].&[20071224] },
( ParallelPeriod( [Date].[Calendar].[Calendar Year],
1,
[Date].[Calendar].CurrentMember ),
[Measures].[Internet Sales Amount] )
)
, FORMAT_STRING = 'Currency'
MEMBER [Internet Sales YoY %] As
iif( [Internet Sales PP] = 0, null,
( [Internet Sales CP] / [Internet Sales PP] ) )
, FORMAT_STRING = 'Percent'
SELECT
{ [Internet Sales PP],
[Internet Sales CP],
[Internet Sales YoY %] } ON 0,
{ [Product].[Color].MEMBERS } ON 1
FROM
[Adventure Works]
|
|
| Wednesday, March 20, 2013 - 3:51:47 PM - Alla | Read The Tip |
|
Hello Ray, I hope you can help me with this MDX query I have measure month-to-month comparison. But for the current month of March, 20th, 2013 the previous year March 2012 showed full month worth of measure. How can I do this with my MDX below: WITH , PARALLELPERIOD ([Date].[Calendar Month].[Calendar Month],12,[Date].[Calendar Month].CurrentMember)) MEMBER [Measures].[SCInvoiceAmountCY] as([Measures].[Invoice Amount]) SELECT
{ [Measures].[SCInvoiceAmountCY] , [Measures].[SCInvoiceAmountPY]
} ONCOLUMNS , ([Date].[Calendar Month].[Calendar Month] ) ONROWS FROM
[Retail Sales] where
[Date].[Calendar Year].&[2013] |
|
| Thursday, March 21, 2013 - 4:16:01 PM - Raymond Barley | Read The Tip |
|
Here is an example I found in the book MDX with Microsoft SQL Server 2008 R2 Analysis Services Cookbook
WITH
MEMBER [Internet Sales CP] AS
Sum( { [Date].[Calendar].[Date].&[20071201] :
[Date].[Calendar].[Date].&[20071224] },
[Measures].[Internet Sales Amount] )
MEMBER [Internet Sales PP] As
Sum({ [Date].[Calendar].[Date].&[20071201] :
[Date].[Calendar].[Date].&[20071224] },
( ParallelPeriod( [Date].[Calendar].[Calendar Year],
1,
[Date].[Calendar].CurrentMember ),
[Measures].[Internet Sales Amount] )
)
, FORMAT_STRING = 'Currency'
MEMBER [Internet Sales YoY %] As
iif( [Internet Sales PP] = 0, null,
( [Internet Sales CP] / [Internet Sales PP] ) )
, FORMAT_STRING = 'Percent'
SELECT
{ [Internet Sales PP],
[Internet Sales CP],
[Internet Sales YoY %] } ON 0,
{ [Product].[Color].MEMBERS } ON 1
FROM
[Adventure Works]
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |