MDX exclude filter in total calculation - mdx

The following query will return results for Cycling Cap product now please ignore the actual calculation I’m just using this as an example. What I’m interested in is that I get the results based on the filtering of data using [Product].[Model Name].&[Cycling Cap] see below.
WITH
MEMBER [Measures].[Unit Costing] AS
(
([Measures].[Total Product Cost] / [Measures].[Unit Price])* [Measures].[Internet Sales Count]
), format_string = '#,###,###,##0'
SELECT NON EMPTY {
[Measures].[Sales Amount]
, [Measures].[Total Product Cost]
, [Measures].[Unit Price]
, [Measures].[Internet Sales Count]
, [Measures].[Unit Costing]
} ON COLUMNS, NON EMPTY
{
{
[Order Date].[Calendar].[Calendar Year].&[2008] *
[Order Date].[Calendar Month].[Calendar Month] *
[Product].[Model Name].&[Cycling Cap]} -- if this is removed second set of results returned
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Adventure Works Cube]
What I get retuned is
Sales Amount Total Product Cost Unit Price Internet Sales Count Unit Costing
2008 August Cycling Cap 2876.8 2215.136 2876.8 320 246
2008 February Cycling Cap 5915.42 4554.8734 5915.42 658 507
2008 July Cycling Cap 2400.33 1848.2541 2400.33 267 206
2008 March Cycling Cap 5483.9 4222.603 5483.9 610 470
Now if I take out [Product].[Model Name].&[Cycling Cap] the reults are
Sales Amount Total Product Cost Unit Price Internet Sales Count Unit Costing
2008 August 4113748.61980021 2419644.0697 4113748.61980021 8596 5,056
2008 February 8388478.68060071 4930341.31220008 8388478.68060071 17236 10,130
2008 July 4154919.58680019 2445288.84530001 4154919.58680019 8550 5,032
2008 March 8358856.30390072 4919934.72500009 8358856.30390072 17067 10,045
So is it possible to have the Unit Cost return the unfiltered value of 5,056 for August instead of 246 which is what I’m currently getting.

If you tuple each of the measures with the ALL member in the calculation then it should force it to be against ALL rather than Cycling Cap:
WITH
MEMBER [Measures].[Unit Costing] AS
(
[Measures].[Total Product Cost]
,[Product].[Model Name].[All]
)
/
(
[Measures].[Unit Price]
,[Product].[Model Name].[All]
)
*
(
[Measures].[Internet Sales Count]
,[Product].[Model Name].[All]
)
,format_string = '#,###,###,##0'
SELECT
NON EMPTY
{
[Measures].[Sales Amount]
,[Measures].[Total Product Cost]
,[Measures].[Unit Price]
,[Measures].[Internet Sales Count]
,[Measures].[Unit Costing]
} ON COLUMNS
,NON EMPTY
-- if this is removed second set of results returned
{
[Order Date].[Calendar].[Calendar Year].&[2008]*
[Order Date].[Calendar Month].[Calendar Month]*
[Product].[Model Name].&[Cycling Cap]
} ON ROWS
FROM [Adventure Works];

Related

MDX calculated non-measured member

I am trying to set a calculated member to create alternate roll up for out legal entities.
Company XYZ as Co X + Co Y + Co Y.
I would to have all measures (expense, revenue etc) calculate for this new roll up.
Welcome to SO. Take a look at the following example, I am trying to add a calculated member to the Product.Category dimension attribute. This new member is called "Bikes&Accessories" and it results the data for any measure for "Bikes" & "Accessories" categories.
The first query shows the initial data
select
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Order Quantity],
[Measures].[Internet Tax Amount],
[Measures].[Internet Gross Profit Margin]
}on 0 ,
[Product].[Category].allmembers
on 1
from
[Adventure Works]
Result
Now lets add the new calculated member. Observe that I have used different measures, each one is aggregated based on its type.
with member [Product].[Category].[Bikes&Accessories]
as
aggregate({[Product].[Category].&1,[Product].[Category].&[4]},[Measures].[Measures].currentmember)
select
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Order Quantity],
[Measures].[Internet Tax Amount],
[Measures].[Internet Gross Profit Margin]
}on 0 ,
[Product].[Category].allmembers
on 1
from
[Adventure Works]
Result

MDX query for bottomcount function

SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
NonEmpty([Customer].[Customer].[Customer].MEMBERS)
,10
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[Calendar Year].&[2005];
The above query is showing me result having last 10 customer names with NULL measure value. I am using Adventure works Cube.
As per my understanding "bottomcount" is not working fine with where clause in "mdx" query. I want to fetch last 10 customer names for 2005 with measure value and hopefully i am looking for solution without using keyword "DESCENDANTS".
Please let me know in case, I am doing something wrong.
Give this a try:
bottomcount(
nonempty(
[Customer].[Customer].[Customer].Members
,[Measures].[Internet Sales Amount]
)
,10
,[Measures].[Internet Sales Amount]
)
You were getting the Customer names with NULL because in OLAP you are doing the cartesian product between the 2 dimensions (Measures is a special dimension, but it still acts as a dimension) it does not matter that you don't have values it will still crossjoin the members in your 2 sets and unless you request just the nonempty ones it will still give you the NULLs.
And if you'd like their full internet sales amount then move some of the logic into the WITH clause:
WITH
SET bot AS
NonEmpty
(
[Customer].[Customer].[Customer].MEMBERS
,(
[Measures].[Internet Sales Amount]
,[Date].[Calendar].[Calendar Year].&[2005]
)
)
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
bot
,10
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works];

