MDX - Add Calculated Percent Measure into a Range - ssas

i didn't found any solution for my problem. Or maybe i dont know the right keywords.
Problem:
Cube contains a calculated member measure:
CREATE MEMBER CURRENTCUBE.[Measures].[Percentage] as
iif([Measures].[Performance GB]=0, null,
[Measures].[Performance GB]/[Measures].[Package GB]),
FORMAT_STRING = "0#.#0 %;-#.#0 %",
VISIBLE = 1;
Now i need another attribute to group the percent values into a range.
My first idea was this member with 4 additional measures. For example:
create MEMBER CURRENTCUBE.[Measures].[0-25%] AS
IIF(
[Measures].[Percentage]>0 AND [Measures].[Percentage]<26
,1
,NULL),VISIBLE = 1;
It works if i show the values on the contract level.
But a deeper watch shows wrong values:
CubeSelection
Whats my error?

Can you try changing 26 to 0.26?
create MEMBER CURRENTCUBE.[Measures].[0-25%] AS
IIF(
[Measures].[Percentage]>0 AND [Measures].[Percentage]<0.26
,1
,NULL),VISIBLE = 1;

My first idea was the wrong way.
The solution is a calucalted field to generate the DimensionKey
when (PerformanceGB/ServiceGB)*100 between 0 and 11 then 1
when (PerformanceGB/ServiceGB)*100 between 11 and 21 then 2
....
And then i could use the PercentRange as a Dimension

Related

SSAS Calculated Member for measures that have the same Dimension

I am trying to create a calculated measure that subtracts measures that are in different groups, but only if they have the same dimension member whatever that may be. (edit - essentially exclude UnknownMember numbers in the subtraction)
I have tried using Scope:
CREATE MEMBER CURRENTCUBE.[Measures].[CalcField] as NULL;
SCOPE([Dimension1].[DimensionField1].MEMBERS);
[Measures].[CalcField] = [Measures].[a] - [Measures].[b];
END SCOPE;
I have also tried using a Tuple without success:
CREATE MEMBER CURRENTCUBE.[Measures].[CalcField]
AS ([Measures].[a] - [Measures].[b], [Dimension1].[DimensionField1]);
But I think there is something fundamental that I am missing
Edit
col a b dimension
1. 9 0 x
2. 0 2 x
3. 1 5 null
If you aggregate those rows I want the answer 7 not 5
Using a simple CASE WHEN works if the dimensions in use, but otherwise it blindly subtracts everything again
CASE
WHEN [Dimension1].[DimensionField1] IS [Dimension1].[DimensionField1].UnknownMember THEN 0
ELSE [Measures].[a] - [Measures].[b]
END
Using Aggregate works at the high level, but then when I use the Dimension I get no per member results
Aggregate(
EXCEPT(
[Dimension1].[DimensionField1].Members, {[Dimension1].[DimensionField1].UnknownMember, [Dimension1].[DimensionField1].[All]}
),[Measures].[a])
-
Aggregate(
EXCEPT(
[Dimension1].[DimensionField1].Members, {[Dimension1].[DimensionField1].UnknownMember, [Dimension1].[DimensionField1].[All]}
),[Measures].[b])
Workaround Solution
I am sorry if I was not clear on my problem, but I have ended up solving this problem by putting the data I need in at the ETL stage rather than calculating it in the Cube
Did you try either Summing or Aggregating your second snippet?
CREATE MEMBER CURRENTCUBE.[Measures].[CalcField]
AS
Sum(
[Measures].[a] - [Measures].[b],
[Dimension1].[DimensionField1]
);
CREATE MEMBER CURRENTCUBE.[Measures].[CalcField]
AS
Aggregate(
[Measures].[a] - [Measures].[b],
[Dimension1].[DimensionField1]
);
With Member [Measures].[CalcField] as [Measures].[a] - [Measures].[b]
Select [Measures].[CalcField]
on columns,
[Dimension1].[DimensionField1].MEMBERS
on rows
from [cube]

Calculated SSAS Member based on multiple dimension attributes

