MDX: Tuple contains more than one member of hierarchy - mdx

I want to display levels of the same hierarchy in the same axis separate entities. For example I want [Customer].[State] and [Customer].[County] to be displayed in separate columns.
I tried the following query:
select NON EMPTY
{[Customer].[State].Members * [Customer].[County].Members}
ON ROWS
from [Search]
but get Mondrian Error: Tuple contains more than one member of hierarchy.
Is possible to do what I want to do, essentially flatten the hierarchy?

i don't know if the following will work in mondrian:
SELECT
{[Measures].[Internet Sales Amount]} ON 0
,NON EMPTY
{
(
[Customer].[Gender].[Gender].MEMBERS
,[Customer].[Marital Status].[(All)]
)
,(
[Customer].[Gender].[(All)]
,[Customer].[Marital Status].[Marital Status].MEMBERS
)
} ON 1
FROM [Adventure Works];
Philip,

Related

MDX - Grand total missing when filtering

I've been working on an ExcelDNA C# xll that allows users to enter simple words (under guidance) and I construct the elaborate MDX for them to query against a remote ActivePivot cube.
During testing I've ntoticed that when filtering, the grand total disappears (presumably it's joining tuples together). How do I still get a grand total? Do I need to use SCOPE or create a calculated member?
Thanks to more advanced MDX people:
SELECT
NON EMPTY
{
[Measures].[Notional.SUM]
,[Measures].[Notional.SHORT]
,[Measures].[Notional.LONG]
} ON COLUMNS
,NON EMPTY
Hierarchize
(
Filter
(
(
[CDR].[CDR].MEMBERS
,[Book].[Book].MEMBERS
)
,
Left([Book].[Book].CurrentMember.MemberValue,2) = "22"
)
,POST
) ON ROWS
FROM [TraderCube]
WHERE
[Date].[Date].[2020-01-24];
The following is something similar against AdvWrks cube:
WITH
//>>inside the WITH clause we have moved the set
SET [FilteredSet] AS
{
Filter
(
[Reseller].[Reseller Type].[Business Type].MEMBERS
,
Left([Reseller].[Reseller Type].CurrentMember.MemberValue,2) = "sp"
OR
Left([Reseller].[Reseller Type].CurrentMember.MemberValue,2) = "VA"
)
}
//>>next we create a custom member that is the sum of the filtered set
MEMBER [Reseller].[Reseller Type].[All Visible Resellers] AS
Aggregate([FilteredSet])
SELECT
NON EMPTY
{[Measures].[Reseller Sales Amount]} ON COLUMNS
,NON EMPTY
//>> inside these curly brackets we declare a set that is the filtered set and the Total member
{
[FilteredSet]
,[Reseller].[Reseller Type].[All Visible Resellers]
} ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[Calendar Year].&[2013];
The results of the above is the following:

MDX - transpose rows to columns

