简体   繁体   中英

In MDX how can I filter a dimension/axis based on a TopCount

I have cube which contains geographic sales data. I want to pull sales of product items from all stores, based on the top 5 sellers from an arbitrary known store (BTW this is a simplified version of the real issue).

In sql (shame on me) this would be

select StoreId, ProductId, Sales from cube where ProductId in 
    (select top 5 ProductId from cube where Store = @Store order by Sales desc)

You can use the Topcount function for this. Provided you have dimensions named Product and Store and a measure named [Sales]:

Select [Measures].[Sales] On Columns,
       CrossJoin([Store].Members,Generate(Topcount(Crossjoin({[Store].@[<StoreId>]},[Product].Members),5,[Measures].[Sales]),[Product].CurrentMember)) On Rows
  From [Yourcube]

Replace <StoreId> with the specific store you're interested in. Generate will loop through the top 5 found for the specific store and return only the Product members. This result is then crossjoined with all the stores.

This will extract the top 5 products for your-store in ROWS axis and then select the [Sales] of these products according to the defaultMember of the [Store] dimension. In case [Store] defaultMember is the root of an aggregatable hierarchy this should give the sales of each product over all the stores.

SELECT

[Measures].[Sales] ON 0,

TopCount( [Store].[your-store] * [Product].Members ), 5, [Measures].[Sales] ) ON 1

FROM [your-cube]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM