MDX and PeriodsToDate - ssas

I'm very new to MDX so I hope this is a simple question for a MDX expert. I have the following expression SUM(PeriodsToDate([Accounting Date].[(All)]),[Measures].[Amount]) which this piece works as expected. I need to add a filter or IIF statement that basically does the following:
IF
SUM(PeriodsToDate([Accounting Date].[(All)]),[Measures].[Amount]) < 0
Then 0
Else
SUM(PeriodsToDate([Accounting Date].[(All)]),[Measures].[Amount])

I'm afraid it's impossible to do with pre-aggregated data. The MDX IIF function works per a member, the SQL IIF works per a row. You have to add a measure like Amount, but with iif([Amount] < 0,0,[Amount]). Also you may try the code below, nonetheless, I doubt it results properly:
SUM(
NULL:[Accounting Date].[Calendar].CurrentMember,
IIF(
[Measures].[Amount] < 0,
NULL,
[Measures].[Amount]
)
)

Related

Getting results for even years only

MDX has a nice feature whereby I can specify a range of members:
SELECT ([Canada],[2006]:[2011]) on Rows,
[Measures].members on Columns
FROM [Sales]
Is there a way to calculate the set of even years: {[2006], [2008], [2010]}? I am looking for a way that would work for large sets, so that listing the years manually is not an option.
You can filter you function using a filter function, a declared function and MOD function (MOD returns the remainder from the division - like % in Java ) :
WITH
FUNCTION isEven(Value _number) as Mod( Int(_number) , 2 ) = 0
SELECT
FILTER( [Date].[Date].[Year] as t, isEven( Year( t.current.key) ) ) on 0
FROM [Cube]
If you are using this filter often you could create a FilterEven declared function once in the script (same for isEven() )
Try this. I used adventure works for the query.For the mod logic i took help from
Mod Logic
WITH
MEMBER [Measures].[Data Type] AS
[Date].[Day of Year].CurrentMember.Properties ("Member_Value",TYPED)
MEMBER [Measures].[IsEven] as
[Measures].[Data Type]-Int([Measures].[Data Type]/2)*2
select {[Measures].[Internet Order Count] }
on columns,
filter (
[Date].[Day of Year].[Day of Year],
[Measures].[IsEven]=0)
on rows
from [Adventure Works]
Plus you can have a column in the date dimension have 1,0 to indicate if the year is even or odd. Then simply use that column in the MDX query , no need to do all the above manipulations

SSAS Grand Total for calculated measure in excel not working

