Exclude members from existing Set using MDX Query - ssas

I need an MDX to eliminate some of the members in existing set, my scenario is as below
I have dimension which has n-level parent child hierarchy and I am using that dimension in PPS dashboard. When user selects all children along with the parent then I should not display children and i should only display parent and this is applicable for any level.
Below is the example, assuming that Company is my dimension and it has HR and Finance departments and sub-departments under the main departments (I added primary key of each dept in Parenthesis).
Company (1)
HR (2)
HR Sub 1 (3)
HR Sub 2 (4)
Finance (5)
Finance Sub 1 (6)
Finance Sub 2 (7)
I am using the above dimension as "Multi Select Tree" in my PPS so user can select HR, HR Sub 1, HR Sub 2 at a time. When selects all child under parent then I need to exclude the children.
Below is the logic to achieve it
First get parent for each member and if the member parent is exists in the selected set then ignore that member. To achieve this I wrote a MDX and I am able to find the Parent of each member but I don’t know how to check whether the parent exists in the set or not.
below is the MDX I wrote
WITH SET DeptSet AS '{[Company].[Dept].&[2], [Company].[Dept].&[3],[Company].[Dept].&4], [Company].[Dept].&[6]}'
MEMBER Measures.ParentNode as [Company].Dept].CURRENTMEMBER.PARENT.UNIQUENAME
SELECT [Measures].[EmpCount] on ROWS, {Need Trimmed Member Set Here} on COLUMNS FROM [CUBE]
In the above MDX "DeptSet" is the set which I receive from PPS and after implementing the above logic I need to get the set as below
{[Company].[Dept].&[2], [Company].[Dept].&[6]}
Thanks in advance.

Use below MDX to get it done
[DeptSet] - Filter([DeptSet], Count([DeptSet].CurrentMember.Parent - [DeptSet])=0)
in the place of {Need Trimmed Member Set Here}
Look at below link for more details
http://social.msdn.microsoft.com/Forums/en/sqlanalysisservices/thread/a020c70c-2986-4f2c-8f4f-366e8be06c2d

Related

MDX: count number of members selected in filter

I have one dimension that I want to put into filter, and created calculated member that should dynamically show number of selected members from the dimension.
The dimension does not have an All member.
So this is my attempt
with member [Measures].[Count1] as count(existing(([MyDimension].[MyDimensionHierarchy].members)))
select [Measures].[Count1] on 0
from [MyCube] -- gives me 1
and this one will give me 2 which is correct:
with member [Measures].[Count1] as count(existing(([MyDimension].[MyDimensionHierarchy].members)))
select [Measures].[Count1] on 0
from [MyCube]
where ({[MyDimension].[MyDimensionHierarchy].[Member1], [MyDimension].[MyDimensionHierarchy].[Member2]})
But, the problem is that when I create calculated member with the formula above, and drag Count1 to the Excel pivot table, and drag MyDimension as filter, and when I do multi-select of the dimension members, I want the count to dynamically change as I change number of members that are selected.
But Count1 always stays equal to 1.
In a meantime I have found an answer:
The query that I wrote in the question actually is not the query that Excel pivot table sends to the cube. Excel pivot table generates query like this:
SELECT FROM (SELECT ({[MyDimension].[MyDimensionHierarchy].[Member1],[MyDimension].[MyDimensionHierarchy].[Member2]}) ON COLUMNS
FROM [MyCube])
WHERE ([Measures].[Count1])
The way this should be done is by using dynamic set that contains filtered members:
create dynamic set [SelectedMembers] as existing( [MyDimension].[MyDimensionHierarchy].members )
And then:
create member Measures.SelectedMembersCount as count([SelectedMembers])
So this set dynamically changes as different members are selected in the filter and SelectedMembersCount is dynamically changed along the way.

Aggregate data in a SSAS hierarchy

