MDX calculated non-measured member - ssas

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

Related

MDX exclude filter in total calculation

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];

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])

How to get an "(all)" value for a level in a user defined hierarchy in SSAS/MDX

I am trying to work out how I can get a total for a level in a user defined hierarchy. For example if I do (using Adventure works here):
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].ALLMEMBERS * [Date].[Month of Year].ALLMEMBERS) ON 1
FROM [Adventure Works]
I get totals for each pair of year/month values.
How can I rewrite this query using the Calendar user hierarchy in the Date dimensions? Eg something like:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar].[Year] * [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
You can use
SELECT [Measures].[Internet Sales Amount] ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
This will show you each year containing its value (which is the sum of the months contained in it) before the months.
The + is the short form of Union in MDX, and Hierarchize sorts a set in hierarchical order, which means parents before children.
And if you want something similar to your first result, the closest thing I can imagine would be via calculated measures like this:
WITH Member Measures.Year AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Calendar Year],
[Date].[Calendar].CurrentMember.Name,
Ancestor([Date].[Calendar].CurrentMember, [Date].[Calendar].[Calendar Year]).Name
)
Member Measures.Month AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Month],
[Date].[Calendar].CurrentMember.Name,
'All Months'
)
SELECT {
Measures.Year,
Measures.Month,
[Measures].[Internet Sales Amount]
}
ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
Note that calculated members need not return numbers, they can as well return strings.
Using allmembers as you have it should return you the ALL line and then each year and each month. If you are interested in only the all line you can change you query to:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year] * [Date].[Month of Year]) ON 1
FROM [Adventure Works]
Notice how it is specifying only the Dimension and the hierarchy.
If you wanted to get the data without the all line use the following:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].children * [Date].[Month of Year].children) ON 1
FROM [Adventure Works]

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.

SSAS - Sum of Top or Bottom

SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,Tail
(
[Date].[Calendar Year].[Calendar Year].MEMBERS
,2
) ON ROWS
FROM [Adventure Works];
Above MDX query gives me output as :
Year Internet Sales Amount
CY 2003 $9,791,060.30
CY 2004 $9,770,899.74
I understood how this query worked but I want to create Calculated measure in a cube which will always give me sum of bottom 2 years. How to do this? I am a newbie to SSAS. I am good at designing simple measures and dimensions but when it comes to using MDX, I am mostly stuck.
PS: I tried using TopCount, BottomCount etc but here I want to order by "Year", which is a dimension.
Any help would be appreciated.
Thanks,
Parry
The following query calculates a measure for sum the the last 2 years of the Date dimension:
WITH
MEMBER [Measures].[Sales from the last 2 Years]
AS Aggregate( Tail( [Date].[Calendar Year].[Calendar Year].Members, 2)
, [Measures].[Internet Sales Amount]
)
SELECT { [Measures].[Sales from the last 2 Years]
} ON COLUMNS
, { Tail( [Date].[Calendar Year].[Calendar Year].Members, 2)
} ON ROWS
FROM [Adventure Works]
Other interesting query, would be calculating a measure for the sum of each year and its previous:
WITH
MEMBER [Measures].[Sales from 2 years]
AS Aggregate( { [Date].[Calendar Year].CurrentMember.PrevMember
: [Date].[Calendar Year].CurrentMember }
, [Measures].[Internet Sales Amount]
)
SELECT { [Measures].[Internet Sales Amount]
, [Measures].[Sales from 2 years]
} ON COLUMNS
, NON EMPTY
{ [Date].[Calendar Year].[Calendar Year]
} ON ROWS
FROM [Adventure Works]
It does a sum, because the aggregation type of the measure [Measures].[Internet Sales Amount] is Sum, and the Aggregate function aggregates according to the measure's aggregation type.
Key Concepts in MDX
Aggregate function reference
MDX is a hard topic to grasp; if you're starting out, I recommend you read the book MDX Solutions, 2nd edition.