MDX Question - Using Top Count with Rank/Order Function - mdx

How do I use a top count so it only returns the first 2 records with the highest sales for each sales territory group (North America, Pacific, etc...)
WITH
MEMBER [Measures].[Group_Ranking_InternetSales] AS
RANK( [Sales Territory].[Sales Territory Group].CurrentMember,
ORDER( [Sales Territory].[Sales Territory Group].[Sales Territory Group].Members , [Measures].[Internet Sales Amount], BDESC)
)
MEMBER [Measures].[Group_Territory_Ranking_InternetSales] AS
RANK(( [Sales Territory].[Sales Territory Group].CurrentMember, [Sales Territory].[Sales Territory Country].CurrentMember),
ORDER( CROSSJOIN([Sales Territory].[Sales Territory Group].CurrentMember, [Sales Territory].[Sales Territory Country].[Sales Territory Country]) , [Measures].[Internet Sales Amount], BDESC)
)
SELECT
{
[Measures].[Group_Ranking_InternetSales]
,[Measures].[Group_Territory_Ranking_InternetSales]
, [Measures].[Internet Sales Amount]
} ON COLUMNS,
(
ORDER ([Sales Territory].[Sales Territory Group].[Sales Territory Group], [Measures].[Group_Ranking_InternetSales] , BASC )
,ORDER ([Sales Territory].[Sales Territory Country].[Sales Territory Country], [Measures].[Group_Territory_Ranking_InternetSales] , BASC )
)
ON ROWS
FROM
[Adventure Works]
The end result should be that it removes the highlighted row below.

Try now
WITH
MEMBER [Measures].[Group_Ranking_InternetSales] AS
RANK( [Sales Territory].[Sales Territory Group].CurrentMember,
ORDER( [Sales Territory].[Sales Territory Group].[Sales Territory Group].Members , [Measures].[Internet Sales Amount], BDESC)
)
MEMBER [Measures].[Group_Territory_Ranking_InternetSales] AS
RANK(( [Sales Territory].[Sales Territory Group].CurrentMember, [Sales Territory].[Sales Territory Country].CurrentMember),
ORDER( CROSSJOIN([Sales Territory].[Sales Territory Group].CurrentMember, [Sales Territory].[Sales Territory Country].[Sales Territory Country]) , [Measures].[Internet Sales Amount], BDESC)
)
SELECT
{
[Measures].[Group_Ranking_InternetSales]
,[Measures].[Group_Territory_Ranking_InternetSales]
, [Measures].[Internet Sales Amount]
} ON COLUMNS,
filter(///Added this
(
ORDER ([Sales Territory].[Sales Territory Group].[Sales Territory Group], [Measures].[Group_Ranking_InternetSales] , BASC )
,ORDER ([Sales Territory].[Sales Territory Country].[Sales Territory Country], [Measures].[Group_Territory_Ranking_InternetSales] , BASC )
)
,[Measures].[Group_Territory_Ranking_InternetSales]<3) ///Added this
ON ROWS
FROM
[Adventure Works]

Related

Slow MDX Queries