I have a hierarchy in a dimension in my cube, and a fact table that references that dimension. Let's call that a hierarchy of departments and the fact table contains billing targets set for each department (on a record-per-day basis if that's of any interest).
The data is aggregated as in a usual hierarchy in a multidimensional cube.
The problem is that the data in the fact table is not just limited to the "leaf" departments in the hierarchy tree, and so each node/department may or may not have an actual value for a billing target.
I need to have my measure returning the actual value for any node if there is a value, or the sum of the values of it's descendants (if they themselves have actual values, then ignore their descendants).
I can return the value at any given node by using the .DATAMEMBER property and add a condition to substitute the null (if that is the case) with the sum of descendants as so:
with member DirectD as (iif(([Department].[Departments].currentmember.datamember, [Measures].[Department Billing Target]) = null,
([Measures].[Department Billing Target]),
([Department].[Departments].currentmember.datamember, [Measures].[Department Billing Target])))
The problem is that I cannot ensure that the same logic applies to every node "down" the hierarchy.
The MembersWithData setting is set to NonLeafDataHidden in the dimension.
[Measures].[Department Billing Target] is a calculated member, so it looks like Aggregate is not an option.
As an example, I'd like to get the value of 3000 if I query the billing target for department A (image 1):
For department C on Image 2 I need to get 1400 (E, while having descendants, has an actual value which takes precedence over the sum of it's children).
Any help would be greatly appreciated.
The Hierarchy that you mention seems to be a parentchild hierarchy since you say "The problem is that the data in the fact table is not just limited to the "leaf" departments in the hierarchy tree". Based on this, the problem is that we need to solve is
1)Foreach member of you hierarchy we call the base attribute.
2)If the measure value for the base attribute is null we retuen the summed up value
3)If the measure value for the base attribute is not null then we retuen the indivisual members value. Below is the code piece, there can be some syntax issues but would explain the idea.
The below code is a similar example on adventure works(I did add FullName attribute in the employee dimension). In the picture on right is a parent child hierarchy's, base attribute. This picture show all the employee who had sales. On the left is the hierarchy, notice how the values change in the Test measure for people who are present on the right hand side. For example take a look at "Amy E. Alberts"
The code is
with member [Measures].test as
case when [Employee].[Employees].currentmember.name='All Employees'
then [Measures].[Reseller Sales Amount]
when (strtomember('[Employee].[Full Name].['+[Employee].[Employees].currentmember.name+']'),[Measures].[Reseller Sales Amount])=null
then [Measures].[Reseller Sales Amount]
else (strtomember('[Employee].[Full Name].['+[Employee].[Employees].currentmember.name+']'),[Measures].[Reseller Sales Amount])
end
select {[Measures].[Reseller Sales Amount],[Measures].test}
on columns,
[Employee].[Employees].members
on rows from [Adventure Works]
In the end, I have added the following solution that is working so far:
IIF(NOT ISEMPTY(([Department].[Departments].CURRENTMEMBER.DATABEMBER, [Measures].[Department Billing Target Canonical])),
([Department].[Departments].CURRENTMEMBER.DATABEMBER, [Measures].[Department Billing Target Canonical]),
IIF(ISEMPTY(SUM(descendants([Department].[Departments].CURRENTMEMBER, [Department].[Departments], AFTER), IIF(
ISEMPTY(([Department].[Departments].CURRENTMEMBER.parent.DATABEMBER, [Measures].[Department Billing Target Canonical])),
([Department].[Departments].CURRENTMEMBER.DATABEMBER, [Measures].[Department Billing Target Canonical]),
NULL))),
([Department].[Departments].CURRENTMEMBER.DATABEMBER, [Measures].[Department Billing Target Canonical]),
SUM(descendants([Department].[Departments].CURRENTMEMBER, [Department].[Departments], AFTER), IIF(
ISEMPTY(([Department].[Departments].CURRENTMEMBER.parent.DATABEMBER, [Measures].[Department Billing Target Canonical])),
([Department].[Departments].CURRENTMEMBER.DATABEMBER, [Measures].[Department Billing Target Canonical]),
NULL))))
Granted it may not be the prettiest solution (in fact I'm pretty sure it's one of the ugliest), but it works for me.
The gist of it is that the algorithm checks if the parent node of the current one has any data (currentmember.parent.datamember) and if so, substitutes the current member's value with NULL. All those values are piled up in a set, and the sum of it's "contents" is the resulting value. The outmost IIF is there to cover a sort of an edge case.
EDIT: There is also a feeling that this whole issue is just a matter of some SSAS setting that has to be changed. This setting continues to elude me.

How to filter a dimension hierarchy based on another dimension hierarchy in mdx

I have a sender and recipient dimensions which are role playing dimensions out of employee physical table.
My fact table has sender, recipient ,messages columns.
I want to get messages sent from a employee to everyone in the company except those reporting to his manager.
I tried something like this
WITH
SET [Others] AS
Except
(
Ascendants([Recipient].[Manager])
,[Sender].[Manager].Parent
)
SELECT
[OTHERS] ON COLUMNS
,{[Measures].[Messages]} ON ROWS
FROM [cube]
WHERE
[Sender].[Manager].&[xyz];
Basic idea is..get all ascendants of the recipients of a given sender and filter those whose ascendant list consists of senders parent.
This doesn't work because I can't do except between two different dimension hierarchies.
Try making the Set more context aware via the keyword EXSITING and then use Filter to compare member_caption
WITH
MEMBER [Measures].[SenderName] AS
[Sender].CurrentMember.Member_Caption
SET [ExistingRecip] AS
(EXISTING
[Recipient].[Manager].MEMBERS)
SET [Others] AS
Filter
(
[ExistingRecip]
,
[ExistingRecip].Item(
[ExistingRecip].CurrentOrdinal - 1).Member_Caption
<>
[Measures].[SenderName]
)
SELECT
[OTHERS] ON COLUMNS
,{[Measures].[Messages]} ON ROWS
FROM [cube]
WHERE
[Sender].[Manager].&[xyz];

Slow MDX Custom Distinct Count Formula

