skyde
skyde

Reputation: 2956

How to groupby and filter on the same dimension in MDX

I want to create a barchart with a bar for each month and some measure.

But i also want to filter on a range of day which might not completly overlap some of the month.

When that happen I would like the aggregate count for those month to only aggregat over the days that fall in my date range not get the aggregate for the whole month.

Is that possible with MDX and if it is how should the request look like?

Upvotes: 1

Views: 1079

Answers (3)

Magnus Smith
Magnus Smith

Reputation: 5963

Create a second time dimension, using a virtual dimension of the original dimension. Use one dimension in the WHERE and another in the SELECT.

This often happens anyway if some people want 'Business Time' of quarters and periods, and others prefer months. Or if you have a financial year which runs April-April.

Upvotes: 1

skyde
skyde

Reputation: 2956

One way I found to do it with Mondrian is as follow

WITH MEMBER [Measures].[Units Shipped2] AS
SUM
(
 {
  exists([Store].[Store Country].currentmember.children,{[Store].[USA].[WA],[Store].[USA].[OR]})
 },[Measures].[Units Shipped]
)
MEMBER [Measures].[Warehouse Sales2] AS
SUM
(
 {
  exists([Store].[Store Country].currentmember.children,{[Store].[USA].[WA],[Store].[USA].[OR]})
 },[Measures].[Warehouse Sales]
)
SELECT 
{[Measures].[Units Shipped2],[Measures].[Warehouse Sales2]} ON 0,
NON EMPTY [Store].[Store Country].Members on 1
FROM [Warehouse]

I am not sure if the filtering will be done in SQL like below and give good performance or be run locally

select Country, sum(unit_shipped)
where state in ('WA','OR' )
group by Country 

Upvotes: 0

Benoit
Benoit

Reputation: 1993

You can use subselect. You can find more information on this page and this page:

When a member is specified in the axis clause then that member with its ascendants and descendants are included in the sub cube space for the subselect; all non mentioned sibling members, in the axis or slicer clause, and their descendants are filtered from the subspace. This way, the space of the outer select has been limited to the existing members in the axis clause or slicer clause, with their ascendants and descendants as mentioned before.

Here is an example:

SELECT { [Customer].[Customer Geography].[Country].&[Australia]
       , [Customer].[Customer Geography].[Country].&[United States]
       } ON 1
     ,  {[Measures].[Internet Sales Amount], [Measures].[Reseller Sales Amount]} ON 0
  FROM ( SELECT {[Customer].[Customer Geography].[Country].&[Australia]
                 , [Customer].[State-Province].&[WA]&[US]} ON 0
           FROM [Adventure Works]
        )

The result will contain one row for Autralia and another one for the United States. With the subselect, I restricted the value of United Stated to the Washington state.

Upvotes: 1

Related Questions