MDX calculate average in ALL - mdx

In the ALL column I want the average of the total income if it is 2010 and holiday, but instead I get the sum of all incomes. Maybe somebody can help?
WITH
MEMBER [Measures].[Income] AS
[Measures].[Average Income]
,FORMAT_STRING = '#.## €'
SELECT
{[Measures].[Income]} ON COLUMNS
,{
Filter
(
[Order Date].[Date].MEMBERS
,(
[Measures].[Income]
,[Order Date].[Holiday].&[True]
)
)
} ON ROWS
FROM [xxxxx]
WHERE
[Order Date].[Year].&[2010];

Will need to test but does this additional measure work?
WITH
MEMBER [Measures].[AvgIncome] AS
Avg
(
Filter
(
(EXISTING
[Order Date].[Date].MEMBERS)
,(
[Measures].[Income]
,[Order Date].[Holiday].&[True]
)
)
,[Measures].[Average Income]
)
MEMBER [Measures].[Income] AS
[Measures].[Average Income]
,FORMAT_STRING = '#.## €'
SELECT
{
[Measures].[Income]
,[Measures].[AvgIncome]
} ON COLUMNS
,{
Filter
(
[Order Date].[Date].MEMBERS
,(
[Measures].[Income]
,[Order Date].[Holiday].&[True]
)
)
} ON ROWS
FROM [xxxxx]
WHERE
[Order Date].[Year].&[2010];

Related

Convert sql rank to mdx rank

This is my sql code - I want to convert it into an mdx query.
Also those results set use into power bi report.
I am unable to write this sql query into mdx query. Can anybody help me?
Select * From(
Select
dense_RANK()over(partition by t.MonthName order by t.amount desc)rank
,t.*
From(
Select DSP.SalesPointShortName ,dd.MonthName
,SUM(fs.salesAmount)Amount
From FactSales FS
INNER JOIN DimDate dd on fs.DateKey=dd.DateKey
INNER JOIN DimSalesPoint DSP on DSP.SalesPointID=FS.SalesPointID
group by dsp.SalesPointShortName ,dd.MonthName
)as t
)as f where f.rank=1
My expected output is:
Lots of google searching i have the answer from below link
After following this link desire results comes.
https://bipassion.wordpress.com/2013/06/22/mdx-dense-rank/
`
WITH SET [Sorted Models] AS
// For each month get Top 25 records, choosed a Top 25 from business case
Generate (
[Dim Date].[Month Name].[Month Name].Members,
TopCount
( nonempty(
{
[Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name].MEMBERS
}
,[Measures].[Sales Amount]
)
, 1
,[Measures].[Sales Amount]
)
)
//Select
//[Measures].[Sales Amount] on 0,[Sorted Models] on 1
//From [MdCubeTest]
//where [Dim Product Group Item].[Sub Category Name].&[Bag]
MEMBER [Measures].[Rank] AS
// Get the Rank of current member Tuple to Current Month Set
// Do not use Sorted Models set due to dynamic for each set
Rank (
(
[Dim Date].[Month Name].CurrentMember
, [Dim Sales Point].[SalesPoint].CurrentMember
)
, Generate ( [Dim Date].[Month Name].CurrentMember,
TopCount
( nonempty(
{ [Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name]
}
,[Measures].[Sales Amount]
)
, 25
,[Measures].[Sales Amount]
)
)
, [Measures].[Sales Amount]
)
MEMBER [Measures].[Previous Set Index] AS
// Get the Set Index using Rank
(
[Measures].[Rank] - 2
)
MEMBER [Measures].[Dense Rank] AS
// Get the Dense Rank using the Index position value
CASE
WHEN [Measures].[Rank] = 1
THEN 1
ELSE
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Dense Rank]
)
+
Iif
(
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Sales Amount]
)
=
[Measures].[Sales Amount]
,0
,1
)
End
SELECT
{
[Measures].[Sales Amount]
, [Measures].[Rank]
, [Measures].[Dense Rank]
//, [Measures].[Previous Set Index]
} ON rows
,NON EMPTY
{
// FILTER Can be used to get the TOP 3 using DENSE RANK
FILTER (
[Sorted Models]
, [Measures].[Dense Rank] <=1
)
} ON columns
FROM [MdCubeTest]
where [Dim Product Group Item].[Sub Category Name].&[Bag]`

