Niks
Niks

Reputation: 1007

how to use Group by with top count in MDX query

I have an example where we prepared query in sql for fetching appropriate results

SQL Query-

select partnerid,BrandDesc,ActualRetailValue
from
(
select DENSE_RANK() over (partition by partnerid order by sum(ActualRetailValue) desc) as rnk,
partnerid,BrandDesc,sum(ActualRetailValue) as ActualRetailValue
from JDASales
where partnerid in (693,77)
group by partnerid,BrandDesc
) as A
where rnk <=5
order by partnerid,rnk

Output -

enter image description here

I want this result with mdx query.Even tryout with this code

SELECT 
  NON EMPTY 
    {[Measures].[Actual Retail Value]} ON COLUMNS
 ,NON EMPTY 
      [DimBrands].[Brand].[Brand].ALLMEMBERS
    * 
      TopCount
      (
          [DimPartners].[Partner].[Partner].ALLMEMBERS
        * 
          [DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
       ,5
       ,[Measures].[Actual Retail Value]
      ) ON ROWS
FROM 
(
  SELECT 
    {[DimPartners].[Partner].&[1275]} ON COLUMNS
  FROM 
  (
    SELECT 
      {[Dim Date].[Fiscal Year].&[2014-01-01T00:00:00]} ON COLUMNS
    FROM [SALES]
  )
)
WHERE 
  [Dim Date].[Fiscal Year].&[2014-01-01T00:00:00];

Upvotes: 1

Views: 975

Answers (1)

whytheq
whytheq

Reputation: 35557

You can amend the rows snippet to use the GENERATE function:

SELECT 
  NON EMPTY 
    {[Measures].[Actual Retail Value]} ON 0
 ,NON EMPTY 
    GENERATE( 
     [DimBrands].[Brand].[Brand].ALLMEMBERS AS B
    , 
     TopCount(
          B.CURRENTMEMBER
         *[DimPartners].[Partner].[Partner].ALLMEMBERS
         *[DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
       ,5
       ,[Measures].[Actual Retail Value]
      )
    ) ON ROWS
...
...

This functions usage is detailed here: https://msdn.microsoft.com/en-us/library/ms145526.aspx

Upvotes: 3

Related Questions