I have an ssas cube with a calculated member called cumulative ptp's.
Everything seems to work in excel except for the Grand Total which is displaying #Value!. I'm not sure where the problem is. I've added a few pics of what I have done. Any help will be most appreciated. Thanks in advance.
IIF
(
[Day-Month Analysis].[Day].FIRSTCHILD = [Day-Month Analysis]. [Day].CURRENTMEMBER,
Sum(
{[Day-Month Analysis].[Day].FIRSTCHILD:[Day-Month Analysis]. [Day].CURRENTMEMBER},
[Measures].[Sum of IsPTP]
)
, SUM(Measures.[Sum of IsPTP])
)
So I downloaded that extension and the MDX error is:
"Range operator (:) operands' have different levels; they must be the same"
Try this:
Sum(
{[Null:[Day-Month Analysis].[Day].CURRENTMEMBER},
[Measures].[Sum of IsPTP]
)
I don't think you need the IIf. If that doesn't work right describe what's in that [Day] hierarchy further.

MDX Error while creating Calculated Measure

I am trying to create a calculated measure that finds the difference between two measures by using the following mdx query
WITH MEMBER [Measures].[Available]
AS ([Measures].[Capacity days] ,[Project].[Projects by Name].[All],[Project].[Projects by Code].[All])
- ([Measures].[Worked Days] ,EXCEPT([Project].[Projects by Name].[Project].MEMBERS,
[Project].[Projects by Name].[Project].&[1214]),[Version].[Version].[Combined],[Charge].[Charge].[All])
In case of second measure Worked Days I want to access it with respect to all projects except one ,so am using EXCEPT function which results in the following error
" The function expects a string or numeric expression for the argument. A tuple set expression was used"
Is there any other way to perform this operation?
The query is mixing tuples with sets. Perhaps you can check this gentle introduction of MDX for main concepts and notations.
The second tuple is using a set (the result of EXCEPT) as its second member which is not possible. You could use the aggregate function as following to compute the [Worked Days] over the members of this set instead :
AS ( [Measures].[Capacity days], ... )
- Aggregate(
Except (
[Project].[Projects by Name].[Project].MEMBERS,
[Project].[Projects by Name].[Project].&[1214]
),
( [Measures].[Worked Days], ... )
)

Mdx Iif statment

I'm trying to get calculated measure while evaluating Pentaho Analyser.
Point: Need a measure with condition. My code:
IIf ([Product].CurrentMember Is [Product].[Food], [Unit Sales], 0)
So I get:
Drinks 0
Food 14
Else 0
End it works only until [Product] dimension is present on rows or columns.
But when I insert another slicer, and remove [Product] ( [Partner] for example) I get:
IBM 0
MS 0
Apple 0
Instead of:
IBM 7
MS 2
Apple 5
I know, the main problem is CurrentMember property, and it's always zero because there is on CurrentMember on given dimension. Please, do not answer with: 'Filter it on "Food"'
because I'm trying to build something more complex and this is a first step.
Is there any trick to get this Measure work?
EDIT:
I have 3 dimensions: Product, Partner, Country
I need to get via mdx formula calculated measure:
[Unit Sales] when it is [Product].[Food] and [Product].[Drink] and it NOT is [Country].[US] and [Country].[DE] but everything else.
Thanks,
Best Regards
I am not a specialist for Pentaho/Mondrian, but I would assume the issue is that in your second scenario, the [Product].CurrentMember is the all member of the products, not the Food member, and thus the system behaves as you told it, and returns 0.
From how I understand your question, something like
IIf(
Count(
Intersect(
{ [Product].CurrentMember },
{ [Product].[Food], [Product].[Drink], [Product].[All] }
)
) = 1
AND
Count(
Intersect(
{ [Country].CurrentMember },
{ [Country].[US], [Country].[DE], [Country].[All] }
)
) = 0
[Unit Sales],
0
)
should deliver what you want. The trick here is that the intersection between the one-element set containing the CurrentMember and another set can either have one element - in case the CurrentMember is contained in the set -, or zero elements - in case the element is not contained in the set.

Filtering dimensions in MDX inside a SUM

I am new to MDX expressions and I am trying to create one that sums the value of a given measure filtered by dimensions.
In my database I have several different dimensions that have the same name: "Answer". To sum them up, I have created the query below:
WITH MEMBER Measures.Total as SUM ({[Activity].[Activity].&[14], [Activity][Activity].&[22]},
[Measures].[Activity time])
SELECT NON EMPTY [Measures].[Total] on COLUMNS from [My Analytics]
This query works, however I had to use the "&[14]" and "&[22]" statments that correspond to two different "Answer" dimensions.
Since I have more than two dimensions with the same name, is there a way to rewrite the query above in a way that I would select all these dimensions without having to add their unique ID? For example, I would re-write the query as something like this:
WITH MEMBER Measures.Total as SUM ({[Activity].[Activity].&["Answer"]},
[Measures].[Activity time])
SELECT NON EMPTY [Measures].[Total] on COLUMNS from [My Analytics]
Is this possible?
Thanks!
You can use the Filter function as following:
with
set [my-answers] as
Filter( [Activity].[Activity].members,
[Activity].[Activity].currentMember.name = 'Answer'
)
member [Measures].[Total] as Sum( [my-answers] )
...