MDX - Retrieve last month of prior year value - ssas

I have a date hierarchy of Year->Qtr->Month->Day
I am trying to retrieve value for last month of prior year (i.e. Dec 2020). Basically all months of 2021 will be compared to value as it was Dec of 2020.
The following gives me exactly what I need, however I don't want to explicitly put in the member for 2021. I want to use whatever year the user is viewing. I have tried currentmember with no luck as I get nothing.
SSAS Calculation:
sum(ClosingPeriod ([Calendar].[Time].[Month],[Calendar].[Time].[Year].&[2021].lag(1)),
[Measures].[Compliance Pct])
Tried this with no luck:
sum(ClosingPeriod ([Calendar].[Time].[Month],[Calendar].[Time].[Year].currentmemeber.lag(1)),
[Measures].[Compliance Pct])

[Calendar].[Time].[Year] looks like a level, not a hierarchy. And there is a CurrentMember per hierarchy, not per level in MDX. In order to get to the current year member independent on which level of the [Calendar].[Time] the current member is, you can use
Ancestor(
[Calendar].[Time].CurrentMember,
[Calendar].[Time].[Year]
).lag(1)
instead of
[Calendar].[Time].[Year].currentmemeber.lag(1)
The Ancestor(...) expression takes the year level member that is on the level of the current member or above it, see the documentation of Ancestor. It will not work if the current member of the hierarchy is the All member, as that would be on the level above the years, not below or on the level. But then your calculation does not make sense anyway.
And, by the way, it is currentmember, not currentmemeber.

Related

How to fix repeating hierarchy month and quarter names issue in SSAS

I am developing a cube in SSAS using adventure works database. I have created a hierarchy as followed:- Year-->Month-->Quarter. I have set key columns for Month as Year and month and for quarter as Year and month and set the attribute relationships by rolling over quarter to month and month to year. When i deploy the cube after browsing it, i see the month names and quarters are repeated which is affecting mdx query to display quarters multiple times. Is there a way to fix this issue so that month names and quarters are not repeated in hierarchy. I searched a lot but couldn't find a way to fix it.
The problem is in your hierarchy.
You should change it from Year-->Month-->Quarter To Year-->Quarter-->Month which is the logical way of things. One year has many quarters and one quarter have many months and this is a hierarchy.

SSAS MDX Except statement issue

I am working on an requirement in Cube MDX like data for year 2008 and prior should not be displayed in report.
So thought of using MDX 'EXCEPT' function in SCOPE statement to exclude the mentioned years.
Time dimension is hierarchy with structure Year-->Quarter-->Month
Initial code was like
SCOPE([Measure Item].[Measure Item].&[SAL]);
SCOPE(EXCEPT({[Time].[Time].[Year]},{[Time].[Time].&[2006],[Time].[Time].&[2007],[Time].[Time].&[2008]}));
this=([Measure Item].[Measure Item].&[SAL]);
END SCOPE;
END SCOPE;
With the code after deploying, data for year 2008 and prior got excluded. But the issue here is, I can only see data on Grand Total on each Year. Please see the example in below screenshot, I cannot see data on Quarter level and month level.
Not able to see data on quarter and month level
Tried few other codes with descendants function, which is working in SSMS but when write it with SCOPE statements its not working.
Code written in SSMS:
select [Measures].[Value] on 0,
EXCEPT({[Time].[Time].members} ,
{descendants([Time].[Time].&[2006],0,SELF_AND_AFTER),descendants([Time].[Time].&[2007],0,SELF_AND_AFTER)}) on 1 from [TOPAS Reporting]
Working in SSMS but not working in SCOPE statement
I can see data on even Quarter and month level. But same code is not working in SCOPE statements of cube MDX.
Please help me to solve this issue and let me know for further details .
Thanks in advance.
Do you really need a scope statement?
You can achieve it by using the following calculation:
Aggregate(
existing [Time].[Time].&[20090101]:NULL,
[Measure Item].&[SAL]
)
where [Time].[Time].&[20090101] is the first day on the Day level.

