Filtering hierarchies in MDX WITH clause - ssas

By using the answer of MDX on multiple hierarchical dimensions I have the following MDX query:
with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Year].&[2010],
[Measures].[x])
select ([Dim Attribute].[Parent Code].&[10000]) on 0,
({L1Y1}) on 1
from DS
Now the problem is how to filter the L1Y1 based on its children. For example suppose we want to filter it so that only season 2 and month 7 included in the query. The following query output is the same as above and the where clause has no effect:
with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Year].&[2010],
[Measures].[x])
select ([Dim Attribute].[Parent Code].&[10000]) on 0,
({L1Y1}) on 1
from DS
where [Dim Date].[Calendar Date].[Season].&[2] and
[Dim Date].[Calendar Date].[Month].&[7]

Unless you removed the attributes you should have multiple hierarchies available in your time dimension. Can you try this :
with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Year].&[2010],
[Measures].[x])
select ([Dim Attribute].[Parent Code].&[10000]) on 0,
({L1Y1}) on 1
from DS
where [Dim Date].[Season].&[2] and [Dim Date].[Month].&[7]
Note, as we are not using the hierarchy [Calendar Date] time member is not overwritten but filtered.

How about:
with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Year].&[2010],
[Measures].[x])
member S2 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Season].&[2],
[Measures].[x])
member M7 as ([Dim Location].[Hierarchy].[Center].&[1],
[Dim Date].[Calendar Date].[Month].&[7],
[Measures].[x])
select ([Dim Attribute].[Parent Code].&[10000]) on 0,
(L1Y1, S2, M7) on 1
from DS
I understand it is a somewhat more laborous approach, but you would get the result. If you otherwise want to get a single value only you can try keeping only the M7 measure.

Related

How to implement "greater than" based on dimension value in MDX

I would like to query the total sales of each country with the GDP growth rate of more than 3. So far, I can display the sales information of each country like such:
SELECT
{[Measures].[Sales]} ON 0,
{[Dim Company Info].[LOC].ALLMEMBERS} ON 1
FROM [Database]
But then I am still blank on how to query all GDP growth rate of more than 3. I have searched SO and found filter to be the answer, however, I do not know where to include it in my code above. How do I go about this?
Edit: I have tried the followings, but I do not think that is what I am supposed to do:
WHERE ([Dim Company Info].[Gdp Growth].&[3] : [Dim Company Info].[Gdp Growth].&[3].LastChild)
and
SELECT
{[Measures].[SALES]} ON 0,
{FILTER([Dim Company Info].[LOC].MEMBERS, [Dim Company Info].[Gdp Growth] > 3)} ON 1
FROM [766 Database]
and
SELECT
{[Measures].[SALES]} ON COLUMNS,
{[Dim Company Info].[LOC].MEMBERS} ON ROWS
FROM [766 Database]
WHERE FILTER([Dim Company Info].[Gdp Growth], [Dim Company Info].[Gdp Growth] > 2)
You can use your below expression when the dimension members are numericly arranged
WHERE ([Dim Company Info].[Gdp Growth].&3 : [Dim Company Info].[Gdp
Growth].&3.LastChild)
with a slight correction
WHERE ([Dim Company Info].[Gdp Growth].&[3] : [Dim Company Info].[Gdp Growth].[Gdp Growth].LastChild)
However if that is not the case, then you need to follow the below method.
I am reporting the product with the max quantity.
select {[Measures].[Internet Sales Amount]} on 0,
non empty
([Product].[Product].[Product],[Promotion].[Max Quantity].[Max Quantity]) on 1
from [Adventure Works]
Now lets manipulate the max quantity to make it behave like a measure.
with
member measures.test
as
case when [Measures].[Internet Sales Amount]>0 then
(nonempty(([Product].[Product].currentmember,[Promotion].[Max Quantity].[Max Quantity]),[Measures].[Internet Sales Amount])).item(0).item(1).name
else
null end
select
{[Measures].[Internet Sales Amount],measures.test}
on 0,
non empty ([Product].[Product].[Product] )
on 1
from [Adventure Works]
Now you can use the filter, but we need to cast(note the use of CInt) the data too
with
member measures.test
as
case when [Measures].[Internet Sales Amount]>0 then
Cint((nonempty(([Product].[Product].currentmember,[Promotion].[Max Quantity].[Max Quantity]),[Measures].[Internet Sales Amount])).item(0).item(1).name)
else
null end
select
{[Measures].[Internet Sales Amount],measures.test}
on 0,
non empty (
filter(
[Product].[Product].[Product]
,measures.test>0)
)
on 1
from [Adventure Works]

DAX - RANK Function

