Using Multiple Descendants in the same crossjoin - mdx

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

Related

MDX Request : How to Topcount without specific members

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]

MDX: Linkmember > Descendants offsets with currentmember context

I'm self taught in MDX and am struggling with a problem.
I need to be able to isolate a set of products according to a window around their release date and then calculate their sales by the current rows date context. The 'window' is a set of streetdates where member is between or equal to currentmember -27 and currentmember + 7 (as we sometimes see early sales/preshipping).
Final output should look something like (forward days blank):
NR Sales
2018-03-25 117.12
2018-03-26 130.01
2018-03-27 2,827.63
2018-03-28
2018-03-29
2018-03-30
2018-03-31
My cube has a street/release date hierarchy which is a mirror of my fiscal date hierarchy. It pulls correct values with a slightly altered query looking at a single day in the where clause.
The below query returns values, but is slightly incorrect as i think the descendants call isn't working properly.
WITH
SET [StreetDateSet] AS
LinkMember
(
Descendants
(
[Date].[Yr-Qtr-Mo-Wk-Day].CurrentMember
,[Date].[Yr-Qtr-Mo-Wk-Day].[Day]
).Item(0)
,[Street Date].[StreetDate-Yr-Mo-Wk-Dy]
).Lag(27)
:
LinkMember
(
Descendants
(
[Date].[Yr-Qtr-Mo-Wk-Day].CurrentMember
,[Date].[Yr-Qtr-Mo-Wk-Day].[Day]
).Item(0)
,[Street Date].[StreetDate-Yr-Mo-Wk-Dy]
).Lag(-7)
MEMBER [NR Sales] AS
Sum
(
[StreetDateSet]
,[Measures].[Sales Value]
)
SELECT
NON EMPTY
[NR Sales] ON 0
,[Date].[Date].Children ON 1
FROM [CUBE]
WHERE
(
[Date].[Yr-Qtr-Mo-Wk-Day].[Week].&[2018]&[2]&[4]
,[Item].[New Used].[New]
,{
[Item].[Category Code].[220]
,[Item].[Category Code].[230]
,[Item].[Category Code].[210]
}
);
I suspect there's a few problems here but am not sure how to address them. Descendants is likely impacted by the date hierarchy in the where clause. I've tried to get it into the 1 axis, but i get 'The Yr-Qtr-Mo-Wk-Day hierarchy already appears in the Axis1 axis.' and am unsure how to address.
Thanks for any advice/guidance on where i'm going wrong.
Turns out that linkmember wasn't required and ended up finding a solution via MSDN that uses a filter/Properties function:
Set iteration in calculation
Solution involves a member which iterates a NULL * CurrentMember defined set and I slightly adopted the statement to generate a dynamic set which matches off the complementary hierarchies.
WITH
MEMBER [Measures].[SameDay] AS
NULL * Count([Date].[Yr-Mo-Wk-Day].CurrentMember AS MySet)
+
Sum
(
{
Filter
(
[Street Date].[StreetDate-Yr-Mo-Wk-Dy].MEMBERS AS MySet2
,
MySet2.Current.Properties("Key") = MySet.Item(0).Properties("Key")
).Item(0).Lag(28)
:
Filter
(
[Street Date].[StreetDate-Yr-Mo-Wk-Dy].MEMBERS AS MySet2
,
MySet2.Current.Properties("Key") = MySet.Item(0).Properties("Key")
).Item(0).Lead(7)
}
,[Measures].[Sales Value]
)

Arbitrarily picking a dimension to add members to

The following script gives exactly the result I want.
It feels like a hack as I've added the custom members VALUE and VALUE_MTD onto the hierarchy [Customer].[Country]. I've chosen this hierarchy arbitrarily - just not used [Measures] or [Date].[Calendar] as they are already in use.
Is there a more standard approach to returning exactly the same set of cells?
WITH
MEMBER [Customer].[Country].[VALUE] AS
Aggregate([Customer].[Country].[(All)].MEMBERS)
MEMBER [Customer].[Country].[VALUE_MTD] AS
Aggregate
(
PeriodsToDate
(
[Date].[Calendar].[Month]
,[Date].[Calendar].CurrentMember
)
,[Customer].[Country].[VALUE]
)
SELECT
{
[Customer].[Country].[VALUE]
,[Customer].[Country].[VALUE_MTD]
} ON 0
,NON EMPTY
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Order Quantity]
}
*
Descendants
(
{
[Date].[Calendar].[Month].&[2007]&[12]
:
[Date].[Calendar].[Month].&[2008]&[01]
}
,[Date].[Calendar].[Date]
) ON 1
FROM [Adventure Works];
The standard approach is called utility dimension. If you Google this term, you will find several descriptions of this approach. A "utility dimension" is one which does not reference any data, but is just added to the cube for the purpose of being able to cross join them with all other dimensions for calculations. You can have one or more of them.
Thus, in most cases, physically there is nothing in the dimension. It is just used for calculated members. (Depending on the implementation, you may have the attribute members defined physically, if you want to have some properties for them. But then, only the default member is referenced in the star schema from the fact tables. The attribute member values are then overwritten in the calculation script.)
Typical applications for this are time calculations like YTD, MTD, MAT (Moving Annual Total, i. e. a full year of data ending in the selected date), or comparisons like growth vs. a previous period.

Aggregating MDX query results on existing facts only

I am very new to MDX, so probably I'm missing something very simple.
In my cube, I have a dimension [Asset] and a measure [Visits], calculating (in this case) how many visits an asset has been consumed by. An important thing to note is that not every visit is associated with an asset.
What I need to find out is how many visits there are that consumed at least one asset. I wrote the following query:
SELECT
[Asset].[All] ON COLUMNS,
[Measures].[Visits] ON ROWS
FROM
[Analytics]
But this query just returns the total number of visits in the cube. I tried applying the NON EMPTY modifier to both axes, but that doesn't help.
This query should give you what you expect:
WITH MEMBER [Asset].[Asset Name].[All Assets] AS
AGGREGATE( EXCEPT( [Asset].[Asset Name].MEMBERS, { [Asset].[All] } ) )
SELECT
{ [Asset].[Asset Name].[All Assets] } ON COLUMNS,
[Measures].[Visits] ON ROWS
FROM
[Analytics]
You may need to put {[Asset].[Asset Name].[All]} as second argument of Except if the All member was not excluded.
In the query I create a calculated member [Asset].[Asset Name].[all assets] that should represent all your existing assets. I supposed that your existing assets are all the members of the level [Asset].[Asset Name] but the All member.
You can find more information about the Aggregate function here.
This works as well:
SELECT
[Measures].[Visits] ON 0
FROM
[Analytics]
WHERE
DRILLDOWNLEVEL([Asset].[All])
Update: as well as this:
SELECT
[Measures].[Visits] ON 0
FROM
[Analytics]
WHERE
[Asset].[All].CHILDREN

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]