MDX how do you return the Average over a number of months

I've been looking at AVG(MDX) and would like to be able to get an average over a number of months.
Using this as a simpla MDX:
SELECT NON EMPTY
{[Measures].[Freight]} ON COLUMNS
,NON EMPTY
{[Due Date].[Calendar Month].[Calendar Month].MEMBERS} ON ROWS
FROM [Adventure Works Cube];
It will return:
Is it possible to return:
Freight
August 108567.2463
February 208635.5203
July 103180.7446
March 209712.7297
November 103873.3682
Avg Freight 146793.9218
I have tried:
WITH MEMBER [Measures].[Average Freight] AS
AVG([Due Date].[Calendar Month].[All],
[Measure].[Sale])
SELECT NON EMPTY
{[Measures].[Freight],
[Measures].[Average Freight]} ON COLUMNS
,NON EMPTY
{[Due Date].[Calendar Month].[Calendar Month].MEMBERS} ON ROWS
FROM [Adventure Works Cube];
It just returns:
If you do this you do not get the average:
WITH
MEMBER [Measures].[Average Freight] AS
Avg([Measures].[Freight Cost])
SELECT
{
[Measures].[Average Freight]
,[Measures].[Freight Cost]
} ON COLUMNS
,NON EMPTY
[Date].[Calendar].[Month].MEMBERS ON ROWS
FROM [Adventure Works];
The average is just the same as the actual measure:
To do an average you need a SET to do the average over:
WITH
MEMBER [Measures].[Average Freight] AS
Avg
(
[Date].[Calendar].CurrentMember.Children
,[Measures].[Freight Cost]
)
SELECT
{
[Measures].[Average Freight]
,[Measures].[Freight Cost]
} ON COLUMNS
,NON EMPTY
[Date].[Calendar].[Month].MEMBERS ON ROWS
FROM [Adventure Works];
So the average measure now makes sense:
Below a tested example with Adventure Works. msdn Avg (MDX) link
WITH MEMBER [Measures].[Average Freight] AS
AVG ([Measures].[Freight Cost]
)
SELECT
[Measures].[Average Freight] ON COLUMNS,
NON EMPTY [Date].[Calendar].[Month].Members ON ROWS
FROM
[Adventure Works];
Result
Average Freight
July 2005 24067.9287
August 2005 51115.0148999999
September 2005 40996.0152
October 2005 33951.2746
....

MDX, first SUM then AVERAGE

I have a fact model that contains the following data:
WorkOrderNumber | WorkOrderLineNumber | Cost
So I have a dimension WorkOrder:
[WorkOrder].[WorkOrderNumber]
[WorkOrder].[WorkOrderLineNumber]
And a Measure group with the following Measure:
[Measures].[Cost]
I am trying to create a calculate measure:
[Measures].[Average WorkOrder Cost]
This must be calculated by Summing up the values per Work Order and afterwards taking an average of all these sums per workorders.
However I can not seem to get it working.
CASE WHEN [WorkOrder].[WorkOrder].CurrentMember = [WorkOrder].[WorkOrder].[All]
THEN
/* the work order is not selected -> AVG*/
DIVIDE(SUM(),[Measures].[Cost]), Count()) ))
ELSE
/* the Work Order is selected -> SUM*/
SUM([Measures].[Cost])
END
Here is an example of taking an average per sales order using the Adventure Works cube. You could replace with your [Cost] and [WorkOrderNumber]...
with member [CalculatedAvg] as
AVG([Internet Sales Order Details].[Sales Order Number].[Sales Order Number], [Measures].[Internet Sales Amount])
, FORMAT_STRING = "$#,##0.00"
select
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Order Count],
[Measures].[Internet Average Sales Amount],
[CalculatedAvg]
} on 0,
[Date].[Calendar].[Calendar Year].members on 1
from
[Direct Sales]
for example...
AVG([WorkOrder].[WorkOrderNumber].[WorkOrderNumber], [Measures].[Cost])

MDX SSAS - Max Date in Measure

Just need to get MAX date in ALL my Measures in the Cube. For instance, DateID is a Dimention, [Measure].[First Measure],...,...,[Second Measure].
How to get list of MAX(DateID) from all Measures in my Cube.
The following will get you the max date value associated with each measure...but you will have to manually create a calculated member corresponding to each measure.
WITH
MEMBER [Measures].[Max Date - Internet Sales Amount] AS
TAIL(
NONEMPTY(
[Date].[Date].[Date]
,[Measures].[Internet Sales Amount]
)
,1
).Item(0).MemberValue
MEMBER [Measures].[Max Date - Reseller Sales Amount] AS
TAIL(
NONEMPTY(
[Date].[Date].[Date]
,[Measures].[Reseller Sales Amount]
)
,1
).Item(0).MemberValue
SELECT
{
[Measures].[Max Date - Internet Sales Amount],
[Measures].[Max Date - Reseller Sales Amount]
} ON 0
FROM
[Adventure Works]
If you want to get the single max date across all measures in the cube, you'll need to take a different approach.