MDX crossjoin with 5 dimensions - ssas

Assuming we have the following dimension
DATE
CONTRACTOR
COLOR
SIZE
and a set of 6 measures, we want to get the 6 measures for every Semestrer, Trimester, Month, Contractor, Color and Size for a given Year.
Here is my MDX query:
SELECT
{
... the 6 measures ...
} ON COLUMNS,
{
(
[Dim DATE].[year].[year].AllMembers *
[Dim DATE].[SEMESTRE NOM].[SEMESTRE NOM].AllMembers *
[Dim DATE].[TRIMESTRE NOM].[TRIMESTRE NOM].AllMembers *
[Dim DATE].[MOIS NOM].[MOIS NOM].AllMembers *
[Dim CONTRACTOR].[Name CONTRACTOR].AllMembers *
[Dim COLOR].[Name COLOR].AllMembers *
[Dim SIZE].[Name SIZE].AllMembers
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM (
SELECT ( { StrToSet( "[Dim DATE].[year].[year].&[" + #Year + "]" , CONSTRAINED ) }
) ON COLUMNS
FROM [TheCube]
This is very slow (more than 20 minutes), with only 6 Contractors, 6 Color, 18 sizes... and what is the differences between .AllMembers and .Members ?

Depending on the actual number of years in your hierarchy, you might create a quite large set with the crossjoin. As icCube mentionned in the comment, why not using :
StrToSet( "[Dim DATE].[year].[year].&[" + #Year + "]" , CONSTRAINED )
in the crossjoin on the ROWS ?
AllMembers vs Members; here is a doc; allmembers include the calc. members; do you have any costly calc. members ?
How many members do you have in [Dim DATE].[MOIS NOM].[MOIS NOM].AllMembers ? trimestre, semestre ?

Related

Number or Results and WITH MEMBER Calculation interacting weird

In our MDX Queries, we use a template like the following WITH MEMBER . SELECT SUBSET . FROM (SELECT . FROM .) a lot.
What this particular query should do (and does!) is list all product variants matching a filter, and give the sales amount for it for a month in time. On the same row, we have the sales total of all variants of this product.
We need only a subset of the result (for paging), and the results need to ordered to allow paging of course. Then there is a non empty, so only product variants that were sold in the time period qualify.
This works just fine. Paging also works: putting 30, 60, 90 for Start in SUBSET is gives the correct results.
However, if I put a 1 as Count (SUBSET(..., x, 1)) the result of the "Price all" goes to (null) for each and any row I select with x. If I return two or more rows it works just fine, and all rows have their correct values.
Two results and the correct value in the rightmost column (in case you are wondering how the total of all variants, 67, can be lower than the sales of one particular variant (280): I picked a bad example - there was a return (so a negative sale) for another variant of this product, for 213. The 67 is correct and desired!)
One Row and a (null) result
I do not understand how or why the SUBSET count would interfere with the Tuple I select in the WITH MEMBER. If it does it for 30 rows at a time, why not for one?
*edit
I just tried, what would happen if I filter in a way that only one result matches (and SUBSET paging is set to "working values" of 0, 30): The same as in the 2nd screenshot, for one result row the WITH MEMBER evaluates to (null).
So it is not that SUBSET is the problem, but more general: When there is only one result row!
Could this be a bug in our SQL Server Version? It is SQL 2012 with SP4 and all latest security patches.
WITH
MEMBER [DIM Date].[HI Year_Month_Day].[CURRENT_MONTH] AS
[DIM Date].[HI Year_Month_Day].&[20201215].Parent /* Select Month of December */
MEMBER [MEASURES].[Price all] AS (
(
[DIM Products].[HI Products].Currentmember.Parent
, [DIM Products].[HI Subproduct].[Total]
)
, [MEASURES].[Price]
), FORMAT_STRING = '#,0' /* Get the parent product total next to every subproduct */
SELECT {
([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, ([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price All])
} ON COLUMNS
, {
SUBSET(
ORDER(
NONEMPTY(
([DIM Products].[HI Produt Subproduct].[Subproduct])
, {([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])}
)
, ([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, BDESC
)
, 0
, 30
)
} ON ROWS
FROM (
SELECT
{[DIM StoreLocation].[HI Country].[Country].&[US]}
* {[DIM Productgroup].[HI Productgroup].[Productgroup].&[Bikes]}
ON COLUMNS
FROM [Cube]
)

Query to only display matching elements in two dimensions

Short question:
Any idea how I can pull the name from one dimension entry and apply it to another as filter?
Long description: My cube has has dimensions for manufacturer and seller, and I only need sales for "themselves". The dimensions are loaded from distinct tables in the source but have entries with the exact same name.
Manufacturer Seller
A A *
A D
B B *
C C *
C A
C D
A, B and C sell their products themselves, A and C also sell through reseller D. C also sells through A. I'd like to filter for the marked rows only (names matching).
In SQL this would be simple, or the cube might be easily enhanced to flag "own sales", but both options are not available. I need to query the cube "as is".
I tried using STRTOMEMBER and CURRENTMEMBER like so (with NAME and MEMBER_NAME):
STRTOMEMBER("[Dim Seller].[Seller].[" + [Dim Producer].[Producer].CURRENTMEMBER.NAME + "]
That actually works, syntactically, but CURRENTMEMBER seems to always evaluate to ALL and delivers the (correct) measure value for the ALL element yet not the one for matching name.
I also tried to create a WHERE setting the two names equal
[Dim Seller].[Seller].CURRENTMEMBER.NAME = [Dim Producer].[Producer].CURRENTMEMBER.NAME
but that is very SQL and not valid in MDX.
The whole query looks like this:
SELECT
NON EMPTY {
[Measures].[Value]
} ON COLUMNS
, NON EMPTY { (
{
[Dim Producer].[Producer].[A]
, [Dim Producer].[Producer].[B]
, [Dim Producer].[Producer].[C]
}
* [Dim Seller].[Seller].[Seller].ALLMEMBERS
// This line needs to be trimmed to same name as producer
) } ON ROWS
FROM
[Cube]
The producer list is coming from a config file and string-concatenated to the query and subject to change. Having a second seller list to be kept in sync would be a burden but also (when C sells through A) deliver wrong results.
Any idea how I can pull the name from one dimension entry and apply it to another?
The HAVING clause might help you:
SELECT
NON EMPTY {
[Measures].[Value]
} ON COLUMNS
, NON EMPTY {
{
[Dim Producer].[Producer].[A]
, [Dim Producer].[Producer].[B]
, [Dim Producer].[Producer].[C]
}
* [Dim Seller].[Seller].[Seller].ALLMEMBERS
}
HAVING
[Dim Seller].[Seller].CURRENTMEMBER.MEMBER_NAME =
[Dim Producer].[Producer].CURRENTMEMBER.MEMBER_NAME
ON ROWS
FROM
[Cube]
Use the GENERATE function to do the job for you.
with set Producers as
{[Dim Producer].[Producer].[A]
,[Dim Producer].[Producer].[B]
,[Dim Producer].[Producer].[C]}
set Sellers as
[Dim Seller].[Seller].members
//For every producer, get the seller who shares the same name.
set ProducersHavingSameNameAsSellers as
GENERATE(Producers, GENERATE(Sellers, filter
(Sellers,
[Dim Seller].[Seller].CURRENTMEMBER.MEMBER_NAME =
[Dim Producer].[Producer].CURRENTMEMBER.MEMBER_NAME
)
)
)
SELECT
NON EMPTY [Measures].[Value] ON COLUMNS,
ProducersHavingSameNameAsSellers ON ROWS
FROM [Cube]

Get the last three months dynamically

I have the following MDX query
SELECT
Hierarchize
(
{
[PERIOD].[Year Month].&[2014 / 10]
,[PERIOD].[Year Month].&[2014 / 11]
,[PERIOD].[Year Month].&[2014 / 12]
}
) ON COLUMNS
,{
[Measures].[Amount]
,[Measures].[Total Cost]
} ON ROWS
FROM [Asset];
Is there a way to get the last three months dynamically ?
Exactly as George says except in your context I don't think you need EXISTING:
SELECT
Hierarchize
(
Tail([PERIOD].[Year Month].members,3)
) ON COLUMNS
,{
[Measures].[Amount]
,[Measures].[Total Cost]
} ON ROWS
FROM [Asset];
Last N values from any axis could be retrieved by using Tail function:
Tail(EXISTING [PERIOD].[Year Month],3)
Retrieves a set of last 3 items of Month level for Period dimension

List all previous Years and measure till today such that all 12 Month data is available for year.Else exclude the year

We have a typical Date Dimension.
I am struggling to write an MDX to list the Dim.Year dimension member and measure contain data strictly for all month.(from Jan-Dec) otherwise exclude the year?.
This:
SELECT
[Measures].[Internet Sales Amount] ON 0
,[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
Returns this:
If I just now add in NON EMPTY I should get rid of 2009 and 2010:
SELECT
[Measures].[Internet Sales Amount] ON 0
,NON EMPTY
[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
Now returns:
The above is a very simple example.
Other scenarios might require us to get rid of nulls using other functions such as FILTER / NonEmpty / HAVING / IIF.
Edit
To just return years that have 12 months with data I've written the following (a wee bit more complicated than the above!). I will try to simplify later although you will definitely require an additional measure to achieve your goal,
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
Descendants
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Month]
,SELF
)
,[Measures].[Internet Sales Amount]
)
)
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0
,NON EMPTY
[Date].[Calendar].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Adventure Works];
Edit (for #sourav_agasti)
This is a solution against Adventure Works which only uses attribute hierarchies i.e. for a cube without a well formed Date dimension:
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
[Date].[Calendar Year].CurrentMember
*
[Date].[Month of Year].[Month of Year].MEMBERS
,[Measures].[Internet Sales Amount]
)
)
SELECT
{} ON 0
,[Date].[Calendar Year].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Adventure Works];
This is the equivalent to Sourav's script using the above method:
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
[Dim Year].[Year].CurrentMember
*
[Dim Month].[Month].MEMBERS
,[Measures].[Foo]
)
)
SELECT
{} ON 0
,[Dim Year].[Year].[Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Your Cube];
Further Edit
I chanced on an article last night which gives a more efficient method. Apparently Count - NonEmpty which I've done above is not tuned to work in block calculation mode whereas Sum -IIF is. So here is a quicker alternative using the later method:
WITH
MEMBER [Measures].[cntMths - S] AS
Sum
(
[Date].[Month of Year].[Month of Year].MEMBERS
,IIF
(
IsEmpty([Measures].[Internet Sales Amount])
,null
,1
)
)
SELECT
{[Measures].[cntMths - S]} ON 0
,[Date].[Calendar Year].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths - S] = 12 ON 1
FROM [Adventure Works];
I took it as a challenge to write a code which gets you the result without there being any hierarchies anywhere. So here goes my attempt(after few hours of madness!).
You must be having a [Dim Month] dimension too. I am assuming the data there is stored like Dec-2014 or Dec 2014. Basically assuming that the last 4 digits give the year.
Now, one way you can figure out the years having data is to first list out all the months which don't have data and then figuring out the year for these months and then NOT displaying these years in the final output.
The below code does that for you:
WITH SET MonthsWithNoData AS
FILTER([Dim Month].[Month].CHILDREN, [Measures].[foo] = 0)
MEMBER [Measures].Year as
RIGHT([Dim Month].[Month].CURRENTMEMBER.member_value, 4)
SET SetOfYearsWithNoData AS
GENERATE(MonthsWithNoData, STRTOSET("[Dim Year].[Year].&[" + RIGHT(MonthsWithNoData.currentmember.member_value, 4) + "]"))
SET SetOfYearsWithData AS
([Dim Year].[Year].MEMBERS - SetOfYearsWithNoData)
member yearValues as
[Dim Year].[Year].currentmember.member_value
member wellFormedyearValues as
[Dim Year].[Year].currentmember.member_unique_name
SELECT {yearvalues, wellFormedyearValues} on 0,
SetOfYearsWithData on 1
from [Your cube]
I agree that the final output does not look too beautiful though. Having said that, a hierarchy would make the code much less complicated and the MDX would be much simpler. A [Calender] dimension with requisite hierarchies in place(as assumed in #whytheq's answer) would be the better approach in terms of elegance as well as performance.

Percentage Calculation on MDX

Im new to MDX Query and I'm trying to add a percentage on attended percentage in each row. The following code :
SELECT {[Measures].[Client Interventions Count]* [Dim Intervention Attendance].[Attended].Children} ON COLUMNS,
[Dim Date].[Calendar].[Month Name] ON ROWS
FROM [Client Intervention]
procude a result of :
How can I perform a calculation on each row? For example, first row November 2007 , total client intervention count = 68 , so the percentage count should be 57/68 %
Any idea?? Thanks
Use a calculated member - you can find several examples here. The idea for the query is as following:
WITH MEMBER [Dim Intervention Attendance].[Attended].[%] AS
( [Dim Intervention Attendance].[Attended].[Attented],
[Measures].[Client Interventions Count] )
/ ( [Dim Intervention Attendance].[Attended].[Not Attented],
[Measures].[Client Interventions Count] )
SELECT
[Measures].[Client Interventions Count]
* { [Dim Intervention Attendance].[Attended].[%],
[Dim Intervention Attendance].[Attended].Children } ON COLUMNS,
[Dim Date].[Calendar].[Month Name] ON ROWS
FROM [Client Intervention
]