I'm trying to display details from two rows into one using MDX. If I execute the MDX below, it returns 2 rows, one containing the 998 Key and one containing the 999 Key
SELECT NON EMPTY {
[Measures].[FactTableCount] } ON COLUMNS,
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS
* ([DimAAAA].[AAAAKey].[AAAAKey],{[DimBBBB].[Key].&[998],[DimBBBB].[Key].&[999]},[DimCCCC].[CCCCKey].[CCCCKey])
) } ON ROWS
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS
FROM [FactTable])
It returns something like this
(columns [DimXXXX].[XXXXKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])
MyValue, MyAAAAKey1, 998, MyCCCCKey1, 1
MyValue, MyAAAAKey2, 999, MyCCCCKey2, 1
However I want to return one row like this
`(columns [DimXXXX].[Key], [DimAAAA].[AAAAKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])
MyValue, MyAAAAKey1, MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 1
Among other things (such as using SETs, putting the 998/999 logic after the ROWS/COLUMNS, etc) I've tried
SELECT NON EMPTY {
[Measures].[FactTableCount] } ON COLUMNS,
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS
* ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[998],[DimCCCC].[CCCCKey].[CCCCKey])
* ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[999],[DimCCCC].[CCCCKey].[CCCCKey])
) } ON ROWS
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS
FROM [FactTable])
...however because the AAAAKey hierarchy is repeated I receive the error message "The AAAAKey hierarchy is used more than once in the Crossjoin function"
Is there a way to do do this ?
Based on your comment below I have a sample.Let me know if it works.
I think I can see what you're saying however the measures are one
thing, but the dimension values are another - say Record1:MyValue,
MyAAAAKey1, 998, MyCCCCKey1, 2 and Record2: MyValue, MyAAAAKey2, 999,
MyCCCCKey2, 5 - I would like to output MyValue, MyAAAAKey1,
MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 2, 5
So in the query below I am trying to simulate your problem.
select
{[Measures].[Internet Sales Amount]}
on columns,
non empty
([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]},[Product].[Subcategory].[Subcategory])
on rows
from [Adventure Works]
Result
Now onw way is to take the changing values to columns, "{[Product].[Category].&1,[Product].[Category].&2}" in my case and "{[DimBBBB].[Key].&[998],[DimBBBB].[Key].&[999]}" in your case
select
{
({[Product].[Category].&[1],[Product].[Category].&[3]},[Measures].[Internet Sales Amount]),
([Product].[Category].defaultmember,[Measures].[Internet Order Quantity])
}
on columns,
non empty
([Customer].[City].[City],[Product].[Subcategory].[Subcategory])
on rows
from [Adventure Works]
Result:
Note how the values are only repeated for the relevent column. This does add an extra column but you rows are now half the orignal count.
Edit: to handle the requirement based on the comment
1st row of the grid would be Ballard, Bikes, Mountain Bikes, Road
Bikes. The 2nd: Ballard, Clothing, Caps, Gloves. The 3rd: Barstow,
Bikes, Road Bikes, null. I want to merge/list the actual dimension
values
So to achieve above we have two options. But in either case some manipulation on UI would be required.
1)First option
with member
measures.t
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name
member measures.t1
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name
select
{measures.t,measures.t1}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows
from [Adventure Works]
2)The second option,
with member
measures.t1
as
[Customer].[City].currentmember.name
member measures.t2
as
[Product].[Category].currentmember.name
member measures.t3
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name
member measures.t4
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name
select
{measures.t1,measures.t2,measures.t3,measures.t4}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows
from [Adventure Works]

Filter dimensions for a specific member in MDX

I need to filter the dimension [Line] just for a specific [Year].Member.
For example,
[Time].[2004] to show results ([Product].[Line].[Classic Cars] and [Product].[Line].[Ships]) - excude the rest of [Product].[Line] members for [Time].[2004] but do not exclude [Product].[Line] members for the other [Time].Members.
I need a code compatible with Mondrian.
Any suggestion?
SELECT
NON EMPTY {[Measures].[Sales]} ON COLUMNS,
NON EMPTY NonEmptyCrossJoin([Time].[Years].Members, [Product].[Line].Members) ON ROWS
FROM
[SteelWheelsSales]
Something like this should work:
SELECT
NON EMPTY
{[Measures].[Sales]} ON COLUMNS
,NON EMPTY
{
(
[Time].[2004]
,{
[Product].[Line].[Classic Cars]
,[Product].[Line].[Ships]
}
)
,NonEmptyCrossJoin
(
Except
(
[Time].[Years].MEMBERS
,[Time].[2004]
)
,[Product].[Line].MEMBERS
)
} ON ROWS
FROM [SteelWheelsSales];

MDX Query - Select Columns From Same Dimensions

I have a requirement displaying data from same dimension in more than 1 column. For eg. I want to show data Year and Month wise. In my dimension structure, Year and Month belongs to same hierarchy. When I run below query I get error. PFB the query.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members *
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
The error I get is Query (2, 12) The Hierarchy hierarchy is used more than once in the Crossjoin function. I am new to MDX queries. Please help in this regard. Thanks in advance.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members ,
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
Instead of CROSSJOIN have a set as above. In a set, you can put members from same hierarchy
I like Sourav's answer - but it will put the results in one column which is slightly different than the question.
In AdvWorks this is in one column:
SELECT
[State-Province].MEMBERS ON COLUMNS
,{
[Date].[Calendar].[Calendar Year].MEMBERS
,[Date].[Calendar].[Month].MEMBERS
} ON ROWS
FROM [Adventure Works];
It is possible to switch to two columns and use a cross join but you need to find out the details of your Date dimensions Attribute hierarchies (as opposed to User hierarchies):
SELECT
[State-Province].MEMBERS ON COLUMNS
,
[Calendar Year].[All Periods].Children
* [Month].MEMBERS ON ROWS
FROM [Adventure Works];
In your cube maybe something like this:
SELECT
NON EMPTY
{[Measures].[Target Actual Value]} ON 0
,NON EMPTY
[Year Name].MEMBERS
*
[Month Year].MEMBERS ON 1
FROM [Cube_BCG_OLAP];

How can I determine whether dimensions are related?

I am developing a query builder application that generates MDX and trying to get customer counts from a cube using the following, which works just fine:
WITH MEMBER MEASURES.X AS (
{ [Customer].[Gender].[Female]},
[Customer].[Customer].Children
).Count
SELECT Measures.X ON 0 FROM [Adventure Works]
However, if the user drags in a dimension that is not related to the customer like:
WITH MEMBER MEASURES.X AS (
{ [Customer].[Gender].[Female]},
{ [Employee].[Status].[Active], [Employee].[Status].[Inactive]},
[Customer].[Customer].Children
).Count
SELECT Measures.X ON 0 FROM [Adventure Works]
the count result obviously becomes incorrect.
Is there a way to determine whether a dimension is related to the customer so that I can exclude it from the generated MDX query?
This information can be retrieved from the cube through AMO. The Cube class contains all the cube metadata you'll need.
Solved the problem by using the Exists( Set_Expression1 , Set_Expression2 [, MeasureGroupName] ) function. No need to manually determine which dimensions are related. The Exists function filters out the unrelated tuples, leaving only the
{ [Customer].[Customer].Children, [Customer].[Gender].[Female]} set to do the count over.
Here is the MDX:
WITH MEMBER MEASURES.X AS Exists(
[Customer].[Customer].Children,
{[Customer].[Gender].[Female]}
*
{[Employee].[Status].[Active], [Employee].[Status].[Inactive]}
).Count
SELECT Measures.X ON 0 FROM [Adventure Works]