Ranking of multiple dimensions, restarting for every year

I have a measure, Sales Amount. I want to rank customers within a divison by year for that measure. I need to also display that rank as a measure. The rank needs to start over every year. I am able to do customers by year and customers by division, but I can't seem to figure out how to combine them both so it iterates over both dimensions properly. Below is what I have for the customers by year. I have tried adding another Division set, creating another named set that I GENERATE with the YearsWithCustomers set, and RANK using that new named set. I seem to be super close to figuring this out but I think I am putting something in the wrong place. I got the idea to iterate over a set from one of Chris Webb's blogs, located here.
WITH
SET Years AS
TopPercent
(
[Sales and Forecast Date].[Calendar Year].[Year Number].MEMBERS
,100
,[Measures].[Sales Amount]
)
SET Customers AS
Filter
(
[Customer].[Customer Number].[Customer Number].MEMBERS
,
[Measures].[Sales Amount] > 0
)
SET YearsWithCustomers AS
Generate
(
Years
,Union
(
{[Sales and Forecast Date].[Calendar Year].CurrentMember}
,StrToSet
("
Intersect({},
{order(Customers,([Sales Amount],[Sales and Forecast Date].[Calendar Year].CurrentMember),desc)
as CustomerSet"
+
Cstr(Years.CurrentOrdinal)
+ "})"
)
)
,ALL
)
MEMBER [Measures].[Customer Rank] AS
Rank
(
[Customer].[Customer Number].CurrentMember
,StrToSet
("CustomerSet"
+
Cstr
(
Rank
(
[Sales and Forecast Date].[Calendar Year].CurrentMember
,Years
)
)
)
)
SELECT
{
[Customer Rank]
,[Measures].[Sales Amount]
} ON 0
,Order
(
Filter
(
(
YearsWithCustomers
,Customers
)
,
[Sales Amount] > 0
)
,[Sales Amount]
,desc
) ON 1
FROM [OrdersAndBudgets];
Here is what I currently have. I would expect to see 1, 2, 3, etc for the Rank measure. It should reset for each division for every year.
I like this sort of pattern:
WITH
SET [AllCountries] AS
[Country].[Country].MEMBERS
SET [AllProds] AS
[Product].[Product].[Product].MEMBERS
SET [Top5Prods] AS
Generate
(
[AllCountries] AS a
,{
(
a.CurrentMember
,[Product].[Product].[All]
)
+
//The top x prods
a.CurrentMember
*
TopCount
(
[AllProds]
,5
,[Measures].[Internet Sales Amount]
)
}
)
MEMBER [Product].[Product].[All].[Other Products] AS
Aggregate
(
[Country].CurrentMember * [Product].[Product].[Product].MEMBERS
-
[Top5Prods]
)
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,Hierarchize(
{
[Top5Prods]
,[AllCountries] * [Product].[Product].[All].[Other Products]
}
) ON ROWS
FROM [Adventure Works];
It returns the following:
There is quite an extensive thread here: Top X of Top Y with RestOf member where X and Y are hierarchies from different dimensions

Get data from two related fact tables based on common date dimension in MDX

We have two fact tables Fact_Order and Fact_Product.
In fact_Order we have columns OrderId,OrderDate and OrderQuantity.
In fact_Product we have columns ProductReleaseDate and ProductCost
Now we want to join the Releasedate with all the orders that fall within a week in MDX.
Help would be highly appreciated because i am stuck on this from last two days
This is the MDX that i am trying to write for the same.
WITH MEMBER [Week1 order] AS
FILTER
(
Measures.OrderQuantity,
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
SELECT [Week1 order] ON 0,
NON EMPTY
{
[Release Date].[Date].[Date],
FILTER
(
[DimOrder].[OrderID].[OrderID],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
,FILTER
(
[Order Date].[Date].[Date],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
} ON 1
FROM
[ProductReleaseCube]
Does this help?
WITH SET ValidCombOfDates AS
FILTER
(
NonEmpty
(
{
[Release Date].[Date].[Date].MEMBERS * [Order Date].[Date].[Date].MEMBERS
}, Measures.OrderQuantity
),
[Order Date].[Date].CURRENTMEMBER.MEMBER_VALUE - [Release Date].[Date].CURRENTMEMBER.MEMBER_VALUE <=7
)
MEMBER Measures.ExpectedOrderQuantity AS
IIF
(
ISEMPTY(Measures.OrderQuantity),
0,
Measures.OrderQuantity
)
SELECT
ValidCombOfDates * [DimOrder].[OrderID].[OrderID].MEMBERS
ON 1,
Measures.ExpectedOrderQuantity
ON 0
FROM
[ProductReleaseCube]

MDX to count common members - EXISTS alternative

Let me describe the issue giving example from [Adventure Works] cube.
Following MDX returns count of 17473
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works])
WHERE ( [Sales Reason].[Sales Reason].&[1] -- price
and following returns count of 3515
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS ,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works]
WHERE ( [Sales Reason].[Sales Reason].&[2]) -- on promotion
I would like to count [Sales Order Number] which are common in [Sales Reason].&1 and [Sales Reason].&[2]
SQL equivalent would be:
select count(distinct f.SalesOrderNumber)
from FactInternetSales f
join FactInternetSalesReason fs
on f.SalesOrderNumber = fs.SalesOrderNumber and f.SalesOrderLineNumber = fs.SalesOrderLineNumber
where fs.SalesReasonKey = 1 and fs.SalesOrderNumber in
(select SalesOrderNumber from FactInternetSalesReason fs1 where fs1.SalesReasonKey = 2)
-- sales reason 1 = 17473
-- sales reason 2 = 3515
-- common 1689
I got common count using following mdx:
WITH MEMBER [Measures].[common] AS count
( exists ( exists ([Internet Sales Order Details].[Sales Order Number].[Sales Order Number].Members,
[Sales Reason].[Sales Reason].&[1],"Internet Orders"
),
[Sales Reason].[Sales Reason].&[2],"Internet Orders"
)
)
SELECT NON EMPTY [Measures].[common] ON COLUMNS
FROM [Adventure Works]
-- 1689
But use of EXISTS is rather slow for my requirement. Please suggest an alternative.
Also please see related thread here
Thank you
Please try adding both reasons to the WHERE clause as a single set:
SELECT
NON EMPTY
{[Measures].[Internet Order Count]} ON COLUMNS
,[Internet Sales Order Details].[Sales Order Number] ON ROWS
FROM [Adventure Works]
WHERE
{
[Sales Reason].[Sales Reason].&[2]
,[Sales Reason].[Sales Reason].&[1]
};
Here is an alternative that runs faster, only looks at the common orders, and does not use the EXISTS function:
WITH
SET [AllOrders] AS
[Internet Sales Order Details].[Sales Order Number].[Sales Order Number].MEMBERS
SET [OrdersIntersection] AS
Intersect
(
NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[1]
,[Measures].[Internet Order Count]
)
}
)
,NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[2]
,[Measures].[Internet Order Count]
)
}
)
)
MEMBER [Measures].[commonCount] AS
[OrdersIntersection].Count
SELECT
//NON EMPTY //<<not needed
[Measures].[commonCount] ON COLUMNS
FROM [Adventure Works];