I'm attempting to create a new Calculated Measure that is based on 2 different attributes. I can query the data directly to see that the values are there, but when I create the Calculated Member, it always returns null.
Here is what I have so far:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
Filter([Expense].MEMBERS, [Expense].[Amount Category] = "OS"
AND ([Expense].[Account Number] >= 51000
AND [Expense].[Account Number] < 52000))
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
Ultimately, I need to repeat this same pattern many times. A particular accounting "type" (Absorption, Selling & Marketing, Adminstrative, R&D, etc.) is based on a combination of the Category and a range of Account Numbers.
I've tried several combinations of Sum, Aggregate, Filter, IIF, etc. with no luck, the value is always null.
However, if I don't use Filter and just create a Tuple with 2 values, it does give me the data I'd expect, like this:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
{( [Expense].[Amount Category].&[OS], [Expense].[Account Number].&[51400] )}
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
But, I need to specify multiple account numbers, not just one.
In general, you should only use the FILTER function when you need to filter your fact table based on the value of some measure (for instance, all Sales Orders where Sales Amount > 10.000). It is not intended to filter members based on dimension properties (although it could probably work, but the performance would likely suffer).
If you want to filter by members of one or more dimension attributes, use tuples and sets to express the filtering:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS
Sum(
{[Expense].[Account Number].&[51000]:[Expense].[Account Number].&[52000].lag(1)} *
[Expense].[Amount Category].&[OS],
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
Here, I've used the range operator : to construct a set consisting of all [Account Number] members greater than or equal to 51000 and less than 52000. I then cross-join * this set with the relevant [Amount Category] attribute, to get the relevant set of members that I want to sum my measure over.
Note that this only works if you actually have a member with the account number 51000 and 52000 in your Expense dimension (see comments).
An entirely different approach, would be to perform this logic in your ETL process. For example you could have a table of account-number ranges that map to a particular accounting type (Absorption, Selling & Marketing, etc.). You could then add a new attribute to your Expense-dimension, holding the accounting type for each account, and populate it using dynamic SQL and the aforementioned mapping table.
I don't go near cube scripts but do you not need to create some context via the currentmember function and also return some values for correct evaluation against the inequality operators (e.g.>) via the use of say the membervalue function ?
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
[Expense].[Amount Category].&[OS]
*
Filter(
[Expense].[Account Number].MEMBERS,
[Expense].[Account Number].currentmember.membervalue >= 51000
AND
[Expense].[Account Number].currentmember.membervalue < 52000
)
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
EDIT
Dan has used the range operator :. Please make sure your hierarchy is ordered correctly and that the members you use with this operator actually exist. If they do not exist then they will be evaluated as null:
Against the AdvWks cube:
SELECT
{} ON 0
,{
[Date].[Calendar].[Month].&[2008]&[4]
:
[Date].[Calendar].[Month].&[2009]&[2]
} ON 1
FROM [Adventure Works];
Returns the following:
If the left hand member does not exist in the cube then it is evaluated as null and therefore open ended on that side:
SELECT
{} ON 0
,{
[Date].[Calendar].[Month].&[2008]&[4]
:
[Date].[Calendar].[Month].&[1066]&[2] //<<year 1066 obviously not in our cube
} ON 1
FROM [Adventure Works];
Returns:

MDX show all sales until now

i have a huge table of cashflows that means there are +int values for income and -int values for outcome.
I have MeasureGroup for Sum the amount of money.
I now want to display not only the sum of money per month but also the sum of all the past time until the current month so like that:
Month MoneyAmount Total
1 20 20
2 -10 10
3 5 15
4 -10 5
So i know for the first part its just like
select [Measures].[Money] on 0,
[Date].[Month].Members on 1
From MyCube
but how can i add the sum column?
i thought about something like SUM( { NULL : [Date].[Month].CurrentMember } , [Measures].[Money] ) but that didnt work as well :(
In MDX, the total is already there. You do not have to do complex calculations to get it.
But it depends on your exact hierarchy structure how the All member is called. If you have a date user hierarchy named [Date].[Date], and it has a month level named [Date].[Date].[Month], then the all member of the hierarchy would probably be called something like [Date].[Date].[All]. If [Month] is an attribute hierarchy of the Date dimension, then the "all member" would probably be called [Date].[Month].[All]. In the latter case, the all member would already be the first member of the set [Date].[Month].Members. As you are asking the question, I am assuming this is not the case, and you are using a user hierarchy. Then you could change your MDX query to
select [Measures].[Money] on 0,
Union([Date].[Month].Members, { [Date].[Date].[All] }) on 1
From MyCube
Please note that you can change the name of the All member in the property settings of a dimension when designing an Analysis Services dimension, hence I cannot know the definitive name without knowing the details of this setting in your cube. So you might have to adapt the name of the all member.
You can find this name out in SQL Server Management Studio in an MDX window as follows: open the hierarchy that you are using, and then open the "Members" node, below which you should find the "All Member". You can drag this into your MDX statement, and the proper name will appear there.
As in a running sum?
You need a calculated measure, like this:
With Member [Measures].[Running Sum] as Sum( ( [Date].[Months].Members.Item(0) : [Date].[Months].CurrentMember ), [Measures].[Money])
Select [Date].[Months].Members on Rows,
{[Measures].[Money], [Measures].[Running Sum] } on Columns
From [MyCube]

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.

MDX date range query with a missing boundry date

I need an MDX query for Mondrian filtered by date, where one or both of the boundry dates may not exist. I'm using the query below that works as long as both 2013-01-01 and 2013-01-08 dimensions exist. If one of the two dates does not exist then it returns no results, even though the dimensions in between do exist. How would I get this query to work even in the case of a missing boundry date dimension?
SELECT
NON EMPTY {Hierarchize({[Measures].[Number of Something]})} ON COLUMNS,
NON EMPTY {[Date].[2013-01-01]:[Date].[2013-01-08]} ON ROWS
FROM [Users]
MDX is built with the assumption that every member that you refer to exists; it is best then to make sure all conceivable date dimension members do exist by having a separate table with these values precomputed.
You could get tricky and implement that table as a stored procedure but date dimensions don't take up a lot of space in the grand scheme of things so you'd hardly ever do this.
I don't know of any other way to solve your problem.
try to eliminate the NON EMPTY
Even I haven't yet understand the reason of implementing this logic, you can hide this by adding . If you add custom member in Mondrian try it.
/* Exclude Missing Member */
Create Set CurrentCube.[MissingMemberSet] As
iif(IsError(StrToMember("[Dimension].[Hierarchy].&[MEMBER]")),
{}, {[Dimension].[Hierarchy].&[MEMBER]});
Create Member CurrentCube.Measures.[Calculation on Missing Member]
AS
IIF ([MissingMemberSet].Count > 0,
([Dimension].[Hierarchy].&[MEMBER],Measures.[X Measure]),
0
)
,
FORMAT_STRING = "Currency",
LANGUAGE = 1033,
NON_EMPTY_BEHAVIOR = { [X Measure] },
VISIBLE = 1 , DISPLAY_FOLDER = 'Display Folder' ;
Also you can implement in using IIF(IsError or IIF(Exists MDX functions.