Last Complete Month - ssas

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.

Related

MDX Syntax to retrieve Specific Dates

I am building some reports in Pyramid Analytics. I created a custom set and tried to write a MDX query which will retrieve the first day of the first month for the first three quarters of the previous year, selected in the slicer. i.e.
If I choose in my slicer 2017, I expect to see the following in the body of the report: Jan 1st 2016, April 1st 2016 and July 1st 2016. These will change according to the selection in the slicer.
I only got as far as the syntax below, which only returns first day of the first month of the first quarter of the previous year i.e. Jan 1st 2016
[Policy - Inception Date].[Calendar Hierarchy].[!#NewPar#!].PREVMEMBER.FIRSTCHILD.FIRSTCHILD.FIRSTCHILD
Note:Newpar = Parameter
Can you please help with the correct syntax?
Thanks.
Without knowing the structure of your Date dimension I'm having to use some guess work but I've made the following in Pyramid and it works fine against our cube:
Generate
(
Head //<<<this will get the first 3 quarters
(
Descendants
(
{[Date].[Date - Calendar Month].[!#aYear#!].PrevMember} //<<<this will get the previous year
,[Date].[Date - Calendar Month].[Calendar Quarter]
)
,3
)
,Head //<<<this will get the first day of each of the quarters found in the first argument of the generate function
(
Descendants
(
[Date].[Date - Calendar Month].CurrentMember
,[Date].[Date - Calendar Month].[Calendar Day]
)
,1
)
)
Here is the result with the year parameter in the top left:

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;

MDX: How to access currently selected dimension members?

I'm trying to create a measure in MDX to get the first day in the selected period (I have a YMD date dimension). I'm using EXISTING function to get the selected members. It's working fine if I only select one dimension member in Excel pivot table filter. However, as soon as I select multiple members (Example: 2012 & 2013 together), the EXISTING function is not working as expected.
I've created another measure to debug and see what is going on. The measure is defined as:
SetToStr(EXISTING([Date].[Date YMD].[Year].members))
If I have only one dimension member selected, this works fine, I get this back:
{[Date].[Date YMD].[All].[2013]}
However, as soon as I select 2012 and 2013 together, I get a list of all dimension members back:
{[Date].[Date YMD].[All].[N/A],[Date].[Date YMD].[All].[2007],[Date].[Date YMD].[All].[2008],[Date].[Date YMD].[All].[2009],[Date].[Date YMD].[All].[2010],[Date].[Date YMD].[All].[2011],[Date].[Date YMD].[All].[2012],[Date].[Date YMD].[All].[2013],[Date].[Date YMD].[All].[2014]}
The EXISTING function seems to work only when a single member is selected?
--
Update:
Maybe I was not clear enough in the original post. The problem I'm facing is getting the first and last date member if the date dimension is being filtered (in an Excel pivot table filter) and multiple date members are selected in the filter (example: when years 2012 & 2013 are selected together).
I've tried using the solution from here: http://bimic.blogspot.com/2011/07/mdx-rewrite-query-with-currentmember.html, but to no success.
I've created 2 measures now:
First Day Single:
HEAD(
DESCENDANTS(
[Date].[Date YMD].CURRENTMEMBER,
[Date].[Date YMD].[Day]
),
1
).ITEM(0).member_value
First Day Multiple Years
MIN(EXISTING [Date].[Date YMD].[Year].members, [Measures].[First Day Single])
Unfortunately I can't include a screenshot directly. You can see it on this link: http://social.msdn.microsoft.com/Forums/getfile/446659
As you can see, the measures work when a single year is selected in the pivot table filter, but don't work when you select more than one year.
In my cube and via management studio I can write a script like this to create measures that return numeric values based on the first day and last day of each month:
WITH
SET [Last12Months] AS
TAIL (
[Date].[Date - Calendar Month].[Calendar Month].members,
12)
MEMBER [Measures].[FirstDay] AS
HEAD(
DESCENDANTS(
[Date].[Date - Calendar Month].CURRENTMEMBER,
[Date].[Date - Calendar Month].[Calendar Day]
),
1
).ITEM(0).member_value
MEMBER [Measures].[LastDay] AS
TAIL(
DESCENDANTS(
[Date].[Date - Calendar Month].CURRENTMEMBER,
[Date].[Date - Calendar Month].[Calendar Day]
),
1
).ITEM(0).member_value
SELECT
{[Measures].[FirstDay],[Measures].[LastDay]} ON 0,
[Last12Months] ON 1
FROM [MyCube]
We use Office 2010 but using the OLAP Pivottables extensions add-in I can add the following two measures to my pivottable:
1.[Measures].[FirstDay]
HEAD(
DESCENDANTS(
[Date].[Date - Calendar Month].CURRENTMEMBER,
[Date].[Date - Calendar Month].[Calendar Day]
),
1
).ITEM(0).member_value
2.[Measures].[LastDay]
TAIL(
DESCENDANTS(
[Date].[Date - Calendar Month].CURRENTMEMBER,
[Date].[Date - Calendar Month].[Calendar Day]
),
1
).ITEM(0).member_value
Now whatever I use in Rows I get the correct answer from the pivot:
EDIT
If I manipulate the pivot so that our date dimension is in the pivot's filter and then go for multi-select of 2013 and 2014 it seems that the `mdx' which excel is using is the following:
WITH
MEMBER [Measures].[FirstDay] as
HEAD (
DESCENDANTS ( [Date].[Date - Calendar Month].CURRENTMEMBER, [Date].[Date - Calendar Month].[Calendar Day] )
, 1
).ITEM( 0 ).member_value
MEMBER [Measures].[LastDay] as
TAIL (
DESCENDANTS ( [Date].[Date - Calendar Month].CURRENTMEMBER, [Date].[Date - Calendar Month].[Calendar Day] )
, 1
).ITEM( 0 ).member_value
SELECT
{ [Measures].[FirstDay], [Measures].[LastDay] } ON COLUMNS
FROM
(
SELECT
({ [Date].[Date - Calendar Month].[Calendar Year].&[2012],
[Date].[Date - Calendar Month].[Calendar Year].&[2013] }) ON COLUMNS
FROM [OurCube]
)
I think the context of the function CURRENTMEMBER in the custom measures of this script will be lost because of the sub-select.
There is a workaround for your problem described here: http://sqlblog.com/blogs/mosha/archive/2007/09/26/how-to-detect-subselect-inside-mdx-calculations-aka-multiselect-in-excel-2007.aspx. But, as the writer (one of the developers of SSAS) writes, " the solution is neither elegant nor efficient". Anyway, it needs another measure group to be added to the cube, and a stored procedure to be written.
Really old topic, but I am posting my solution here as I haven't found the solution elsewhere:
Using dynamic set seems to have done the trick for me as will be populated only with members in context:
CREATE DYNAMIC SET [LastUpdateSet]
AS
TAIL(
NONEMPTY(
[LastUpdateDate].[Date].ALLMEMBERS
, [Measures].[Quantity]
)
, 1)
;
CREATE MEMBER CURRENTCUBE.[Measures].[LastUpdateQuantity]
AS (
[LastUpdateSet].item(0)
, [Measures].[Quantity]
)
;

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.

Clients growth over time MDX

I want to calculate the clients growth over the time.
So every day i have the total clients per state and per product subscription, and i can calculate the total for every day.
If i want to calculate the growth every day i don't have problems because i use a calculated member with
[Date].CurrentMember-[Date].PrevMember
This works pretty fine, but now i want to calculate the growth on month. So i have to sum all day growths of the month to calculate the month growth, right?
But my problem is that i'm too newbie to MDX and i can't find a way to produce that result (I want to know how many clients i have more or less over the year).
My intuition says that i need to sum all day's growth in the agregate date.
Could you help me?
If your date hierarchy has a month level above the date level (eg. Year-Month-Day), your cube will already have pre-processed this value. I would use ANCESTOR and LAG to get the data for a given day's month:
WITH MEMBER [Date].[YMD].[Current Month] AS
ANCESTOR(
[Date].[YMD].CurrentMember,
[Date].[YMD].[Month Level]
)
MEMBER [Date].[YMD].[Growth this month] AS
(
[Date].[YMD].[Current Month]
-
[Date].[YMD].[Current Month].LAG(1)
)
This will, however, only get the data from a whole-month period.
If what you're after is all the data between a particular day and the same day in the previous month, then PARALLELPERIOD is your go-to function (sidenote: not a goto statement). PARALLELPERIOD(Level, N, Member) will look at the position of Member amongst its siblings, then go to its ancestor at Level, go N members prior to that, and traverse back down to a member in the same relative position as Member.
In other words, it looks up your date in a prior month, year or whatev'.
WITH MEMBER [Date].[YMD].[One Month Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
1,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data since today last month] AS
(
/* The [Member]:[Member] syntax here is a range */
[Date].[YMD].[One Month Ago Today] : [Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[Two Months Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
2,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data between today last month and today in the previous month] AS
(
[Date].[YMD].[Two Months Ago Today] : [Date].[YMD].[One Month Ago Today]
)
MEMBER [Date].[YMD].[Growth in the last month since the previous month] AS
(
[Date].[YMD].[All data between today last month and today in the previous month]
-
[Date].[YMD].[All data since today last month]
)
Hope this helps.
<3