| MDX-How can I get Last (Previous) Year to Date (YTD) values? |
| Written by Vidas Matelis | |||
| Tuesday, 01 May 2007 19:37 | |||
|
Q: How can I get Last (Previous) Year to Date (YTD) values? A: First lets clarify question. Lets say you selected date March 22, 2004. Year To Date means you are interested in the interval from January 1, 2004 up to March 22, 2004. Last (Previous) Year to date means you are interested in the intervale January 1, 2003 up to March 22, 2003. With certain exceptions, you should be able to use function ParallelPeriod to get a date in the previous year matching your selected date. Assumption here is that you have similar members in each years hierarchy. If, for example, you have just business days in day hierarchy, matching might be different. Example: Lets say you selected March 22, 2004. If we use ParallelPeriod function, we can get back member for date March 22, 2003. Query Example:
Result:
As you know how to get previous years matching day, you can calculate YTD for previous year:
Results are:
Now lets run the same query, but substitute YTD and Parallel periods with hardcoded dates. This way we will make sure that calculation is right.
Results match previous query:
Note: You can substitute YTD function with PeriodsToDate function. These tests were done on Adventure Works database.
|
Most read
- How to install Adventure Works SQL DW and Analysis Services 2005/2008 sample database and project
- MDX-How can I get Last (Previous) Year to Date (YTD) values?
- What TCP port SQL Server Analysis Services 2005 uses
- MDX-How do you calculate monthly average of one year, optionally including empty months?
- MDX-How do I calculate sales for 12 Month to date?








Errors related to feature availability and configuration: The 'Measure expressions' feature is not included in the 'Standard Edition' SKU. 0 0
My Client has a requirement where they compare same day last year i.e. Monday with Monday, Tues with Tues !!! I know that the day will not be the same!
eg. the want to compare Thursday, 1 Jul 2010 = Thursday, 2 Jul 2010
so ideally it is Date - 364 !!
Any ideas?
I have managed to create my YTD value using:
SUM(YTD([Calendar].[Year - Period - Date].CurrentMember), [Measures].[Sales Value])
I have my last year YTD value with:
SUM(YTD(ParallelPeriod([Calendar].[Year - Period - Date].[Year]
, 1
, [Calendar].[Year - Period - Date].CurrentMember))
, [Measures].[Sales Value]
)
However I need to create a Full Last Year Value. (which is the same as the Last Year YTD value if it were run on the 31st Dec), basically the full value of the year.
I have SUM([Calendar].[Year].currentmember.prevmember, [Measures].[Sales Value]) but this only displays the correct value against the year or date level but not on the period (Probably as a period looks like [02 2010] etc.
my date heirachy is [Year] - [Period] - [Date]
Does anyone have any idea of what I can do to get it on the Period Level?
thanks. Andrew
"FROM [Adventure Works] WHERE ([Date].[Calendar].[Date].[March 22, 2004])"
FROM [Adventure Works] WHERE (Last Load in time & Not Empty)"
===========================================
WITH MEMBER [Measures].[Current YTD] AS
SUM({[Date].[Calendar].[Date].[January 1, 2004]:[Date].[Calendar].[Date].[March 22, 2004]}
, [Measures].[Internet Order Quantity])
MEMBER [Measures].[Last YTD] AS
SUM( {[Date].[Calendar].[Date].[January 1, 2003]:[Date].[Calendar].[Date].[March 22, 2003]} -- Jan 1, 2003: March 22, 2003
, [Measures].[Internet Order Quantity]
)
SELECT {[Measures].[Current YTD]
, [Measures].[Last YTD]
} ON 0
FROM [Adventure Works]
=================================
NOTE : This mean Last YTD is here as true, now my question is if we wish to retrieve data for Last YTD as false then how we can do it.
I mean, looking data all except Last YTD.
Please help me do it and provide the mdx.
Thanks in advance.
Regards
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=83&SiteID=1
http://technet.microsoft.com/en-us/library/ms175589.aspx
Your Time dimension probably is not specified as time, and attribute for year is not specified as Year. In such case you cannot use YTD. You need to fix structure. Also, you can rewrite this using PeriodsToDate function. Just instead of dealing with attributes start working with hierarchies, that is [Date].[YourHierarchyName].[Month], etc.
I have year level like this, [Date].[Year].[Year].[All].
I tried to replace hierachy level still did not work.
Execute:
SELECT {YTD([Date].[Year].CurrentMember)} ON 0
, {[Measures].[NRT Count]} ON 1
FROM [Data Warehouse]
Do you see expected results. If no, replace YTD with PeriodsToDate and specify proper level. If PeriodsToDate works, your date dimension does not have proper "Type" property set.
WITH MEMBER [Measures].[Current YTD] AS
SUM(YTD([Date].[Year].CurrentMember), [Measures].[NRT Count])
MEMBER [Measures].[Last YTD] AS
SUM(YTD(ParallelPeriod([Date].[Year].[Year].[2007]
, 1
, [Date].[Year].CurrentMember))
, [Measures].[NRT Count]
)
SELECT {[Measures].[Current YTD]
, [Measures].[Last YTD]
} ON 0
FROM [Data Warehouse]
WHERE ([Date].[Year].[Year].[2007])