MDX Calculated Member in where clause

I am new to MDX queries. I have the following query and would like to limit the results to only show records where the Margin Pct is > 0. Any help would appreciated.
WITH
MEMBER [Measures].[Margin Pct] as ([Measures].[Mgmt Margin Excluding Markup]/[Measures].[Net Sales])*100,format_string="0.0"
MEMBER [Measures].[Mgmt Margin] as [Measures].[Mgmt Margin Excluding Markup],format_string="0.0"
MEMBER [Measures].[Mgmt Cost Unit] as [Measures].[Mgmt Cost Unit Excluding Markup],format_string="0.00"
MEMBER [Measures].[FOBPrce] as [Measures].[FOB Price],format_string="0.00"
MEMBER [Measures].[CommUnt] as [Measures].[Comm/Unit],format_string="0.000"
MEMBER [Measures].[RebUnt] as [Measures].[Reb/Unit],format_string="0.00"
MEMBER [Measures].[FrtUnt] as [Measures].[Frt/Unit],format_string="0.00"
MEMBER [Measures].[PriceUnt] as [Measures].[Price/Unit],format_string="0.00"
SELECT NON EMPTY {
[Measures].[Rpt Inv Shp Date],
[Measures].[Lbs Shipped],
[Measures].[Net Sales],
[Measures].[FOBPrce],
[Measures].[CommUnt],
[Measures].[RebUnt],
[Measures].[FrtUnt],
[Measures].[PriceUnt],
[Measures].[Mgmt Cost Unit],
[Measures].[Mgmt Margin],
[Measures].[Margin Pct]
} ON COLUMNS, NON EMPTY {
(
[Item].[Group Sort].[Group Sort],
[Item].[Form Sort].[Form Sort],
[Item].[Specie Sort].[Specie Sort],
{[Item].[Group thru Item ID].[Group].ALLMEMBERS},
[Shrimp Group].[Shrimp Group].[Shrimp Group Name].ALLMEMBERS ,
{[Item].[Form].[Form].ALLMEMBERS},
[Item].[Meat - In Shell].[Meat or Inshell].ALLMEMBERS ,
[Item].[Super Specie].[Super Specie].ALLMEMBERS ,
{[Item].[Species].[Species].ALLMEMBERS},
{[Item].[Item ID].[Item ID].ALLMEMBERS},
[Item].[Desc-ItemID].[Item ID Description].ALLMEMBERS ,
[Item].[Package Type].[Packaging].ALLMEMBERS ,
{[Brand].[Brand].[Brand Name].ALLMEMBERS},
{[Warehouse].[Warehouse].[Warehouse Code].ALLMEMBERS},
[Order Invoice Lot].[Order-Invoice-Lot].[Lot].ALLMEMBERS ,
{[Customer Account Number].[Customer Account No].Levels(1)},
{[Ship To Customer].[Customer Name].Levels(1)},
{[Sales Person].[Person].Levels(1)},
[Order Invoice Lot].[Sales Order].[Sales Order].ALLMEMBERS ,
[Order Invoice Lot].[Invoice].[Invoice].ALLMEMBERS
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM (
SELECT StrToSet( '{[Breaded Group].[Breaded Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Inventory Category].[Inventory Category].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Customer Buying Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Ship To Customer Sales Group].[Ship To Customer Sales Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Customer Legal Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Country Of Origin].[Long Name].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Is Sample].[Sample].[Description].[Regular]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Invoicing Status].[Invoicing Status-Detail].[Detail].[Sale Only],[Invoicing Status].[Invoicing Status-Detail].[Detail].[Credit Only]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Invoice Date].[Fiscal Year-Quarter-Month].[Fiscal Month].[Jul-FY13]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Name].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Is NRV].[NRV].[All]}' ,CONSTRAINED )
ON COLUMNS FROM [FishTrackerReporting] ) ) ) ) ) ) ) ) ) ) )
I have tried to use a where clause against the [Measures].[Margin Pct] but get this error:
The WHERE clause function expects a tuple set expression for the argument. A string or numeric expression was used.
I also tried to use a filter after the on columns part of the query but get out of memory issues so I think I am missing something.
Too bad the reference of MDX is not the best. You can use HAVING or FILTER to achieve what you want. I'd go with HAVING since it's easier to use.
Please have a look here and find the last example.