First date of Current Quarter MDX - mdx

I am trying to convert some T-SQL queries into MDX:
I require the first date of the Current Quarter for our reporting solution.
Within the cube we have a dynamic 'Current Quarter' flag
This seems like a simple problem but it has caused me no end of issues. I've managed to isolate the correct date but with the query below but in SSRS I get an error that states 'The default value for StartDate is not the expected type' i.e. it is not in DateTime format. This leads me to believe it is being passed as a string
How do I modify the following query to pass the StartDate as a DateTime and not a string?
The second piece of code is a secondary attempt at the same thing except I get the first value of the entire Date dimension (1900-01-01) instead of the first day of the current quarter.
First attempt:
SELECT
NON EMPTY
{} ON COLUMNS,
HEAD(
[Date Lead Date].[Calendar Date].[Calendar Date]
,1) ON ROWS
FROM [Leads]
WHERE [Date Lead Date].[Current Quarter Flag].&[1]
Second attempt (produces 1900-01-01):
WITH
Member [Measures].[StartDate] AS
HEAD(
[Date Lead Date].[Calendar Date].[Calendar Date]
,1).ITEM(0).ITEM(0).NAME
SELECT
NON EMPTY
[Measures].[StartDate] ON COLUMNS
FROM [Leads]
WHERE [Date Lead Date].[Current Quarter Flag].&[1]

Untested but I'm wondering if this works?
WITH
Member [Measures].[StartDate] AS
[Date Lead Date].[Calendar Date].CURRENTMEMBER.MEMBER_VALUE
SELECT
NON EMPTY
{[Measures].[StartDate]} ON COLUMNS,
HEAD(
[Date Lead Date].[Calendar Date].[Calendar Date]
,1) ON ROWS
FROM [Leads]
WHERE [Date Lead Date].[Current Quarter Flag].&[1]

Related

How can I show a last year value through a dimension in MDX?

