MDXQuery - Currentmember with his parent - mdx

I'm trying to write a MDX query that would display all my products and the subgroups (hierarchy parent) that they belong to. While the task seems rather trivial i still can't figure it out. Here is my MDX query.
select
{
[Products].[Hierarchy].currentmember.parent
}
on 0,
{
[Products].[Hierarchy].[Product].members
}
on 1
from [CUBE]
I also tried
select
{
Ancestor([Products].[Hierarchy].currentmember,
[Products].[Hierarchy].[Subgroup])
}
on 0,
{
[Products].[Hierarchy].[Product].members
}
on 1
from [CUBE];
But all I get from it is a gray list of products without any actual column. Any help/tips would be greatly appreciated.

If you want to use the same hierarchy then you can only use it on one axis - there are 3 axis in context - ON COLUMNS / ON ROWS / WHERE.
So putting [Products].[Hierarchy]. ON COLUMNS and also ON ROWS would throw an exception.
What you can do is move some of the logic into a WITH clause and move the narrative into the Measures dimension:
WITH
MEMBER [Measures].[ProdName] AS
Ancestor(
[Products].[Hierarchy].currentmember,
[Products].[Hierarchy].[Subgroup]
).MEMBER_CAPTION
SELECT
[Measures].[ProdName] ON 0,
[Products].[Hierarchy].[Product].MEMBERS ON 1
FROM [CUBE];
You could make use of your attribute hierarchies and do something like this:
SELECT
{} ON 0,
[Products].[Product].[Product]
*[Products].[Subgroup].[Subgroup]
ON 1
FROM [CUBE];

Related

MDX - Why Cross join between measures do not work?

In MDX, we can CROSS JOIN two members, a measure and a member but not two measures. Why is this so? What does it imply?
SELECT
[Measures].[xyz] * [DimTable1].[SomeHierarchy].[Level] on 0,
[DimTable2].[SomeOtherHierarchy].&[Value] on 1
FROM [MyCube]
// WORKS
SELECT
[Measures].[xyz] on 0,
[DimTable2].[SomeOtherHierarchy].&[Value] * [DimTable1].[SomeHierarchy].[Level] on 1
FROM [MyCube]
// OF COURSE IT WORKS
SELECT
[Measures].[xyz] * [Measures].[ABC] on 0,
[DimTable1].[SomeHierarchy].&[Value] on 1
FROM [MyCube]
// DOES NOT WORK!!
I believe you forgot:
SELECT
[dd].[hh].[mm1] * [dd].[hh].[mm2] on 0,
[DimTable1].[SomeHierarchy].&[Value] on 1 FROM [MyCube]
did not work neither. [Measures] is not different than [dd] in my example. In MDX you cannot define a tuple with _ several members _ of the _ same hierarchy _. Have a look to this gentle introduction explaining the main concepts.
EDIT
Your third query, that does not work, looks like this:
The yellow area is empty so it is understandable that it is not happy.
EDIT
Following is an analogy using Excel pivot tables which use OLAP technology
If you put a crossjoin of measures A and B on rows you get something like this:
Then if we add a very small level (with 4 members) onto columns we get the following:
So what will go into the main body of this table?
A count is possible and probably is, in MDX, if you create a custom measure (don't have a server to test this statement on). Excel will default to a count but the result is pretty pointless?

MDX - CHILDREN doesn't return any values, MEMBERS do

I used the following expression in MDX:
SELECT
null ON 0,
[dim].[hier].CHILDREN ON 1
FROM [Cube]
It did not return any values. However, when I issued
SELECT
null ON 0,
[dim].[hier].[hier].MEMBERS ON 1
FROM [Cube]
It worked. What is interesting, yesterday first query was working too, so I suspect some cube change might've caused that, however I don`t have an access so I cannot check it.
Is there some subtle difference between CHILDREN and MEMBERS which causes that bahaviour? I thought the two are similar except for the fact that CHILDREN digs one level down.
Not sure about the exact semantic of CHILDREN in case you apply it to a non-member; i.e., not sure it is implemented as MEMBERS. But to reply to this sentence :
CHILDREN digs one level down
no; children returns the children of the member it is applied to. Those children does not necessarily belong to the next level down. In case of a ragged hierarchy, the children could be several levels down.
it is not working cause of this:
Do not Forget on this brackets {} >> which bounds the couple on a row (on 1)
SELECT NON EMPTY AddCalculatedMembers ( { [member name].[].[].&[]&[].children, [member name].[].[].&[1]&[].children } ) on 1, [Measures].[Sales] on 0 FROM [Cube Name];

MDX query: filter and sum of filtered

I've just started to learn MDX and i want to do a query like that:
filter data by the cost ( i've already made that query but without the sum) like that:
SELECT [Measures].[SumOfSelled] ON 0,
FILTER ([From].[From].[City].members, [Measures].[SumOfSelled]>7000) ON 1
FROM [BI-Avia]
It's working
and it is OK
BUT!!!
I need also to show the sum of filtered elements under this filtered result by cities
I know how to find it separately:
with member [Measures].FilteredSum as sum(filter([From].From].City].members,Measures].SunOfSelled]>7000),Measures].[SumOfSelled])
select{SumOfSelled} on 0
from [BI-AVIA]
But i have to show this together!! The SUM under Filtered! two in one! I need youe help! I think it's very clear for you!!!
Just define the calculated member on the [From].[From] hierarchy and then combine both queries, using a union of sets (abbreviated with + in MDX):
with member [From].[From].FilteredSum as
sum(filter([From].[From].City].members, Measures].SumOfSelled]>7000))
SELECT [Measures].[SumOfSelled]
ON 0,
FILTER ([From].[From].[City].members, [Measures].[SumOfSelled]>7000)
+
{ [From].[From].FilteredSum }
ON 1
FROM [BI-Avia]
You could possibly define the filter as a set in the WITH clause, which would avoid that Analysis Services evaluates it twice.

