Calculating total Amount of Internet Sales (AdventureWorks data) MDX - mdx

How can I count the total number of sales in the "FactInternetSales" table for the selected member of the DATES hierarchy?
(
[Order Date].[DATES].CURRENTMEMBER,
COUNT(DISTINCT([Fact Internet Sales].[Sales Order Number].[Sales Order Number]
)
this is what I have at the moment however it doesn't work :/

Try count on the outside with extra braces to indicate a set of tuples:
COUNT(
{
[Order Date].[DATES].CURRENTMEMBER
*
[Fact Internet Sales].[Sales Order Number].[Sales Order Number]
}
)

with set abc as
EXISTS([Fact Internet Sales].[Sales Order Number].[Sales Order Number],
[Order Date].[DATES].CURRENTMEMBER,
"Fact Internet Sales"
)
member [Measures].[Count]
AS Count(Distinct(abc))
SELECT [Measures].[Count] on 0
from [YourCube]
WHERE Order Date].[DATES].&[01/01/2015] --Some random value for slicer

Related

mssql: add column with the same value for all rows to search results

I have my query:
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
This gets me 3 columns:
Shipment Date | Running Costs | Total Running Costs
I would like to add a fourth column to this query which has the same value for all rows, and the same number of rows as my original query results.
I know you could add for example '999'as Something to the search results, but how can I do the same for a sum of another column (example: Imagine the total sum of the a column in another table is 1500, and I want to have 1500 for all rows in the fourth column. Something like select sum(column_name)?
The database engine is MSSQL.
You can use a nested query
SELECT [Shipment Date], [Amount] as [Running Costs], [Total Running Costs], SUM([Total Running Costs] OVER ())
FROM
(
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
)
Nested window function should also work
SUM(SUM([Running costs]) OVER (ORDER BY [Shipment Date])) OVER ()

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

Query using Group By, Top N and Sum

I have looked through the forum and can find a variety of examples to solve my problem but just cannot put everything together.
My situation is typical that I would like to show the Top 10 customers (Orders.[Customer Name]) by group (Shop_Lookup.ShopGroup]) for their total revenue.
I can get so far in producing the overall Top 10 regardless of ShopGroup but just cannot get my head around getting the Sub Query to work. My current code is -
SELECT TOP 10 Orders.[Customer Name],
Sum(Orders.[Actual Revenue]) AS [SumOfActual Revenue],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
FROM Orders
INNER JOIN Shop_Lookup ON Orders.[ShopID] = ShopLookup.[ShopID]
WHERE ((Orders.[This Month])="current")
GROUP BY Orders.[Customer Name], Orders.[This Month], Shop_Lookup.[ShopGroup]
ORDER BY Sum(Orders.[Actual Revenue]) DESC;
Completely AIR CODED ! Proceed with caution.
You can use Sub Query to get this !
SELECT
Orders.[Customer Name],
Sum(Orders.[Actual Revenue]) AS [SumOfActual Revenue],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
FROM
Orders
INNER JOIN
Shop_Lookup
ON
Orders.[ShopID] = ShopLookup.[ShopID]
WHERE
(
(Orders.[This Month] = 'Current')
AND
(Orders.ShopID IN
(SELECT
TOP 10 ShopID
FROM
Orders AS Dupe
WHERE
Dupe.ShopID = Orders.ShopID
)
)
)
GROUP BY
Orders.[Customer Name],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
ORDER BY
Sum(Orders.[Actual Revenue]) DESC;
More information on Subqueries : http://allenbrowne.com/subquery-01.html