year total not changing according to month selection in MDX - mdx

I want to bring the year totals to the report, but the year totals should remain the same in the month selection. I just want the year totals to change in the year selection. what can I do to achieve the same
SUM(FILTER
([Time].[Year-Month].children,[Tarih].[Year-Month].CurrentMember.NAME < FORMAT(DATEADD("Y",1,Now()), "yyyyMM"))
,[Measures].[Target])

There are various properties in MDX - I'm unsure .NAME is correct - you can try some of the other properties:
WITH MEMBER Measures.MemberName AS
[Date].[Calendar].[July 1, 2003].Properties('Name')
MEMBER Measures.MemberVal AS
[Date].[Calendar].[July 1, 2003].Properties('Member_Value')
MEMBER Measures.MemberKey AS
[Date].[Calendar].[July 1, 2003].Properties('Key')
MEMBER Measures.MemberID AS
[Date].[Calendar].[July 1, 2003].Properties('ID')
MEMBER Measures.MemberCaption AS
[Date].[Calendar].[July 1, 2003].Properties('Caption')
Also it is safer to add a conversion around the NOW() function:
FORMAT(DATEADD("Y",1, CDATE(Now())), "yyyyMM")

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;

MDX - Calculated MTD measure returning null value

I have a Cube with a Date dimension hierarchy (Year,Semester,Quarter,Month,Day).
There are 2 measures available in the datawarehouse.
[AUD DLY] : Daily numbers
[AUD YTD] : Daily incremental YTD numbers
Hierarchy is as follows
Date - Dimension
Financial - Hierarchy
Year - [Date].[Financial].[Year]
Semester - [Date].[Financial].[Semester]
Quarter - [Date].[Financial].[Quarter]
Month - [Date].[Financial].[Month]
Day - [Date].[Financial].[Day]
Measures [AUD DLY]
Measures [AUD YTD]
I need to add a MTD field in the measures such that when the business select a particular date on their slicer in excel for e.g., 3 March 2016, MTD should be calculated as either of the following ways :
1) [AUD MTD] should be calculated by subtracting [AUD YTD] on
last day of previous month from the current selected date.
So if we select 3 March 2016 then
[AUD MTD] = [AUD YTD] on 3 March 2016 - [AUD YTD] on 29 Feb 2016
OR
2) [AUD MTD] should be calculated by adding the [AUD DLY] from first day
of the current month until the selected date in that month.
So if we select 3 March 2016 then
[AUD MTD] = SUM ([AUD DLY] from 1 March 2016 to 3 March 2016)
I created a New Calculated Member from Calculations tab in the Cube designer in the BIDS 2010. The MDX query is below. However when I try to browse the cube the [AUD MTD] values are only returning nulls.
Can someone please help what am I doing wrong ?
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS Aggregate
(
PeriodsToDate
(
[Date].[Financial].[Month]
,[Date].[Financial].CurrentMember
)
,[Measures].[AUD DLY]
),
FORMAT_STRING = "Currency",
NON_EMPTY_BEHAVIOR = { [AUD DLY] },
VISIBLE = 1 , DISPLAY_FOLDER = 'AUD Values' , ASSOCIATED_MEASURE_GROUP = 'Measures' ;
Also the business would be using the new calculated measure in excel using the Slicer, they want to select any date and be able to view the MTD value for that month.
Also can someone please help with MDX query for both methods (1) and (2) ?
Your help much appreciated.
I think the problem with your quoted code (method 2) is that [Date].[Financial].CurrentMember must be at a level at or below the [Date].[Financial].[Month] level. Otherwise PeriodsToDate returns an empty set.
So the problem is not in your calculated member definition, but somewhere in the query in which it's being used. In that query, [Date].[Financial].CurrentMember may be returning a member at a level above Months. Hard to see without seeing the query itself.
Method (1) is more fiddly. You can get the last day of the previous month with
Ancestor([Date].[Financial].CurrentMember,[Date].[Financial].[Month]).PrevMember.LastChild
but you'd have to build in some logic for days in the first month of the financial year, which would otherwise subtract the value for a previous fin year from this year's YTD value. So I'd recommend method (2).
As far as I know selecting a date (I mean a date, not a month) in the Excel slicer will make that date the .CurrentMember. I'm a bit hesitant because Excel does generate some deeply bizarre MDX sometimes.
EDIT: Another possible problem is a hierarchy mismatch. You can select a perfectly good Day in a date hierarchy, but if it isn't in exactly the hierarchy you specify in your calculated member definition, you can get weird results. IMHO more recent versions of SSAS encourage a proliferation of attribute hierarchies and multiple "real" hierarchies, making this a real problem.
As a test can you please add this very simple measure to make sure that currentmember is behaving as expected:
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS [Date].[Financial].CurrentMember.member_caption
If when you use the above all it returns is the All member then you know something is wrong.
You need to double-check your relationships and datatypes used within your date hierarchies as this is often the reason for time calculation problems.
This is an alternative to your measure but I suspect if the original script is not working then neither will this...
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS SUM
(
MTD([Date].[Financial].CurrentMember)
, [Measures].[AUD DLY]
)