Filter SET by currentMember.MemberCaption

I have a SET of members of some dimension, let's say [Dim].[Dim].[Dim]. Then I wrote a query that returns all the elements from this dimension for 2016 year of some measure, let's say there are 5 of such.
The next step that I want to make is to find first one member in this dimension with the same name (as CurrentMember in row) but for 2015 year. As result, I want to calculate some measure with regards to 2015-member, not 2016's.
So the problem is in scope - I can't GENERATE such set because CurrentMember's function scope will be equal to GENERATE's scope so I am unable to extract actual current member that is on row now and for which I want to find "same previous by name" members.
Am I able to do this with plain MDX or should I go with some workaround like finding such "same" elements in t-sql view and making child-parent relations in cube by hand? The second approach I think is very undesirable and ugly.
Thanks.
Is there a reason you can't just use .PrevMember on the Time (Year, I guess) dimension?
WITH MEMBER [Measures].[SomePrevYearCalculation]
AS ([Time].[Year].CurrentMember.PrevMember,[Measures].[AMeasure])
SELECT
{[Measures].[AMeasure],[Measures].[Some2015Calculation]} ON 0,
[[Some complicated dimension stuff]] ON 1
FROM Cube
WHERE [Time].[Year].[2016]
Or are you dealing with a dimension that has multiple duplicate Names at leaf-level, so that you can't match the 2015 to 2016 figures by the actual dimension member?

MDX Retention Rate

I am new to OLAP and I have a pretty tricky assignment that I am not sure is possible in MDX:
I work for an insurance company and I have been asked to provide a Retention Rate calculation to compare the number of policies we have kept from one time period to another.
The data in our fact table consists of a month-end snapshots of each our policies and there is a flag to indicate whether they are currently active at that time.
So, in plain English, the steps to find the Retention Rate from December 2014 to December 2015 would be:
Get the set of active policy IDs as of December 2014 (set #1)
Get the set of active policies as of December 2015 that have the SAME policy ID as set #1 (set #2)
Divide set #2 by set #1 to get the Retention Rate
I am just not sure if it is possible to compare specific IDs from two different sets like that in MDX.
Any help would be greatly appreciated!!
This isn't something one would normally use MDX for, since it involves a condition at the detail level (PolicyID), and MDX is all about data in aggregate.
However, if you are willing and able to add a flag to your fact table/view it can be done. To address your exact question, you could simply add a bit (or int) flag into your fact table. For each record, the flag would be true (1) if the PolicyID is active now AND was active a year ago, and false (0) if it was not.
Then you can add a new measure to your cube that counts "retained policies", which is just the sum of the flag you just added, and then you can easily divide one measure by another.
If your needs are more complex than this one instance, there might be ways to add more complex data, but the point is that you have to create a way for your cube to be able to compare aggregations.

Rename Attribute value in Time Dimension in SSAS

I am working on SQL Analysis service to provide ad hoc reporting in my application. I have created a time dimension to use in my cube. It has some predefined attributes. e.g. Month of year. It is having values like Month 1, Month 2, etc. while I want January for Month 1, February for Month 2, etc...
Can any one please suggest me some work around it??
As I am newbie to SSAS, Sorry if I am missing something very silly....
When you work with attributes in SSAS, there are two properties that affect the members of that attribute. The first property - which is set by default when you create the attribute - is KeyColumn. The column that you use here determines how many members are in the attribute because processing generates a SELECT DISTINCT statement based on this column. It's a good idea if you use an integer value here for better performance.
It sounds like perhaps you have a month number for your attribute here, which is good. Except that you want to display a month name. In that case, you set the NameColumn property with the column in your data source view that contains the month name. This produces the label that you see when you browse the dimension.
That said, it's usually not a good idea to have just a month number or month name because you probably want to create a hierarchy to roll up months by year and you won't be able to do that with just a month name. I wrote a blog post describing how to set up a date dimension that might help you: http://blog.datainspirations.com/2011/05/11/sqlu-ssas-week-dimension-design-101-2/