I have a query which builds a month to date and year to date SSRS report for the current month & year using parameters passed to the query. The problem is that because of there are so many customers, part numbers, etc. the query is taking way too long to execute. Is there any way to write the query better or to speed it up. Sorry, I'm very new to this. Below is the query...
WITH
MEMBER [Measures].[Key for Today] AS
Format
(
Now(),'yyyyMMdd'
)
MEMBER [Measures].[Today string] AS
'[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']'
MEMBER [Measures].[Quantity Shipped MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Quantity Shipped]
)
MEMBER [Measures].[Quantity Shipped YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Quantity Shipped]
)
MEMBER [Measures].[Sales Amount MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Sales Amount]
)
MEMBER [Measures].[Sales Amount YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Sales Amount]
)
MEMBER [Measures].[Cost Amount MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Cost Amount - Sales]
)
MEMBER [Measures].[Cost Amount YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Cost Amount - Sales]
)
MEMBER [Measures].[Sales Margin MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Sales Margin]
)
MEMBER [Measures].[Sales Margin YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Sales Margin]
)
MEMBER [Measures].[Forecast Quantity MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Quantity]
)
MEMBER [Measures].[Forecast Quantity YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Quantity]
)
MEMBER [Measures].[Forecast Turnover MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Total Turnover]
)
MEMBER [Measures].[Forecast Turnover YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Total Turnover]
)
MEMBER [Measures].[Forcast Cost MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Total Cost]
)
MEMBER [Measures].[Forecast Cost YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Total Cost]
)
MEMBER [Measures].[Forecast Margin MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Margin]
)
MEMBER [Measures].[Forecast Margin YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Forecast Margin]
)
MEMBER [Measures].[Budget Quantity MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Quantity]
)
MEMBER [Measures].[Budget Quantity YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Quantity]
)
MEMBER [Measures].[Budget Turnover MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Total Turnover]
)
MEMBER [Measures].[Budget Turnover YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Total Turnover]
)
MEMBER [Measures].[Budget Cost MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Total Cost]
)
MEMBER [Measures].[Budget Cost YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Total Cost]
)
MEMBER [Measures].[Budget Margin MTD] AS
Sum
(
MTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Margin]
)
MEMBER [Measures].[Budget Margin YTD] AS
Sum
(
YTD([Date].[Dates].CurrentMember)
,[Measures].[Budget Margin]
)
SELECT
{
[Measures].[Quantity Shipped MTD],
[Measures].[Sales Amount MTD],
[Measures].[Cost Amount MTD],
[Measures].[Sales Margin MTD],
[Measures].[Margin %],
[Measures].[Forecast Quantity MTD],
[Measures].[Forecast Turnover MTD],
[Measures].[Forcast Cost MTD],
[Measures].[Forecast Margin MTD],
[Measures].[Margin %],
[Measures].[Budget Quantity MTD],
[Measures].[Budget Turnover MTD],
[Measures].[Budget Cost MTD],
[Measures].[Budget Margin MTD],
[Measures].[Margin %],
[Measures].[Quantity Shipped YTD],
[Measures].[Sales Amount YTD],
[Measures].[Cost Amount YTD],
[Measures].[Sales Margin YTD],
[Measures].[Margin %],
[Measures].[Forecast Quantity YTD],
[Measures].[Forecast Turnover YTD],
[Measures].[Forecast Cost YTD],
[Measures].[Forecast Margin YTD],
[Measures].[Margin %],
[Measures].[Budget Quantity YTD],
[Measures].[Budget Turnover YTD],
[Measures].[Budget Cost YTD],
[Measures].[Budget Margin YTD],
[Measures].[Margin %]
} ON COLUMNS,
NON EMPTY { (
[Customer].[Customer].[Customer].ALLMEMBERS *
[Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS *
[Customer Sales].[Vehicle Mode].[Vehicle Mode].ALLMEMBERS *
[Customer Sales].[Part Number].[Part Number].ALLMEMBERS ) }
ON ROWS
FROM ( SELECT ( STRTOSET(#SummaryProdGroup, CONSTRAINED) ) ON COLUMNS
FROM ( SELECT ( STRTOSET(#SalesSummaryCode, CONSTRAINED) ) ON COLUMNS
FROM ( SELECT ( STRTOSET(#BusinessType, CONSTRAINED) ) ON COLUMNS FROM [Sales])))
Where((
StrToMember
(
[Measures].[Today string],constrained
)),
IIF( STRTOSET(#BusinessType, CONSTRAINED).Count = 1, STRTOSET(#BusinessType, CONSTRAINED), [Customer].[Business Type].currentmember ),
IIF( STRTOSET(#SalesSummaryCode, CONSTRAINED).Count = 1, STRTOSET(#SalesSummaryCode, CONSTRAINED), [Customer].[Sales Summary Code].currentmember ))
Thank you
I'd be tempted to try moving the following out of your WHERE clause:
StrToMember
(
[Measures].[Today string],constrained
)
So the WITH clause could then be like the following:
WITH
MEMBER [Measures].[Key for Today] AS
Format
(
Now(),'yyyyMMdd'
)
MEMBER [Measures].[Today string] AS
'[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']'
SET [today] AS
StrToSet([Measures].[Today string],constrained)
MEMBER [Measures].[Quantity Shipped MTD] AS
Sum
(
MTD([today].ITEM(0))
,[Measures].[Quantity Shipped]
)
MEMBER [Measures].[Quantity Shipped YTD] AS
Sum
(
YTD([today].ITEM(0))
,[Measures].[Quantity Shipped]
)
...
...

MDX as Calculated Member in SSAS Cube

I have a FactBudget per sales region with fields [Budget SalesRegion] and [Sales Region]. In the dimension DimCustomer I have the fields [Sales Region] and [Customer Type].
The relation between FactBudget and DimCustomer is defined only on the [Sales Region].
I have a rather simple mdx statement that filters the [Sales Region] of the DimCustomer for a certain [Customer Type]. With the returned [Sales Region] I select the correspondent [Budget SalesRegion]:
SELECT
(
FILTER(
[Kunde].[Sales Region].members
,[Kunde].[Customer Type].CURRENTMEMBER
=[Kunde].[Customer Type].[All].[Direct Sales]
)
,[Measures].[Budget SalesRegion]
) ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
How can I translate this statement into an Calculated Member in the SSAS cube so that the selected [Customer Type] in a slicer filters the statement accordingly?
This:
SELECT
(
FILTER(
[Kunde].[Sales Region].members
,[Kunde].[Customer Type].CURRENTMEMBER
=[Kunde].[Customer Type].[All].[Direct Sales]
)
,[Measures].[Budget SalesRegion]
) ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
Is the same as this:
SELECT
[Measures].[Budget SalesRegion] ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
Or if you want to use the slicer axis:
SELECT
[Measures].[Budget SalesRegion] ON 0
FROM [BI_DWH]
WHERE [Kunde].[Customer Type].[All].[Direct Sales];
Now it is simplified I do not understand what you question is?

MDX How do you create a variance and variance % in a report

Using AdventureWorksDW2008R I have the following DataSet
SELECT NON EMPTY {
[Measures].[Sales Amount], [Measures].[Total Product Cost], [Measures].[Internet Sales Count]
} ON COLUMNS, NON EMPTY
{
([Order Date].[Calendar Year].[Calendar Year].ALLMEMBERS )
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Adventure Works Cube]
Resutls are:
Sales Amount Total Product Cost Internet Sales Count
2005 4342674.0296 2562584.6235 8949
2008 25016003.1911002 14715208.9522001 51449
Is there a way to calculate the variance of each in the report?
For example the Variance of Internet Sales Count would be:
51449 – 8949 = 42500
And the % variance would be
42500/51449 = 83%
I know I can use the following to get the Sum:
=Sum(Fields!Internet_Sales_Count.Value, "DataSet1")
Is there a way to get the 2008 value and subtract the 2005 value?
Here is one possibility:
WITH
MEMBER [Measures].[Internet Sales diff] AS
(
[Delivery Date].[Calendar Year].CurrentMember
,[Measures].[Internet Sales Amount]
)
-
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
), format_string = '#,###,###,##0.00'
SELECT
NON EMPTY
{
[Measures].[Sales Amount]
,[Measures].[Total Product Cost]
,[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales diff]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].ALLMEMBERS}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM [Adventure Works];
The result of the above is the following:
A percentage measure could then be added like this:
WITH
MEMBER [Measures].[Internet Sales diff] AS
(
[Delivery Date].[Calendar Year].CurrentMember
,[Measures].[Internet Sales Amount]
)
-
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
)
,format_string = '#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff %] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,
[Measures].[Internet Sales diff]
/
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
)
)
,format_string = '#,###,###,##0.00%'
SELECT
NON EMPTY
{
[Measures].[Sales Amount]
,[Measures].[Total Product Cost]
,[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales diff]
,[Measures].[Internet Sales diff %]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].ALLMEMBERS}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM [Adventure Works];
Results in this:
Here is a better approach using the parallelperiod function:
WITH
MEMBER [Measures].[Internet Sales PrevYr] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,(
[Measures].[Internet Sales Amount]
,ParallelPeriod
(
[Delivery Date].[Calendar Year].[Calendar Year]
,1
,[Delivery Date].[Calendar Year].CurrentMember
)
)
)
,format_string = '$#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,
[Measures].[Internet Sales Amount] - [Measures].[Internet Sales PrevYr]
)
,format_string = '$#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff %] AS
IIF
(
[Measures].[Internet Sales PrevYr] = 0
,null
,
[Measures].[Internet Sales diff] / [Measures].[Internet Sales PrevYr]
)
,format_string = '#,###,###,##0.00%'
SELECT
NON EMPTY
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales PrevYr]
,[Measures].[Internet Sales diff]
,[Measures].[Internet Sales diff %]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].MEMBERS} ON ROWS
FROM [Adventure Works];
Results:

Fix several values : mdx query

I have this mdx query :
SELECT
NonEmpty
(
{
[Dim Sales Territory].[Sales Territory Country].[Sales Territory Country]
}
) ON COLUMNS
,NonEmpty
(
{
[Due Date].[Calendar Year].[Calendar Year]
*
[Due Date].[French Month Name].MEMBERS
}
) ON ROWS
FROM [AW Cube]
WHERE
[Measures].[Sales Amount];
This gives me the result: for each month of year my sales amount
What I want to do is to get result only for 2 years ..
For one year I did this
SELECT
NonEmpty
(
{
[Dim Sales Territory].[Sales Territory Country].[Sales Territory Country]
}
) ON COLUMNS
,NonEmpty
(
{
[Due Date].[Calendar Year].&[2008]
*
[Due Date].[French Month Name].MEMBERS
}
) ON ROWS
FROM [AW Cube]
WHERE
[Measures].[Sales Amount];
But for two years i didn't found anything !!
How about :
NONEMPTY(
{ [Due Date].[Calendar Year].&[2008], [Due Date].[Calendar Year].&[2009] }
* { [Due Date].[French Month Name].MEMBERS }
) ON ROWS
This is actually a shortcut of the crossjoin function.

intersect and topcount and order function in MDX query

I use SQL Server 2008 R2 and i use SSRS and i use Adventure Work DataBase.
I write this MDX query for get 10 city that were in top ten in both years 2003 , 2004.
with
set [Best Cities in CY 2003/2004] as
intersect(
order(
topcount(
[Customer].[Customer Geography].[City],
10,
(
[Measures].[Internet Sales Amount],
[Date].[Calendar].[Calendar Year].[CY 2003]
)
),
[Measures].[Internet Sales Amount],
bdesc
),
order(
topcount(
[Customer].[Customer Geography].[City],
10,
(
[Measures].[Internet Sales Amount],
[Date].[Calendar].[Calendar Year].[CY 2004]
)
),
[Measures].[Internet Sales Amount],
bdesc
)
)
Select [Measures].[Internet Sales Amount] on columns,
[Best Cities in CY 2003/2004] on rows
From [Adventure Works]
Where
{
[Date].[Calendar].[Calendar Year].[CY 2003],
[Date].[Calendar].[Calendar Year].[CY 2004]
}
But i want get list of cities that internet sales has decreased by 35% compared to the previous year and cities among the top 10 cities in the same year as well.
How i can get this result?
You would use Filter for this:
with
set [Best Cities in CY 2003/2004] as
filter(
intersect(
topcount(
[Customer].[Customer Geography].[City],
10,
(
[Measures].[Internet Sales Amount],
[Date].[Calendar].[Calendar Year].[CY 2003]
)
),
topcount(
[Customer].[Customer Geography].[City],
10,
(
[Measures].[Internet Sales Amount],
[Date].[Calendar].[Calendar Year].[CY 2004]
)
)
),
([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].[CY 2004])
/
([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].[CY 2003])
- 1.0
< -0.35
)
Select [Measures].[Internet Sales Amount] on columns,
[Best Cities in CY 2003/2004] on rows
From [Adventure Works]
Where
{
[Date].[Calendar].[Calendar Year].[CY 2003],
[Date].[Calendar].[Calendar Year].[CY 2004]
}