MDX as Calculated Member in SSAS Cube - ssas

I have a FactBudget per sales region with fields [Budget SalesRegion] and [Sales Region]. In the dimension DimCustomer I have the fields [Sales Region] and [Customer Type].
The relation between FactBudget and DimCustomer is defined only on the [Sales Region].
I have a rather simple mdx statement that filters the [Sales Region] of the DimCustomer for a certain [Customer Type]. With the returned [Sales Region] I select the correspondent [Budget SalesRegion]:
SELECT
(
FILTER(
[Kunde].[Sales Region].members
,[Kunde].[Customer Type].CURRENTMEMBER
=[Kunde].[Customer Type].[All].[Direct Sales]
)
,[Measures].[Budget SalesRegion]
) ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
How can I translate this statement into an Calculated Member in the SSAS cube so that the selected [Customer Type] in a slicer filters the statement accordingly?

This:
SELECT
(
FILTER(
[Kunde].[Sales Region].members
,[Kunde].[Customer Type].CURRENTMEMBER
=[Kunde].[Customer Type].[All].[Direct Sales]
)
,[Measures].[Budget SalesRegion]
) ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
Is the same as this:
SELECT
[Measures].[Budget SalesRegion] ON 0,
[Kunde].[Customer Type].[All].[Direct Sales] ON 1
FROM [BI_DWH];
Or if you want to use the slicer axis:
SELECT
[Measures].[Budget SalesRegion] ON 0
FROM [BI_DWH]
WHERE [Kunde].[Customer Type].[All].[Direct Sales];
Now it is simplified I do not understand what you question is?

Related

Brand new to SQL, looking to find total sales on a specific day

I have the following:
SELECT [Sales_Line_Item].[Sales Date],
[Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price] AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#;
Which displays:
I want to add the total sales together so that the result is:
Sales Date Total Sales
9/1/2020 $6,276.00
Thank you for any help
I wanted to use SUM(Total Sales) someway but I'm missing an operator
You can do:
SELECT [Sales_Line_Item].[Sales Date],
SUM([Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price]) AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#
GROUP BY [Sales Date]

Performance issue when converting MDX to DAX queries

I have a MDX query which I want to convert to DAX to improve performance, however the result is not as expected, MDX took 11 secs to complete while DAX was 34 secs. any suggestion to improve the DAX query
MDX Query:
SELECT
{
[Measures].[Internet Total Sales]
} ON COLUMNS,
ORDER(
NONEMPTY
(
{
[Product].[Model Name].[Model Name].AllMembers *
[Product].[Product Line].[Product Line].AllMembers *
[Product].[Product Name].[Product Name].AllMembers *
[Customer].[First Name].[First Name].AllMembers *
[Customer].[Last Name].[Last Name].AllMembers
},
{
[Measures].[Internet Total Sales]
}
),
[Product].[Model Name].CurrentMember.MemberValue, ASC
) ON ROWS
FROM [Model]
DAX Query:
EVALUATE
CALCULATETABLE
(
FILTER
(
SUMMARIZE
(
CROSSJOIN('Product', 'Customer'),
[Model Name],
[Product Line],
[Product Name],
[First Name],
[Last Name],
"Internet Total Sales",
[Internet Total Sales]
),
NOT ISBLANK([Internet Total Sales])
)
)
ORDER BY [Model Name] ASC
Thank you.
EVALUATE
SUMMARIZECOLUMNS(
'Product'[Model Name],
'Product'[Product Line],
'Product'[Product Name],
'Customer'[First Name],
'Customer'[Last Name],
"Internet Total Sales", [Internet Total Sales]
)
ORDER BY 'Product'[Model Name]

Calculating Avg Orders per day using MDX

I am trying to create a calculated member which should returns the averages orders per week per person.
Below is the screen shot where the client will be looking to forecast the work load. Staff may not work all the days in a week.
The calculation will be : No of Orders / No of scheduled days in that week.
It would not be very hard to calculate this if the week name is in the same hierarchy of the date, but in this case it is not in hierarchy but just member of the dimension.
This is the MDX I've tried:
Avg ( Descendants
( [Date Planned].[Date Planned].CurrentMember, [Date Planned].[Date Planned].[Date Planned] ),
[Measures].[Orders Qty] )
It would be much easier when you have week and day added into the hierarchy, but if you don't have possibility to have this added, there is different non elegant solution.
If you would assume that this member will always be used with Week on rows your member calculation would be as follow:
This will work correctly only with weeks on rows - so I would recommend to extend your Calendar hierarchy by week and day and then we can have a chat about much more elegant solution.
Try using this:
WITH MEMBER [Measures].[avg_week] AS
Avg ( EXISTING { [Date Planned].[Week Of Year].[Week Of Year] },
[Measures].[Orders Qty] )
SELECT [Measures].[avg_week] ON COLUMNS,
NON EMPTY
{ [Person].[PersonName].[PersonName] * [Date Planned].[Week Of Year].[Week Of Year] } ON ROWS
FROM [Your cube]
Let me know if this can help you.
Maybe something like the following:
WITH
MEMBER [Measures].[PlannedDayCnt] AS
Count
(
Exists
(
(EXISTING
[Date Planned].[Date Planned].[Date Planned].MEMBERS)
,[Date Planned].[Week Of Year].currentmember
)
)
MEMBER [Measures].[Averge Orders] AS
[Measures].[Orders Qty] / [Measures].[PlannedDayCnt]
SELECT
{
[Measures].[Orders Qty]
,[Measures].[PlannedDayCnt]
,[Measures].[Averge Orders]
} ON 0
,
[Person].[PersonName].[PersonName]
* [Date Planned].[Week Of Year].[Week Of Year] ON 1
FROM [aCube];
I'd like to test but unsure how to model your scenario in the AdvWrks cube.
I would first want to see how many "dates" belong to the week. That would be easy as it would be those days when Orders were placed for that person that week.
NonEmpty
(
[Date Planned].[Date Planned].[Date Planned].MEMBERS,
(
[Date Planned].[Week Of Year].currentmember,
[Person].[PersonName].currentmember,
[Measures].[Orders Qty]
)
)
Now that you have the requisite dates for the "current" week and the "current" person, you would need to calculate the average order quantity for these set of dates. It would look like this:
AVG
(
NonEmpty
(
[Date Planned].[Date Planned].[Date Planned].MEMBERS,
(
[Date Planned].[Week Of Year].currentmember,
[Person].[PersonName].currentmember,
[Measures].[Orders Qty]
)
)
, [Measures].[Orders Qty]
)
Your final construct would need to be:
WITH MEMBER Measures.AverageOrderPerDay AS
AVG
(
NonEmpty
(
[Date Planned].[Date Planned].[Date Planned].MEMBERS,
(
[Date Planned].[Week Of Year].currentmember,
[Person].[PersonName].currentmember,
[Measures].[Orders Qty]
)
)
, [Measures].[Orders Qty]
)
SELECT
{
[Measures].AverageOrderPerDay,
[Measures].[Orders Qty]
}
ON 0,
[Person].[PersonName].[PersonName].MEMBERS
* [Date Planned].[Week Of Year].[Week Of Year].MEMBERS ON 1
FROM [YourCube]

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