MDX error with aggregate - ssas

I have the following query with MDX
CREATE
MEMBER CURRENTCUBE.[Measures].[Estimating Accuracy Labour Hours] AS
Sum
(
CASE
WHEN
(1
-
[Measures].[Estimation Difference]
/
Aggregate
(
[Measures].[Actual Labour Hours]
,[DISTRICTS D].[District]
,[WORK D].[Work].[Work Description]
)
)
* 100
> 0
THEN
(1
-
[Measures].[Estimation Difference]
/
Aggregate
(
[Measures].[Actual Labour Hours]
,[DISTRICTS D].[District]
,[WORK D].[Work].[Work Description]
)
)
* 100
ELSE 0
END
)
,VISIBLE = 1
,ASSOCIATED_MEASURE_ = 'WORK F' ;
I got the following error
MdxScript(TEST) (52, 71) Too many arguments were passed to the
AGGREGATE function. The maximum argument count for the function is 2.
How to resolve it ?

The error is easier to understand than most MDX error messages!
You have three arguments in this snippet:
Aggregate
(
[Measures].[Actual Labour Hours]
,[DISTRICTS D].[District]
,[WORK D].[Work].[Work Description]
)
Here is the MSDN definition: https://msdn.microsoft.com/en-us/library/ms145524.aspx
Aggregate(Set_Expression [ ,Numeric_Expression ])
Following this definition, move your Numeric_Expression to the end and the other expressions in front of it:
Aggregate
(
,[DISTRICTS D].[District]
,[WORK D].[Work].[Work Description]
,[Measures].[Actual Labour Hours]
)
Still should be erroring. You then need to make a tuple set out of the first two arguments:
Aggregate
(
{(
[DISTRICTS D].[District]
,[WORK D].[Work].[Work Description]
)}
,[Measures].[Actual Labour Hours]
)
Your error message should now go away.

Related

DAX conditional measure errors out *Single Value for Column*

Sale Type is an attribute and is returning a Single Value error.
This expression works great in SQL but will not work here.
If ( [Sale Type] = "Upsell"
&& [Current Name] = "SaaS"
&& DATEDIFF ( [StartDate],[EndDate],Day ) + 1 > 364,
SUM ( [ARR] ),
SUM ( [TotalPrice] )
)

Convert SQL query to MDX - have Group by & Count Functions

I have the following SQL query which I am trying to convert into MDX:
select avg(skucount)
from
(
SELECT count(distinct [SKUCode]) as skucount
--,[SHOPCODE_WITHOUT_DIST]
FROM [HFPL_DW].[dbo].[FactSecondarySales]
where DISTCODE in
(
SELECT [DISTRIBUTORCODE]
FROM [HFPL_DW].[dbo].[DimDistHierarchy]
where REGION = 'KARACHI'
)
and month(saledate) = 7 and year(saledate) = 2018
group by [SHOPCODE_WITHOUT_DIST]
) as inner_query
The inner query returns the count of SKU saled on each shop(which is fulfilled by using "Group by ShopCode")
First I am trying to convert the inner query to MDX, I have tried the following:
WITH MEMBER [Measures].[SKU Count] AS
COUNT( NonEmpty( { [Product Hierarchy].[SKU].[SKU].Members }, ( [Shop Hierarchy].[SHOPCODE WITHOUT DIST] ) ) )
SELECT
{
[Measures].[SKU Count]
} ON COLUMNS,
NonEmpty(
{ [Product Hierarchy].[SKU].[SKU].Members },
([Shop Hierarchy].[SHOPCODE WITHOUT DIST] )
) ON ROWS
FROM
[Consolidated Sales]
where(
[Time Analysis].[Month].&[2018-07-01T00:00:00],
[Distribution Hierarchy].[DISTRIBUTORCODE].&[1002]
)
Reference: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/51988607-78cc-4520-88db-c6d3e99dd1fc/mdx-to-count-the-number-of-members-in-a-dimension-based-on-another-dimension?forum=sqlanalysisservices
It is not returning anything.
Kindly help me acheive the desired output of the Average SKUs saled(The outer query), the number of SKUs saled per shop (inner query)

