MDX Request : How to Topcount without specific members - mdx

I'm new to MDX and i am having troubles with the TOPCOUNT() function.
Lets say i want the following result →
This is what I get →
Here is like what my request would look like with this exemple →
WITH
SET SpecificStores AS EXCEPT([Stores].CHILDREN,[Stores].&[Store3])
SET TopProducts AS TOPCOUNT(SpecificStores * [Products],3,[Measures].[QTE])
SELECT {[Measures].[QTE]} ON COLUMNS,
{TopProducts} ON ROWS
FROM [MyCube];
The SpecificStores Member seems to work like a list and not like a unique member, is there a way to fix that ?

You defined your SpecificStores as a set: a list of all children (except the one you're excluding). The TopProducts set will be calculated based on that.
What you want is to define SpecificStores as a member:
WITH
MEMBER [Stores].[SpecificStores] AS Aggregate( EXCEPT([Stores].CHILDREN,[Stores].&[Store3]) )
SET TopProducts as TopCount( {[Stores].[SpecificStores]} * [Products].Children , 3, [Measures].[QTE])
SELECT
TopProducts on Rows,
[Measures].[QTE] on Columns
FROM [MyCube]

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.

Measure with indexable name

I'm trying to do a search using ordering and paging, but without success, I mean, it's works, but not like I was expecting
I'm doing a search like this:
WITH
MEMBER [Measures].[indexable_name] AS
[myDimension].[name].CurrentMember.UniqueName
SELECT
SubSet
(
Order
(
{
[myDimension].[id].MEMBERS
,[myDimension].[name].MEMBERS
}
,[Measures].[indexable_name]
,ASC
)
,0
,20
) ON ROWS
,{[Measures].[indexable_name]} ON COLUMNS
FROM [myCube];
that bring to me something like this
"nome" is the name of my item and indexable_name are the unique name
So, my problem is, when I'm doing the paging in my java code, I only can take half of what I want to, because I'm looking for two rows to take all the information that I need(id and name), there's some way to put all the information in one row to fix it?
You may join name of two attributes in one by an extra measure:
With
Member [Measures].[indexable_name] as
[myDimension].[name].CurrentMember.uniqueName
Member [Measures].[DimsName] as
[myDimension].[id].CurrentMember.Name + ' ' + [myDimension].[name].CurrentMember.Name
select
Subset(
Order(
{[myDimension].[id].Members,[myDimension].[name].Members},
[Measures].[indexable_name],ASC
),
0,
20
) on rows,
{[Measures].[DimsName],[Measures].[indexable_name]} on columns
from [myCube]

Using Multiple Descendants in the same crossjoin

I'm trying to use multiple members within the same dimension (in the same crossjoin) to have level 0 descendants.
I'd like it to be somehting like this:
Crossjoin(
{Descendants(
[LB_PL_HOME],[LOB].levels(0)
)
,Descendants(
[LB_PL_OTH],[LOB].levels(0)
)
},
Only I can't get the syntax to work.
Any ideas on how to do this?
This is not going to work:
Descendants(
[LB_PL_HOME]
,[LOB].levels(0)
)
Well I assume it won't work as it doesn't look like [LB_PL_HOME] and [LOB] are related - you're only allowed to find members at a specified level for a member within the same dimension. e.g. for a specified Year I would be allowed to find the members at the Day level, as Year and Day are related.
If [LB_PL_HOME] and [LOB] are from different dimensions then you could do something like this:
NonEmpty(
[LOB].levels(0).MEMBERS
,[LB_PL_HOME]
)
Your next problem is that you're not allowed to crossjoin members from the same hierarchy but you could chuck them all into the same set:
{
NonEmpty(
[LOB].levels(0).MEMBERS
,[LB_PL_HOME]
)
,NonEmpty(
[LOB].levels(0).MEMBERS
,[LB_PL_OTH]
)
}

How filter only the children members in MDX?

When I run this mdx query, works fine (get the children members from a hierarchy level):
select {} on columns,
[Dimension].[hierarchy].[level].children on rows
from [Cube]
But, when I add some tuple on rows, doesn't filter filter the children members (shows all the members) :S
select {} on columns,
[Dimension].[hierarchy].[level].children
* [Dimension2].[hierarchy2].[level2].allmembers on rows
from [Cube]
* is a cross join - you will get the Cartesian product of [Dimension].[hierarchy].[level].children and [Dimension2].[hierarchy2].[level2].allmembers because they are different dimensions.
If they were two hierarchies from the same dimension then auto exist behaviour would limit the results e.g. Year2014 crossed with month should just show the months in 2014.
Try using DESCENDANTS function + you might not require NULLs so try the NON EMPTY
SELECT
{} ON COLUMNS,
NON EMPTY
DESCENDANTS(
[Dimension].[hierarchy].[level].[PickAHigherUpMember],
[Dimension].[hierarchy].[PickTheLevelYouWantToDrillTo]
)
*
[Dimension2].[hierarchy2].[level2].allmembers ON ROWS
FROM [Cube]
if you look at the mdx language reference for children, you will also find another example of how to use the function with a hierarchy in stead of a member_expression.
http://msdn.microsoft.com/en-us/library/ms146018.aspx
but it won't work with a hierarchy level.
Maybe the row expression was initialy a hierarchy that you've have changed into a level expression.
in the following a similar working mdx with a hierarchy on rows:
select {} on 0,
[Product].[Model Name].children
*
[Geography].[Country].[All Geographies]
on 1
FROM [Adventure Works
Philip,
I guess you want only those rows where the children have a value on the default measure. In that case you could try the following:
select {} on columns,
Nonempty([Dimension].[hierarchy].[level].children
* [Dimension2].[hierarchy2].[level2].allmembers) on rows
from [Cube]
Now if, for the children, you'd need all the members from Dimension2 then you could try:
select {} on columns,
Nonempty([Dimension].[hierarchy].[level].children, [Dimension2].[hierarchy2].[level2].allmembers)
* [Dimension2].[hierarchy2].[level2].allmembers) on rows
from [Cube]
In the second case the Nonempty function takes a second parameter and the cross join is done with the result of the Nonempty function. For the documentation on Nonempty including the usage of the second parameter see https://learn.microsoft.com/en-us/sql/mdx/nonempty-mdx

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] )
...