Null values for calculated members in case statement (MDX) - ssas

i am a beginner in MDX. I want to show the number of a month January --> 1.
CREATE MEMBER CURRENTCUBE.[Measures].[Mnr]
AS CASE [Measures].[Time].[Calendar].[Month].Currentmember.Name
WHEN 'January' THEN '1'
WHEN 'February' THEN '2'
ELSE 'Unknown'
END
I get only null values and can not find my mistake.

Probably you need to replace [Measures].[Time].[Calendar].[Month].Currentmember.Name with [Time].[Calendar].CurrentMember.Name
The CurrentMember function operates on a hierarchy not a level. And Measures is wrong as the Time dimension isn’t a child of Measures.

Related

MDX: Make Measure Value 0 based on flag

Would much appreciate any help on this.
I have a measure called "Sales" populated with values, however i am trying to turn the "Sales" value to 0, whenever the "Sales Flag" is set to 0.
Important Note: The Sales Flag is based on Date (lowest level of detail).
The difficulty that i am really experiencing and cant get a grip on, is how i am trying the display the MDX outcome.
As explained above, i would want to make the "Sales" value 0 whenever we have a 0 in the "Sales Flag" (which is based on the Date), but when I run the MDX Script I would wan't on the ROWS to NOT display the Date, but instead just the Week (higher Level to Date), as below shows:
I really have spent hours on this and can't seem to understand how we can create this needed custom Sales measure based on the Sales Flag on the date level, but having the MDX outcome display ROWS on Week level.
Laz
You need to define the member in the MDX before the select. Something like that:
WITH MEMBER [Measures].[Fixed Sales] as IIF([Sales Flag].currentMember=1,[Sales], 0)
SELECT [Measures].[Fixed Sales] on 0, [Sales Flag] on 1 from [Cube]
I am writing the code without SSAS here so it might not be the 100% correct syntax but you can get the general idea ;)
You can add the iif in the SELECT part but I find creating member to be the cleaner solution.
SELECT IIF([Sales Flag].currentMember=1,[Sales], 0) on 0, [Sales Flag] on 1 from [Cube]
If you have a control over the cube in SSAS you can create a calculated member there and you can access it easier.
Glad to hear if Veselin's answer works for you, but if not...
Several approaches are also possible.
Use Measure expression for Sales measure:
Use SCOPE command for Day level (if it's Key level of Date dimension). If it's not a key level you have to aggregate on EVERY level (week, year etc) to emulate AggregateFunction of Sales measure but with updated behavior for one flag:
SCOPE([Date].[Your Date Hierarchy].[Day].members,[Measures].[Sales]);
THIS=IIF([Sales Flag].CurrentMember = 1,[Measures].[Sales],0);
END SCOPE;
Update logic in DSV to multiply Sales column by SalesFlag. This is the easiest way from T-SQL perspective.

AVG(EXISTING()) syntax error

I want to calculate sales for promotion using it's date. I need 3 measures, avg sales from 21 days before promotion start date, sales in between of promotion's start and end date, and sales from 21 days after promotion's end date.
Why Visual Studio highlights avg in code below?
CREATE MEMBER CURRENTCUBE.[Measures].[Sales in promotion]
AS Avg(Existing([Promotion].[Promotion name].[Promotion name]),[Measures].[Sales]), ...
Same in here:
CREATE MEMBER CURRENTCUBE.[Measures].[Sales before promotion]
AS (EXISTING([Promotion].[Promotion name].[Promotion name]), AVG(strtomember("[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"]").lag(21) : strtomember("[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"),
[Measures].[Sales])) ...
If I do sum(existing()) in first measure, the sum is calculated correctly, but it doesn't allow me to get average.
EXISTING will only help if [Promotion] is part of your query in either the WHERE or SELECT clause. If it is not included in either of these clause then EXISTING will be finding 1 member - the All member.
You could try NonEmpty and maybe move the period logic into a custom set?
WITH
SET [PERIOD] AS
STRTOSET(
"[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"].lag(21)
:
[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"
)
From the code you posted I cannot tell if you want a daily average or and average per promotion ? Say there were 2 promotions over the 21 days does this mean you want (Total/2/21) ?

Create MDX to Divide two measures for each month and then sum the all of the months

I have a multidimensional cube that needs a custom measure that I'm not sure how to build.
That data looks like this:
MemberID-----Date-------EventType
1--------------1/1/2016-------1
2--------------1/1/2016-------2
3--------------2/1/2016-------1
2--------------2/1/2016-------2
4--------------2/1/2016-------2
There is a count measure in the cube, but others can be added if needed. I need to create a measure that will use whatever filters the user applies and then count the EventType (1 and 2 only) by month, divide the resulting counts for EventType 1 into the count for EventType 2 (for each month individually), and finally sum the monthly results. For example 1/1/2016 would be 1/1=1 (count of EventType 1 and count of EventType 2) and 2/1/2016 would be 1/2=0.5 so the resulting measure value for the two months would be 1+0.5=1.5. Any help is greatly appreciated.
Let's assume you have a Date dimension with an attribute called Month. And let's assume you have an EventType dimension. And let's assume you have a count measure in your measure group called Cnt. Here's what else you need to do.
First, go to the DSV and add a new calculated column to the fact table which is called NullInt and is the following expression:
cast(null as int)
Then create a new Sum measure in your measure group off that column and call the measure My Rollup. Under the Source property, change NullHandling to Preserve so that it will start off null.
To explain why we're doing this, a scoped assignment to a physical measure will aggregate up. (If you assign a value to a physical measure at the grain of each month, then it will rollup to the grand total.) But a scoped assignment to a calculated measure doesn't roll up.
Then in your MDX script add the following calculations:
scope([Date].[Month].[Month].Members); //calculate at the month level then rollup
[Measures].[My Rollup] = DIVIDE(
([Event Type].[Event Type].&[1],[Measures].[Cnt]),
([Event Type].[Event Type].&[2],[Measures].[Cnt])
);
end scope;
Note that your version of SSAS probably has the DIVIDE function if it's AS2012 with the latest service pack or newer. But if it doesn't, you can always do division the old fashioned way as IIF(denom=0,null,num/denom).

How to compare two dates from different dimensions in the Cube MDX

I need to create a calculation or KPI (I am not sure what exactly) which helps to compare to dates from different dimensions.
I have a cube and I need run a report when I pick up Course date and Absence date for a student if the dates are the same then a value should be missing.
This is a SQL query which is perfectly working but I need to implement it in a cube:
case when AbsenceDate=CourseDate then'missing'
else 'not the same date' End as 'date info'
If I was writing a script and needed the measure, it would probably look vaguely like this:
WITH
MEMBER [Measures].[date info] AS
IIF(
[AbsenceDate].CURRENTMEMBER.MEMBER_CAPTION
= [CourseDate].CURRENTMEMBER.MEMBER_CAPTION
,NULL
,'not the same date'
)
SELECT
...
...

MDX LastPeriods for Rolling Count Null values

I have created a calculated member in my cube to figure out the event count for a 12 month rolling period. I have a measure called Event Count I have used the LastPeriods function to do this. However, I am having a problem when I have a month that has not got any data, my Measure [Event Count] is showing null and my calculated member shows null as well - I really need the 12 month rolling count to continue, replacing the Null for a 0 in the count so I would get a value for the month if I got a 0.
So if jan event Count was 5 and Feb was Null I should still all the previous 11 months worth of data added together with so the rolling count would still be 5
I have the following MDX as my calculated member and I still get nulls. I have tried doing a CASE statement as well.
IIF (isEmpty([Measures].[Event Count]) ,
Sum(LastPeriods(12,[Event Date].[CalendarYQMD].CurrentMember),0),
Sum(LastPeriods(12,[Event Date].[CalendarYQMD].CurrentMember),[Measures].[Event Count])
has anyone came across this problem before or can see where I am going wrong
Firstly, you explicitly state in your expression that the result should be zero and not the event count if the current period has no event count, as then you sum 0 and not the event count measure. hence, the formula should be
Sum(LastPeriods(12,[Event Date].[CalendarYQMD].CurrentMember),[Measures].[Event Count])
without the IIf(IsEmpty()).
You could then set the value to zero after summing, by nesting the result into CoalesceEmpty(), i. e.
CoalesceEmpty(
Sum(
LastPeriods(12,[Event Date].[CalendarYQMD].CurrentMember),
[Measures].[Event Count]
),
0
)
Secondly, the formula as stated only works correctly for months. For quarters, it would sum 12 quarters, and for years it would sum twelve years. Instead of using LastPeriods, I would suggest using ParallelPeriod to go back one year, independent of the level of teh period hierarchy on which you are, and then from there go one member forward to get the start period of the sum, i. e. use the following formula:
CoalesceEmpty(
Sum(
ParallelPeriod(
[Event Date].[CalendarYQMD].[Year Name],
1,
[Event Date].[CalendarYQMD].CurrentMember
).NextMember
:
[Event Date].[CalendarYQMD].CurrentMember,
[Measures].[Event Count]
),
0
)
Please note that the colon operator creates a range of members beginning with its left operand and ending with its right operand.