MDX Calculation speed - CurrentMember?

I am still getting to grips with MDX and I am looking for some help. Here is my MDX query:
CREATE MEMBER CURRENTCUBE.[Measures].[Fake]
AS
IIF(
(
(
[Sales Data].[Ship-to No].CurrentMember
,[Sales Data].[Price Type].&[Core]
,[Measures].[Sales - Local Currency]
)
/ (
[Sales Data].[Ship-to No].CurrentMember,
[Sales Data].[Price Type].[(All)].[ALL],
[Measures].[Sales - Local Currency]
)
) > 0.9
,1
,NULL
),
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[Measures].[Test Calc]
AS
SUM(
Descendants(
[Sales Data].[Ship-to No].CurrentMember
,[Sales Data].[Ship-to No].[Ship-to No]
),
[Measures].[Fake]
),
VISIBLE = 1;
SalesData is the fact table and the basic calculation is to count the number of customers that have sales of more than 90% of the price type "Core".
The query currently takes around 5 seconds to complete but I think this can be faster if I don't use CurrentMember according to http://sqlblog.com/blogs/mosha/archive/2008/07/29/product-volatility-optimizing-mdx-with-mdx-studio.aspx
But I don't know where to begin in changing this, can anyone help me make this more efficient?
If you delete currentmember from the first measure does it make any difference?
CREATE MEMBER CURRENTCUBE.[Measures].[Fake]
AS
IIF(
(
(
[Sales Data].[Price Type].&[Core]
,[Measures].[Sales - Local Currency]
)
/ (
[Sales Data].[Price Type].[(All)].[ALL],
[Measures].[Sales - Local Currency]
)
) > 0.9
,1
,NULL
),
VISIBLE = 1;
In respect of the currentmember you use in the second measure maybe EXISTING will suffice:
CREATE MEMBER CURRENTCUBE.[Measures].[Test Calc]
AS
SUM(
EXISTING [Sales Data].[Ship-to No].[Ship-to No].MEMBERS
,[Measures].[Fake]
),
VISIBLE = 1;

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]

Get data from two related fact tables based on common date dimension in MDX

We have two fact tables Fact_Order and Fact_Product.
In fact_Order we have columns OrderId,OrderDate and OrderQuantity.
In fact_Product we have columns ProductReleaseDate and ProductCost
Now we want to join the Releasedate with all the orders that fall within a week in MDX.
Help would be highly appreciated because i am stuck on this from last two days
This is the MDX that i am trying to write for the same.
WITH MEMBER [Week1 order] AS
FILTER
(
Measures.OrderQuantity,
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
SELECT [Week1 order] ON 0,
NON EMPTY
{
[Release Date].[Date].[Date],
FILTER
(
[DimOrder].[OrderID].[OrderID],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
,FILTER
(
[Order Date].[Date].[Date],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
} ON 1
FROM
[ProductReleaseCube]
Does this help?
WITH SET ValidCombOfDates AS
FILTER
(
NonEmpty
(
{
[Release Date].[Date].[Date].MEMBERS * [Order Date].[Date].[Date].MEMBERS
}, Measures.OrderQuantity
),
[Order Date].[Date].CURRENTMEMBER.MEMBER_VALUE - [Release Date].[Date].CURRENTMEMBER.MEMBER_VALUE <=7
)
MEMBER Measures.ExpectedOrderQuantity AS
IIF
(
ISEMPTY(Measures.OrderQuantity),
0,
Measures.OrderQuantity
)
SELECT
ValidCombOfDates * [DimOrder].[OrderID].[OrderID].MEMBERS
ON 1,
Measures.ExpectedOrderQuantity
ON 0
FROM
[ProductReleaseCube]