MDX to calculate difference between sales amount between 2 dates

I have a Cube with a Date dimension hierarchy (Calendar,Year,Month,Week,Day) and Measure as SalesData, would like to add a calculated measure which would give me the SalesData for the selected date and the last date of the Previous Month.
Calendar Hierarchy is as follows
Date - Dimension
Calendar - Hierarchy
Year - [Date].[Calendar].[Year]
Quarter - [Date].[Calendar].[Quarter]
Month - [Date].[Calendar].[Month]
Week - [Date].[Calendar].[Week]
Day - [Date].[Calendar].[Day]
Measures
[Sales]
New Calculated Member need to be created - say [SalesMTD]
Requirement is when a user select any date say 3 March 2016, the calculated member should give Sales as follows
[SalesMTD] = [Sales] on 3 March 2016 - [Sales] on 29 Feb 2016
Can someone please help me write an MDX query for the Calculated Measure ?
Your help much appreciated.
Please try the below code.
CREATE MEMBER CURRENTCUBE.[Measures].[Sales Last Month]
AS ([Date].[Months].CURRENTMEMBER.PREVMEMBER, [Measures].[sales])
, VISIBLE = 1 ;
Then write another calculation using the above calculations
CREATE MEMBER CURRENTCUBE.[Measures].[Sales Difference]
AS ([Measures].[Revenue]-[Measures].[Sales Last Month])
, VISIBLE = 1 ;
with member measures.SalesDataLastDaylasMonth
as
[Date].[Calendar].currentmember.firstsibling.lag(1)
//or [Date].[Calendar].currentmember.firstsibling.prevmember
select {measures.SalesData, measures.SalesDataLastDaylasMonth} on 0,
[Date].[Calendar].[Day].members on 1
from [Some Cube]
Here currentmember.firstsibling.prevmember fetches the member prior to the first day in the list of days in the current month.
Also, you can obviously create this member in a cube instead of having it query scoped like this. The syntax would be similar to the above answer.
Based upon your edit
There are multiple ways of getting to it. Below are some:
with member Measures.[SalesMTD]
as
Measures.[Sales]
-
(Measures.[Sales],Ancestor([Date].[Calendar].currentmember, [Date].[Calendar].[Month]).firstchild.firstchild)
//(Measures.[Sales],[Date].[Calendar].currentmember.parent.parent.firstchild.firstchild.lag(1))
//(Measures.[Sales],[Date].[Calendar].currentmember.parent.parent.firstchild.firstchild.prevmember)
select Measures.[SalesMTD] on 0,
[Date].[Calendar].[Day].members on 1
from [Some Cube]
The first approach is the neatest.

PPS 2010 YTD vs. calculated measure aggregation fails (with pics)

I have 2 measures:
Sales Value
Sales Value LY (calculated measure)
and a time filter with single month member and YTD formula.
If i make simple PerformancePoint report like so:
It shows up correctly like this when single month is selected from the time filter:
But when i try the YTD formula from the time filter:
the calculated measure is lost from the report.
Any ideas?
I would try to change the ParallelPeriod(Month, 12) to ParallelPeriod(Year, 1) in your measure definition:
scope ({Measures.[Sales Value LY]});
this = (PARALLELPERIOD(
[Time].[Year - Month - Date].[Year],
1,
[Time].[Year - Month - Date].CURRENTMEMBER
),
[Measures].[SALES AMOUNT]);
end scope;
ParallelPeriod only works properly if the current period is below or at the level stated as the first argument.
EDIT
Maybe changing the expression like proposed at http://sqlblog.com/blogs/mosha/archive/2007/01/13/multiselect-friendly-mdx-for-calculations-looking-at-current-coordinate.aspx would help:
scope ({Measures.[Sales Value LY]});
this = Sum(EXISTING [Time].[Year - Month - Date].[Day].Members,
(PARALLELPERIOD(
[Time].[Year - Month - Date].[Year],
1,
[Time].[Year - Month - Date].CURRENTMEMBER
),
[Measures].[SALES AMOUNT]
)
);
end scope;
I was guessing the name of your day level, maybe you have to adapt that.
Have a workaround thus:
built-in YTD semiadditive function in PPS time intelligence can be written also like this:
year.firstmonth.parent:year.month.parent
Dunno if the YTD function is buggy or whatnot, but this was the quick way to get the parallel YTD values..

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.