Selecting all levels in a hierarchy as a tuple in MDX

I made my first cube yesterday. I'm still new to this, so please forgive any misuse of terminology. One of my dimensions, let's call it MyDimension1 has a hierarchy, let's call it MyHierarchy, with two levels, let's call them Level1 and Level2.
I seem to be able to do something like:
SELECT
{ [Measures].[Whatever] } ON 0,
{ ([MyDimension1].[Level1].[Level1], [MyDimension1].[Level2].[Level2]) } ON 1
FROM MyCube
Which gives me the result I want. If I understand correctly, the set defining axis 1 contains a tuple with two components(?). I've read that each component(?) of a tuple is supposed to be from a different dimension, but I seem to be able to specify components of the same dimension, as demonstrated by the above query. I'd like to be able to just specify the hierarchy by name and then have the server create the tuples for me, though. So something like:
SELECT
{ [Measures].[Whatever] } ON 0,
{ AllLevelsToTuple([MyDimension1].[MyHierarchy]) } ON 1
FROM MyCube
But I can't seem to figure out how to do this. Thanks in advance!
EDIT (My example, as requested by an answerer):
This does what I want, functionally, but isn't the syntax I'd like:
SELECT [Measures].[Original] ON 0,
([Customer].[Customer].[Customer], [Customer].[Account ID].[Account ID]) ON 1
Results (In SSMS):
Original
--------- -- --------
CustomerA 15 306.03
CustomerA 16 754.20
...
CustomerB 17 524.43
...
CustomerC 22 760.42
...
When I expand the dimension, it looks like:
- Customer
- Hierarchy
+ Members
+ Customer
+ Account ID
When I do something like (I'd like to do something similar syntactically):
SELECT [Measures].[Original] ON 0,
([Customer].[Hierarchy].Members) ON 1
I get the following results:
Original
---------- --------
All 17638.15
CustomerA 2624.76
15
16
...
CustomerB 3113.67
17 524.43
...
CustomerC 3427.01
22 760.42
...
I want there to be 3 columns, not 2, basically.
You might want to read this MDX gentle introduction.
I've read that each component(?) of a tuple is supposed to be from a
different dimension
From different hierarchy instead. In AS, I guess that for each level, you have as well a corresponding flat hierarchy; so the following looks like you're accessing levels of two different hierarchies:
{ ([MyDimension1].[Level1].[Level1], [MyDimension1].[Level2].[Level2]) } ON 1
I'm not a specialist of AS but I guess the following statement :
SELECT [Measures].[Original] ON 0,
([Customer].[Customer].[Customer], [Customer].[Account ID].[Account ID]) ON 1
is actually interpreted as a crossjoin of level members from 2 different hierarchies; it is more likely that the () notation is interpreted as the () operator instead of tuple notation and then {},{} is a crossjoin in MDX :
SELECT [Measures].[Original] ON 0,
[Customer].[Customer].[Customer].members * [Customer].[Account ID].[Account ID].members ON 1
hence the two columns in front of your measures in the result.
I want there to be 3 columns, not 2, basically.
You'll need to use calculated measures; something like :
with
MEMBER Measures.HN as [Customer].Currentmember.Hierarchy.Name
MEMBER Measures.LN as [Customer].Currentmember.Level.Name
MEMBER Measures.MN as [Customer].Currentmember.Name
SELECT { Measures.HN, Measures.LN, Measures.MN, [Measures].[Original] } ON 0,
([Customer].[Hierarchy].Members) ON 1
Yes, you can create a tuple from different dimensions, and you do so by placing members between parentheses and separated by commas.
But in this case, you don't need a tuple, simply a set. A set contains members from a single dimension, separated by commas and placed between curly braces: {}.
Have a look at the descendants function. You can create a set from your hierarchy using this function.

MDX on multiple hierarchical dimensions

2 hierarchical dimensions (for location and time) are defined on a cube. My question is about possibility of writing a single MDX query for retrieving following structure. I mean writing a single query for obtaining values V1, V2, V3 and V4:
The obvious way is to use multiple MDX queries. Just wondering if there is some magic syntax in MDX.
Try:
SELECT
{
[Measures].[Some Measure]
} ON 0,
{
[Location].[Level 1].[Level 1].Members*
[Date].[Year].[Year].Members*
[Location].[Level 2].[Level 2].Members
} ON 1
FROM [Your Cube]
You can also get the totals with:
SELECT
{
[Measures].[Some Measure]
} ON 0,
{
[Location].[Level 1].Members*
[Date].[Year].Members*
[Location].[Level 2].[Level 2].Members
} ON 1
FROM [Your Cube]
No magic here - just crossjoining the attribute hierarchies as needed. Please note that if you place the two Location hierarchies next to each other the excution will be faster.
No idea if this completely answers your question but at least may help a bit...