I have a question related to creating a (more efficient) custom Distinct Count Measure using MDX.
Background
My cube has several long many to many relationship chains between Facts and Dimensions and it is important for me to be able to track which members in certain Dimensions do and do not relate to other Dimensions. As such, I have created a "Not Related" record in each of my dimension tables and set those records' ID values to -1. Then in my intermediate mapping fact tables I use the -1 ID to connect to these "Not Related" records.
The issue arises when I try to run a normal out-of-the-box distinct count on any field where the -1 members are present. In the case that a -1 member exists, the distinct count measure will return a result of 1 more than the true answer.
To solve this issue I have written the following MDX:
CREATE MEMBER CURRENTCUBE.[Measures].[Provider DCount]
AS
//Oddly enough MDX seems to require that the PID (Provider ID) field be different from both the linking field and the user sliceable field.
SUM( [Providers].[PID Used For MDX].Children ,
//Don't count the 'No Related Record' item.
IIF( NOT([Providers].[PID Used For MDX].CURRENTMEMBER IS [Providers].[PID Used For MDX].&[-1])
//For some reason this seems to be necessary to calculate the Unknown Member correctly.
//The "Regular Provider DCount Measure" below is the out-of-the-box, non-MDX measure built off the same field, and is not shown in the final output.
AND [Measures].[Regular Provider DCount Measure] > 0 , 1 , NULL )
),
VISIBLE = 1 , DISPLAY_FOLDER = 'Distinct Count Measures' ;
The Issue
This MDX works and always shows the correct answer (yeah!), but it is EXTREMELY slow when users start pulling Pivot Tables with more than a few hundred cells that use this measure. For less than 100 cells, the results are nearly instantaneously. For a few thousand cells (which is not uncommon at all), the results could take up to an hour to resolve (uggghhh!).
Can anyone help show me how to write a more efficient MDX formula to accomplish this task? Your help would be GREATLY appreciated!!
Jon Oakdale
jonoakdale#hotmail.com
Jon
You can use predefined scope to nullify all unnecessary (-1) members and than create your measure.
SCOPE ([Providers].[PID Used For MDX].&[-1]
,[Measures].[Regular Provider DCount Measure]);
THIS = NULL;
END SCOPE;
CREATE MEMBER CURRENTCUBE.[Measures].[Provider DCount]
AS
SUM([Providers].[PID Used For MDX].Children
,[Measures].[Regular Provider DCount Measure]),
VISIBLE = 1;
By the way, I used in my tests [Providers].[PID Used For MDX].[All].Children construction since don't know, what is dimension / hierarchy / ALL-level in your case. It seems like [PID Used For MDX] is ALL-level and [Providers] is name of dimension and hierarchy, and HierarchyUniqueName is set to Hide.

How to create a calculated member based on two measures and the hierarchy level of the queried dimension's current member?

I have a cube which has
two measure members: [Measures].[Value] (integer) and [Measures].[EffectiveBelowLevel] (integer).
a dimension called [DimParentChild] with a ragged user hierarchy called [ParentChildHierarchy].
I would like to create a calculated member on the measures dimension ([Measures].[EffectiveValue]) based on [Measures].[Value] which when queried along [DimParentChild] and [ParentChildHierarchy] behaves as follows:
- [Measures].[Value] is used if the hierarchy level of [DimParentChild].[ParentChildHierarchy].CURRENTMEMBER > [Measures].[EffectiveBelowLevel].
- 0 is used if the hierarchy level of [DimParentChild].[ParentChildHierarchy].CURRENTMEMBER <= [Measures].[EffectiveBelowLevel].
Is it possible to achieve this functionaly with a calcuated member on the measures dimension?
If yes then what the formula would look like?
If not then what other way would there be?
I am very interested in any other kind of solution as well (e.g. an mdx query, etc.)
As an example:
[Measures]
[Value] [EffectiveBelowLevel] ParentChildAssociation
10 1 GrandChild1
20 2 GrandChild2
[DimParentChild].[ParentChildHierarchy]
Member HierarchyLevel Description
Parent 1 -
Child 2 first child of Parent
GrandChild1 3 first child of Child
GrandChild2 3 second child of Child
With this data [Measures].[EffectiveValue] should look like this
ParentChild EffectiveValue
Parent 0
Child 10
GrandChild1 10
GrandChild2 20
How about something along the lines (I'm not sure about level ordinal being 0-based):
with member xx as
Sum( [DimParentChild].[ParentChildHierarchy].currentMember as myCurrentMember,
Sum( Descendants( myCurrentMember(0), 64, LEAVES ),
IIF( myCurrentMember(0).level.ordinal > [EffectiveBelowLevel], [Value], 0 )
)
)
select [xx] on 0, [DimParentChild].[ParentChildHierarchy].members on 1 from [...]
You can have a look to this MDX documentation here for more details.
I see you have posted this question here also (saw it originally on ssas msdn forum), so I am providing the link to my answer as it might help other people. thread link on SSAS msdn forum
#Marc - As this is a case of parent child dimension and p/c dimensions can have data associated on nonleaf members your query would not return the correct results. It took me some time to figure out how to aggregate the correct results from children in this case and recommend you have a look at the link. Offtopic: good luck with your product, I hope I'll get the time to evaulate it one day :)
Regards,
Hrvoje