I have a RANK code from WEBI and need to write similar one in DAX or MDX.Both can work. But DAX will be more useful.
=Rank([OrderCount];([Category1];[Category2];[Category3]))
I have tried below code in DAX but it is not exact what i need.
=RANK.EQ(table1[OrderCount];table1[OrderCount])
Can you help me out write similar?
Grouping by Count. This is extra.
I'm not very good at DAX, but MDX way is the following:
with
Dynamic Set OrderedSet as
Order(
NonEmptyCrossJoin(
[Dim Product].[Subcategory Name].[Subcategory Name].Members,
[Dim Product].[Category Name].[Category Name].Members
[Measures].[Order Quantity],
2
),
[Measures].[Order Quantity],
BDESC
)
Member [Measures].[Rank] as
Rank(
([Dim Product].[Subcategory Name].Currentmember,
[Dim Product].[Category Name].Currentmember),
OrderedSet
)
select
{[Measures].[Order Quantity],[Measures].[Rank]} on 0,
non empty OrderedSet on 1
from
[Adventure Works DW2016CTP3]
DenseRank:
with
Dynamic Set OrderedSet as
Order(
NonEmptyCrossJoin(
[Dim Product].[Subcategory Name].[Subcategory Name].Members,
[Dim Product].[Category Name].[Category Name].Members,
[Measures].[Order Quantity],
2
),
[Measures].[Order Quantity],
BDESC
)
Dynamic Set DenseOrderedSet as
Order(
NonEmpty(
OrderedSet,
[Measures].[RankFirstMatch]
),
[Measures].[Order Quantity],
BDESC
)
Member [Measures].[Rank] as
Rank(
([Dim Product].[Subcategory Name].Currentmember,[Dim Product].[Category Name].CurrentMember),
OrderedSet
)
Member [Measures].[RankFirstMatch] as
IIF(
[Measures].[Order Quantity]
=
(
OrderedSet.Item([Measures].[Rank] -2),
[Measures].[Order Quantity]
),
NULL,
[Measures].[Rank]
)
Member [Measures].[RankDenseSet] as
Rank(
([Dim Product].[Subcategory Name].Currentmember,[Dim Product].[Category Name].CurrentMember),
DenseOrderedSet
)
Member [Measures].[DenseRank] as
IIF(
[Measures].[RankDenseSet] = 0,
(OrderedSet.Item([Measures].[Rank] -2),[Measures].[DenseRank]),
[Measures].[RankDenseSet]
)
select {[Measures].[Order Quantity],[Measures].[Rank],[Measures].[RankFirstMatch],[Measures].[RankDenseSet],[Measures].[DenseRank]} on 0,
non empty OrderedSet on 1
from [Adventure Works DW2016CTP3]
Here is the DAX measure for your question:
Rank =
IF (
HASONEVALUE ( YourTableName[ProductName] ),
RANKX (
ALL ( YourTableName ),
CALCULATE ( SUM ( YourTableName[Order Quantity] ) ),
,
,
DENSE
)
)
IF (HASONEVALUE ( YourTableName[ProductName] ).... part will eliminate your Grand Total from showing 1

how to use MDX average function?

I am VERY new to mdx, and about to learn it. i got into trouble right now ,because i want to get average amount for each weekday between two dates.
For example between 1st of november and 1st of december there are 4 mondays, so average would be [Measures].[Amount] / 4, but i really cant figure out how to express this simple function in MDX
with member [Measures].[Avg] as
avg([Dim Date].[Day Of Week].[Day Of Week]
, [Measures].[amount])
SELECT NON EMPTY { ([Measures].[Avg]), ([Measures].[Amount])} ON COLUMNS,
NON EMPTY { ([Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS * [Dim Date].[Date Int].[Date Int].ALLMEMBERS )}
ON ROWS FROM ( SELECT ( { [Dim Client].[Common Client UID].&[{xx}] } )
ON COLUMNS FROM ( SELECT ( [Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215] )
ON COLUMNS FROM [ff]))
WHERE ( [Dim Client].[Common Client UID].&[{xx}] )
This query give me each weekday and all the dates which are bound to the specific weekday, and for each date there are an total amount.
like you can see my average is the same as total. I thought that i could just select all the weekdays between two date, find out for example how many mondays there are and divide it number with amount. but i couldnt figure that out either.
with member [Measures].[avg] as
avg([Dim Date].[Day Of Week].[Day Of Week], [Measures].[Amount])
SELECT NON EMPTY {[Measures].[avg], [Measures].[Amount]} ON COLUMNS, NON EMPTY { ([Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS )}
ON ROWS FROM ( SELECT ( { [Dim Client].[Common Client UID].&[{xx}] } )
ON COLUMNS FROM ( SELECT ( [Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215] )
ON COLUMNS FROM [ff]))
WHERE ( [Dim Client].[Common Client UID].&[{xx}] )
result :
How do i solve this?
What you really want is average amount over the dates corresponding to that weekday for the given date range.
You can just move the date range to the definition of calculated member, so that when value for a particular weekday is evaluated, the date range is also considered.
with member [Measures].[Avg] as
avg
(
[Dim Date].[Day Of Week].CURRENTMEMBER * {[Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215]}
,[Measures].[amount]
)
SELECT NON EMPTY { ([Measures].[Avg]), ([Measures].[Amount])} ON COLUMNS,
NON EMPTY [Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS * [Dim Date].[Date Int].[Date Int].ALLMEMBERS ON ROWS
FROM [ff]
WHERE ( [Dim Client].[Common Client UID].&[{xx}] )

How to use avg function in mdx?

I have my mdx query:
SELECT
NON EMPTY
{[Measures].[Amount]} ON COLUMNS
,NON EMPTY
{[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM
(
SELECT
[Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
FROM [Cube]
WHERE
[Dim Client].[Common Client UID].&[{some id}]
);
so i have my a weekday dim, which contain members as numbers from 1-7. Query find returns amount for each weekday, which is summed up, but i want to find out an average, so somehow i need to find out how many items was summed to give me [Measures].[Amount] result. I have tryed with separate member function which didnt worked.
WITH MEMBER [Measures].[Avg] AS
Avg(
( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
)
Avg return exectly the same value. How do i do such a request in mdx?
This will give you an average over 1 member:
WITH MEMBER [Measures].[Avg] AS
Avg(
( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
)
That is because CURRENTMEMBER is returning 1 member.
If you want an average over several members than you need to supply a set as the first argument for the Avg function. Here is an example:
WITH MEMBER [Measures].[Daily Avg] AS
Avg(
Descedants(
[Date].[Date - Calendar Month].CURRENTMEMBER
,[Date].[Date - Calendar Month].[Calendar Day]
,[Measures].[Amount]
)
Although I suspect something like the follwoing should work in your context:
WITH
MEMBER [Measures].[Avg] AS
Avg
(
(EXISTING
[Dim Date].[Date Int].MEMBERS)
,[Measures].[Amount]
)
SELECT
NON EMPTY
{
[Measures].[Amount]
,[Measures].[Avg]
} ON COLUMNS
,NON EMPTY
{[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM
(
SELECT
[Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
FROM [Cube]
WHERE
[Dim Client].[Common Client UID].&[{some id}]
);

Optimize a MDX Query

I am quite new to MDX and I need some help with this query.
Generate(
filter(
[Dim Products].[Product].[Product].members
*
[Dim Date].week.week.members,
[Measures].[Price]
),
nonempty(
topcount(
[Dim Price].[Price].[Price]
*
[Dim Products].[Product].currentmember
*
[Dim Date].week.currentmember,
1,
[Measures].[Price Count]
)
)
)
I am using the above Named Set in a dashboard tool (Dundas Dashboard) in order to retrieve the MODE (price value that repeats the most). It does show correct results however it is slow. It takes 2-3 seconds if there is a filter on a single week and takes about 6-7 seconds if there is no filter on week (shows data for all weeks). And this is in SSMS but the client tool takes even longer to get the result set, sometimes timing out.
After some tests it appears that the number of the rows in the fact table does not affect the performance, I've drastically decreased it but still the same. The performance increased, however, once I've decreased the number of members in 2 of the dimension tables - [Dim Price], [Dim Products]. Initially it was taking 20+ seconds to get result set but improved to 6-7 seconds once I've decreased the number of members as follows:
Table | Rows Before | Rows After
[Dim Price] | 2400 | 620
[Dim Products] | 1080 | 101
This makes me think there is a Cartesian Product between the 3 dimensions that is affecting the performance.
I need someone to advise how I can improve the query to increase the performance.
Try the EXISTING function before the NONEMPTY function like this:
Generate(
filter(
[Dim Products].[Product].[Product].members
*
[Dim Date].week.week.members,
[Measures].[Price] //<<<<<<<<<<SHOULD THIS NOT INCLUDE A CONDITION e.g. [Measures].[Price] > 0 ?
),
EXISTING nonempty(
topcount(
[Dim Price].[Price].[Price]
*
[Dim Products].[Product].currentmember
*
[Dim Date].week.currentmember,
1,
[Measures].[Price Count]
)
)
)
Attempt 2:
Generate(
filter(
NONEMPTY(
[Dim Products].[Product].[Product].members
*
[Dim Date].week.week.members,
[Measures].[Price]
)
),
EXISTING nonempty(
topcount(
[Dim Price].[Price].[Price]
*
[Dim Products].[Product].currentmember
*
[Dim Date].week.currentmember,
1,
[Measures].[Price Count]
)
)
)
Attempt 3:
GENERATE(
FILTER(
NONEMPTY(
[Dim Products].[Product].[Product].MEMBERS
*
[Dim Date].week.week.MEMBERS
),
[Measures].[Price] > 0
),
TOPCOUNT(
NONEMPTY(
EXISTING
[Dim Price].[Price].[Price]
*
[Dim Products].[Product].currentmember
*
[Dim Date].week.currentmember,
[Measures].[Price Count]
),
1,
[Measures].[Price Count]
)
)