We have a relative date dimension in our cube that has member values This Year and Last Year as an example.
The user uses this set on columns against sales so they can look at Sales for this year and the same period last year.
The problem comes when they are using the Calendar Date filter to only select values for this month. If the user selects just this month, then the Last Year member disappears.
Is there a way (perhaps with scope statements) that I can tell SSAS: If the user is using these attributes and they select a specific month (or other level), then use ParallelPeriod to implicitly include the same members for the previous year so that they can see the last year sales?
If not, without using calculated members (I have so many measures that I don't want to have to duplicate them), is there a way using dimensions to show a last year value even if the user selects this year in the date dimension?
There are a few options here...
I would just add a new Calculated Member to an existing dimension,i'll add it to a Pseudo-Dimension [Time Period] dimension with something like this:
(i'm pretty sure you need to add it to an existing Hierarchy. I'll assume [Relative Time])
CREATE MEMBER [Time Period].[Relative Time].[Last Year]
AS NULL
, VISIBLE=1;
SCOPE(
DESCENDTS([Time].[YearMonthDate].[Year].MEMBERS,,AFTER)
,[Time Period].[Relative Time].[Last Year]
);
THIS = AGGREGATE(
PARALLELPERIOD(
[Time].[YearMonthDate].[Year]
,1
,[Time].[YearMonthDate].CURRENTMEMBER
)
,[Measures].CURRENTMEMBER
);
END SCOPE;

Using the ANCESTOR function on a DATE dimension

Here is my script:
WITH MEMBER [Date].[Date - Calendar Month].[2MthPrev] AS
(
ANCESTOR(
CLOSINGPERIOD([Date].[Date - Calendar Month].[Calendar Month]),
2
))
SELECT
NON EMPTY
{
[Date].[Date - Calendar Month].[2MthPrev]
}
ON ROWS,
NON EMPTY
[Measures].[Revenue]
ON COLUMNS
FROM [OurCube]
The query runs with no error but the result pane is empty.
I've attempted to create a custom member in the [Date - Calendar Month] hierarchy that is two months previous to the last month in the hierarchy's level [Calendar Month]. So if the last loaded month in the cube is July 2013 then I'd hope that [2MthPrev] would show the results from May 2013.
I believe the problem is with the custom member [2MthPrev] and its use of ANCESTOR - how is this fixed?
This query returns 2 months prior from the last populated date for the given measure group. You may have to fiddle with it to make a calculated member. The second argument in tail is optional. If you don't include it, the default value is 1.
So I'm returning the item that is 2 prior to (lag) the first item (Item(0)) of the set which includes the last month (tail) from the set of months for which there are values in the Measure Group (exists clause).
select {Tail(Exists([Date].[Date - Calendar Month].[Calendar Month].members, , "Measure Group Name")).Item(0).lag(2)} on 0
from [OurCube]
Not sure to understand the query but assuming [Calendar Month] is having at most 2 levels (ALL + months) I guess you're asking for something like :
[a-month].parent.parent = [all].parent = null
[2MthPrev] is a scalar value and not a member; if you want to debug to sth like:
with [2MthPrev] as ancestor( ... ).uniqueName
Hope that helps.

Last Complete Month

The following returns the last member in our set of months:
TAIL([Date].[Date - Calendar Month].[Calendar Month],1)
Our cube contains data upto and including yesterday - so if the above is run today it returns the member [July 2013].
I want the Last Completed Month so if run today (4th July) it should return [June 2013].
Not sure if this adds some extra complexity but if the expression is run on the first of a month then the last member in the hierarchy will actually be equal to the Last Complete Month.
So sometime the last completed month is the last member, and sometime it is the next but last member, in the hierarchy [Calendar Month]
Is there a fool-proof way of coding this expression?
You can determine easily if 'now' is the first day of month as following:
IIF( Day(Now()) = 1, ... , ... )
So you can create a calculated member :
with member [Last Completed Month] as
IIF( Day(Now()) = 1,
TAIL([Date].[Date - Calendar Month].[Calendar Month],1)(0),
TAIL([Date].[Date - Calendar Month].[Calendar Month],2)(0)
)
dunno about the [Calendar Month] hierarchy structure, but perhaps using a lastChild and lastChild.prevMember would be more efficient...
[edit] Tail( ... )(n) retrieve the n-th element of the set returned by Tail - this is a shortcut of item(n)
[edit] imagine the month hierarchy is flat under a all member: [Calendar Month].[All].lastChild would do the same as the Tail() and lastChild.prevMember to get the one before the last.

MDX TTM Calculated member - day level

I need to create a calculated member that calculates revenue for TTM (Trailing Twelve Months) associated to selected date (day level).
I tried something like this:
SUM(
{
[Accounting Effective Date].[Date Hierarchy].CurrentMember.Lag(365)
: [Accounting Effective Date].[Date Hierarchy].CurrentMember
},
[Measures].[Revenue]
)
But this doesn't work with leap year, for example if I pick 2013-01-01 than it returns 2012-01-02.
I have also tried this but this one is also not good since it takes first day of the month:
SUM(
{
[Accounting Effective Date].[Date Hierarchy].CurrentMember.Parent.Lag(12).FirstChild
: [Accounting Effective Date].[Date Hierarchy].CurrentMember
},
[Measures].[Revenue]
)
Date hierarhy is following:
Year > Quarter > Month > Date
the following query can help, it uses cousin function to return last years date.
with member [Measures].[TestValue] as
(cousin([Date].[Calendar].currentmember,[Date].[Calendar].currentmember.parent.parent.lag(1)),[Measures].[Internet Sales Amount])
member [Measures].[TestDate] as
cousin([Date].[Calendar].currentmember,[Date].[Calendar].currentmember.parent.parent.lag(1)).item(0).name
select
{[Measures].[Internet Sales Amount],[Measures].[TestValue],[Measures].[TestDate]}
on columns,
{
[Date].[Calendar].[Date].&[20130922]
}
on rows from
[adventure works]

How to Get Current Year with MDX Query?

I have little thing about MDX.
Is there any way to get current year from the system/server with MDX query?
I didn't have any dimension related to date or something like that.
Regards.
Create a member and define it as 'YEAR(NOW())'
Member [Measures].[Current Year] as 'YEAR(NOW())'
Here is a more complete sample
-- The First Calculated member is the value of NOW()
WITH MEMBER [Measures].[Full Date] as 'NOW()'
-- The Second Calculated Member is the Day part of the first calculated member.
MEMBER [Measures].[What Day] as 'DAY([Full Date])'
-- The Third Calculated Member is the Month part of the first calculated member.
MEMBER [Measures].[What Month] as 'MONTH([Full Date])'
-- The Fourth Calculated Member is the Year part of the first calculated member.
Member [Measures].[What Year] as 'YEAR([Full Date])'
SELECT
{[Full Date],[What Day],[What Month],[What Year]} ON COLUMNS
FROM [Your Cube]