MDX, first SUM then AVERAGE - ssas

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

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 formatting results

I'm new to MDX querying. I am having trouble changing how the MDX output is formatted. I have made a similar example using the "Adventure Works Internet Sales Model". See below:
WITH
MEMBER [Measures].[Calculate YTD] AS
Sum
(
periodstodate([Date].[Calendar].[Year],[Date].[Calendar].CurrentMember)
,[Measures].[Internet Total Sales]
)
SELECT
{[Measures].[Internet Total Sales]
,[Measures].[Calculate YTD]
} ON COLUMNS,
[Date].[Calendar].[Month] ON ROWS
FROM [Adventure Works Internet Sales Model]
WHERE ([Date].[Date].&[2012-01-01T00:00:00]:[Date].[Date].&[2018-01-01T00:00:00])WHERE ([Date].[Date].&[2012-01-01T00:00:00]:[Date].[Date].&[2018-01-01T00:00:00])
the results looks like this:
MDX results
What I would like to see is that the "[Date].[Calendar].[Month]" row to be displayed as the end of month date (e.g. 31-Mar-2019)
Welcome to SO!
You could add in an additional column showing the last day of each month:
WITH
MEMBER [Measures].[Last Day of Month] AS
[Date].[Calendar].CurrentMember.LastChild.MEMBER_CAPTION
MEMBER [Measures].[Last Day of Month v2] AS
TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBER_CAPTION
MEMBER [Measures].[Calculate YTD] AS
Sum
(
periodstodate(
[Date].[Calendar].[Year],[Date].[Calendar].CurrentMember
)
,[Measures].[Internet Total Sales]
)
SELECT
{
[Measures].[Last Day of Month]
, [Measures].[Last Day of Month v2]
,[Measures].[Internet Total Sales]
,[Measures].[Calculate YTD]
} ON COLUMNS,
[Date].[Calendar].[Month]
ON ROWS
FROM [Adventure Works Internet Sales Model]
WHERE
(
[Date].[Date].&[2012-01-01T00:00:00]:
[Date].[Date].&[2018-01-01T00:00:00]
);
Output

MDX query with count distinct filtered on another measure, that plays with other filters

in the SSAS AdventureWorks schema, I'm trying to figure out a distinct count of how many distinct customers have made Internet orders greater than $1000.
I attempted to make an MDX query like this, and it seems to almost work, except that new measure seems to ignore the slicer axis / WHERE condition, showing the count of customers for all countries, not just Australia:
With
member [measures].[DistinctCustomersHighSales] as
distinctcount(
filter([Customer].[Full Name].Members, [Measures].[Internet Sales-Sales Amount] > 1000)
)
SELECT {
[Measures].[Internet Sales-Sales Amount],
[Measures].[DistinctCustomersHighSales]
}
on columns,
([Date].[Calendar Date].[Calendar Year].Members) on rows
FROM [Analysis Services Tutorial]
WHERE [Customer].[Customer Geography].[Country-Region].[Australia]
What am I doing wrong?
If I were doing this in SQL, I would be looking for something like this
SELECT Year(OrderDate) as year,
sum(SalesAmount)
count(case when count(distinct case when SalesAmount > 1000 then dc.customerkey end)
FROM dbo.FactInternetSales fis
join dbo.dimcustomer dc on fis.CustomerKey=dc.CustomerKey
join dbo.DimGeography dg on dc.GeographyKey=dg.GeographyKey
WHERE EnglishCountryRegionName='Australia'
GROUP BY year(OrderDate)
ORDER BY year
Try adding EXISTING:
With
member [measures].[DistinctCustomersHighSales] as
distinctcount(
filter(EXISTING [Customer].[Full Name].Members, [Measures].[Internet Sales-Sales Amount] > 1000)
)
SELECT {
[Measures].[Internet Sales-Sales Amount],
[Measures].[DistinctCustomersHighSales]
}
on columns,
([Date].[Calendar Date].[Calendar Year].Members) on rows
FROM [Analysis Services Tutorial]
WHERE [Customer].[Customer Geography].[Country-Region].[Australia]
Scope is a very important concept in MDX. Only when you add EXISTING, does the engine realize the slicer.
The initial set in the definition of member is :
[Customer].[Full Name].Members
Sets are static in MDX. So, this set by default contains ALL the customers. When you add an EXISTING, before forming the set, the context is checked. Based on the dimension usage(relationship) in your cube, it is able to filter the customers belonging to Australia. Thus it works.

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 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.