MDX using except doesn't work - sql

I have the following Problem:
Select
{
[Measures].[PerformanceTotalYtd]
} on columns,
Non Empty{
Except(([Desk].[DeskName].[Trade].Members,[Time].[Year-Month-Day].[Day].&[2012]&[1]&[10]),([Desk].[DeskName].[Trade].Members,[Time].[Year-Month-Day].[Day].&[2012]&[1]&[09]))
} on rows
from [Cube]
where ([Entity].[Entity].&[9], [Audience].[View].&[GOD])
It exists a Dimension with the Name Desk. This Dimension has a Hierarchy with the name DeskName. The lowest Level ist Trade.
Desk: -Total -Segment -BusinessArea -Department -4th Level Portfolio -Desk -Trade
With the Query showing below, i want to show all Trades, that have the Measure "PerformanceTotalYtd" != NULL on the Date of 2012/01/10 except the Trades with the Measure "PerformanceTotalYtd" != NULL on the Date of 2012/01/09 !
Example:
Trades with Measure PerformanceTotalYtd on the 2012/01/10:
ABC 12,99
DEF 3,22
GHI 55,60
Trades with Measure PerformanceTotalYtd on the 2012/01/09:
ABC 80,00
DEF 8,78
I want the following Result because the Trade "GHI" doesn't exists on the 2012/01/09 and is new:
GHI 55,60
My Query showing below have this result:
ABC 12,99
DEF 3,22
GHI 55,60
It doesn't delete the existing Trades from the 2012/01/09.
I have a Solution in SQL but want to make it in MDX:
SELECT DD.Code, Sum(PerformanceTotalYtd) as TOTAL
FROM [Reporting_DB].[Star].[Fact_PerformanceTotal] FIS
inner join Star.Dimension_Desk DD on FIS.DeskID = DD.DeskID
WHERE FIS.TimeID = 20120110 and FIS.EntityID = 9 AND DD.Code not in ( SELECT DD.Code
FROM [Reporting_DB_HRE].[Star].[Fact_PerformanceTotal] FIS inner join Star.Dimension_Desk DD on FIS.DeskID = DD.DeskID WHERE FIS.TimeID = 20120109 and FIS.EntityID = 9 group by DD.Code)group by DD.Code
Can anybody help me please? I can't find a solution.
Sorry for my bad english!
Alex

I have found a similar example in the Adventure Works cube:
The set {[Customer].[City].&[Bell Gardens]&[CA], [Customer].[City].&[Bellevue]&[WA], [Customer].[City].&[Bellflower]&[CA]} has 3 values for [Measures].[Internet Sales Amount] in [Date].[Calendar Year].&[2002], and only 2 values in [Date].[Calendar Year].&[2004]. So we need to show measure value for member in 2002 where measure value == null in 2004.
The next MDX query achieves the desired result:
with set S as '{[Customer].[City].&[Bell Gardens]&[CA], [Customer].[City].&[Bellevue]&[WA], [Customer].[City].&[Bellflower]&[CA]}'
select
[Measures].[Internet Sales Amount] on 0,
non empty { Filter(S, IsEmpty(([Date].[Calendar Year].&[2004], [Measures].[Internet Sales Amount]))) } on 1
from [Adventure Works]
where ([Date].[Calendar Year].&[2002])
I tried to modify your example accordingly, but can't test it. Here it is:
select
{ [Measures].[PerformanceTotalYtd] } on 0,
non empty { Filter([Desk].[DeskName].[Trade].Members, IsEmpty(([Time].[Year-Month-Day].[Day].&[2012]&[1]&[09], [Measures].[PerformanceTotalYtd]))) } on 1
from [Cube]
where ([Entity].[Entity].&[9], [Audience].[View].&[GOD], [Time].[Year-Month-Day].[Day].&[2012]&[1]&[10])
In short: use Filter instead of Except.

I have the Solution for my Problem. Following Query shows the same Result as the Query of Dmitry Polyanitsa! Have a nice Day guys!
with
set [Trades Today] as NonEmpty([Desk].[DeskName].[Trade].Members, ([Measures].[PerformanceTotalYtd], [Time].[Year-Month-Day].[Day].&[2012]&[1]&[10]))
set [Trades Yesterday] as NonEmpty([Desk].[DeskName].[Trade].Members, ([Measures].[PerformanceTotalYtd], [Time].[Year-Month-Day].[Day].&[2012]&[1]&[9]))
set [Trades Difference] as Except([Trades Today], [Trades Yesterday])
Select
{
[Measures].[PerformanceTotalYtd]
} on columns,
Non Empty{
[Trades Difference]
} on rows
from [Cube]
where ([Entity].[Entity].&[9], [Audience].[View].&[GOD])

Related

join 2 mdx queries into one table

I need to merge the result 2 queries into one table. Queries are similar except one of WHERE conditions.
As far as I was able to find out while googling it is impossible to do as MDX have internal connections in database design.
I have tried to use this way: Merge 2 MDX queries
But it turns out that in 1 hour I get this error:
XML for Analysis parser: The XML for Analysis request timed out before it was completed.
I have tried to make new members like that:
member new_A AS
aggregate
(
K
,
A
)
And then
select { new_A, ...
select { A , B , C , D } on 0,
non empty { Y * Z } on 1
from X
where (except(K), L, M, N, P);
select { A , B , C , D } on 0,
non empty { Y * Z } on 1
from X
where (K, L, M, N, P);
What I need to get in the end is a table that contains values of elements A,B,C,D as columns for condition K only and for all except condition K just next to it. it can be either A, new_A, B, new_B, etc or A, B, C, D , new_A, new_B, etc.
P.S. database is extremely big and the faster it works the better :)
So i have tried to map your problem to AdventureWorks sample database.
In my problem i am trying report [internet Sales Amount] for some countries for a set of subcategories. Then I edit my query to report all subcategories except "Road Bikes" in main columns and for road bikes i use measures.RoadBikes.
Query 1
with member
measures.RoadBikes
as
( ([Product].[Subcategory].&[2],[Measures].[Internet Sales Amount]))
select
non empty
{
({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
[Measures].[Internet Sales Amount]
),
({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
measures.RoadBikes
)
}
on columns,
non empty
[Date].[Month of Year].[Month of Year]
on
rows
from
[Adventure Works]
where
({[Product].[Subcategory].&[31],[Product].[Subcategory].&[1],[Product].[Subcategory].&[2],[Product].[Subcategory].&[37],[Product].[Subcategory].&[3]})
Result
Edit the query
with member measures.UsRoadBikes as ([Product].[Subcategory].&[2],[Measures].[Internet Sales Amount])
select non empty
{({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
[Measures].[Internet Sales Amount])}
on columns,
non empty
({[Product].[Subcategory].&[31],[Product].[Subcategory].&[1],[Product].[Subcategory].&[2],[Product].[Subcategory].&[37],[Product].[Subcategory].&[3]},
[Date].[Month of Year].[Month of Year]
) on rows
from [Adventure Works]
Result
Thanks everyone!
It was decided to run queries in parallel to speed up.
Return data to be populated into dataset and then Linq to be used to work with it.

How can i use a subquery or (WITH)statment in MDX Query?

I have a CustomerToFactor as a Measure and Customer as a Dimension. Now I want to create a MDX code like this SQL code but I can't. because (WITH) statements has another meaning in MDX.
with Total_Customer(
select cus_id
,sum(ctf_price) cus_total_price
from dbo.Customer
join dbo.CustomertoFactor on cus_id = ctf_cus_id
group by cus_id
)
select cus_id
,cus_name
,ctf_date
,ctf_price
,(cus_total_price / 100 * ctf_price) as Price_pro_customer
from dbo.Customer
join dbo.CustomertoFactor on cus_id = ctf_cus_id
join Total_Customer on Total_customer.cus_id = dbo.Customer.cus_id
SELECT NON EMPTY { [Measures].[ctf_date]
,[Measures].[ctf_price]
, (?) Price_pro_customer
} ON COLUMNS
,NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS}
FROM [CustomerToFactor]
Thanks for your Answers. but it doesn't work. Actually I want it to be grouped for every name you name. for Example: for the name Alex only the sum would have to be calculated for Alex(100+300 = 400) as well as Group by.
I do not really understand the point of the calculation :)
But anyway, in MDX you can have your own measures calculated like this:
WITH MEMBER [Measures].[Price_pro_customer] AS
(SUM([Measures].[ctf_price]) / 100 * [Measures].[ctf_price])
SELECT NON EMPTY { [Measures].[ctf_date]
,[Measures].[ctf_price]
,[Measures].[Price_pro_customer]
} ON COLUMNS
,NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS}
FROM [CustomerToFactor]
I am not sure you'll get the same result as the SQL query though, since you have [Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS on the rows which basically does a GROUP BY on the customer name.
So if in the table you had several rows for the same customer the output of MDX query should be 1 row for each customer. The SUM([Measures].[ctf_price]) is also different since it sums over all customers
I think you should create a date dimension reference to ctf_date.
Then your mdx should be as below:
WITH MEMBER [Measures].[Price_pro_customer] AS
SUM([DimDate].[ctf_date].[All], [Measures].[ctf_price]) / 100 * [Measures].[ctf_price]
SELECT NON EMPTY {
[Measures].[ctf_price] ,
[Measures].[Price_pro_customer]
} ON COLUMNS ,
NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS *
[DimDate].[ctf_date].[ctf_date].ALLMEMBERS} ON ROWS
FROM [CustomerToFactor]

MDX-Query - Query with to many rows. Set correct filter

I work on a problem with an MDX Query.
The cube contains models and serials (units) and should show all units in warranty for each year.
This is the a cube with this Dimensions/Measures:
CubeOverview
Now I would select all Serials which are in warranty for a special year.
The problem is that the whole Table v Dim Unit Model 4IB Test contains more than 50 Mio rows which results alsways to an QueryTimeout or sometimes to an MemoryException.
At the moment I have a MDX query (see below) which works if I select special model. But I need the filter to all models.
WITH
MEMBER [Measures].[QtyTotal] AS
[Measures].[QtyInWarranty] + [Measures].[QtyInExtension]
SELECT
NON EMPTY
{
[Measures].[QtyStdOut] ,[Measures].[QtyInExtension] ,[Measures].[QtyStdIn]
,[Measures].[QtyInWarranty] ,[Measures].[QtyTotal] ,[Measures].[SumStartWarranty]
} ON COLUMNS
,NON EMPTY
{
crossjoin(
[v Dim Unit Model 4IB Test].[ModelUnitMapping].[Id Unit].Members
,[Dim Country].[Id Country].[Id Country].members
,[Dim Calendar].[Calendar].[Month Name4report].members
)
} ON ROWS
FROM
(
SELECT
{
[v Dim Unit Model 4IB Test].[model no short].[Model No Short].&[SampleModel]
} ON COLUMNS
FROM
(
SELECT
{
[Dim Calendar].[Calendar].[Year].&[2015]
} ON COLUMNS
FROM [InstalledBaseCS_Serial]
)
)
Does anybody knows a tip to update the query to get all units for one year (round about 4 Mio rows)?
If you're trying to return the results to a visible grid in MDXstudio or SSMS then it may be timing out because there is quite a bit to render.
If you use OPENQUERY or the CLR OLAP Extensions then try the following:
Do not return the results to the screen but INSERT results into a table.
Simplifiy your script by taking away the custom measure. This can easily be calculated later as it is trivial: I have a feeling it is slowing down ssas.
Script
SELECT
NON EMPTY
{
[Measures].[QtyStdOut]
,[Measures].[QtyInExtension]
,[Measures].[QtyStdIn]
,[Measures].[QtyInWarranty]
,[Measures].[SumStartWarranty]
} ON 0
,NON EMPTY
[v Dim Unit Model 4IB Test].[ModelUnitMapping].[Id Unit].Members
*[Dim Country].[Id Country].[Id Country].members
*[Dim Calendar].[Calendar].[Month Name4report].members
ON 1
FROM
(
SELECT
[v Dim Unit Model 4IB Test].[model no short].[Model No Short].&[SampleModel] ON 0
FROM
(
SELECT [Dim Calendar].[Calendar].[Year].&[2015] ON 0
FROM [InstalledBaseCS_Serial]
)
);

How to mimic SQL subtraction of results from two different queries in mdx

I wanted to do the trend analysis between the dates. For an instance current date- 30 days
30-60 days and so on.Below is the snippet of comparable sql query but same I wanted to do in MDX.
SQL
SELECT
ROUND
(
(
(
(
SELECT
SUM(del_pri_impr)
FROM
reporting.so_sli_calc_val a,
reporting.user_group_tenant b,
reporting.salesorder c
WHERE
created_on BETWEEN DATE(now()-30) AND DATE(now())
)
-
(
SELECT
SUM(del_pri_impr)
FROM
reporting.so_sli_calc_val a,
reporting.user_group_tenant b,
reporting.salesorder c
WHERE
created_on BETWEEN DATE(now()-60) AND DATE(now()-30)
)
)
/
(
SELECT
SUM(del_pri_impr)
FROM
reporting.so_sli_calc_val a,
reporting.user_group_tenant b,
reporting.salesorder c
WHERE
created_on BETWEEN DATE(now()-60) AND DATE(now()-30)
) *100
)
,
0
) AS trend
MDX:
WITH
SET [~FILTER] AS
{[Created_Date.Created_Hir].[Created_On].[2014-04-01]:[Created_Date.Created_Hir].[Created_On].[2014-04-30]}
SET [~ROWS] AS
{[Sales Order Attributes SO.Sales_order].[Sales Order ID].Members}
SELECT
NON EMPTY {[Measures].[CONT_AMT_GROSS], [Measures].[CONT_AMT_NET]} ON COLUMNS,
NON EMPTY [~ROWS] ON ROWS
FROM [SALES_ORDER]
WHERE [~FILTER]
As of now I have hard coded the dates, that will come from parameters.
I am facing difficulty in creating the second set and how to do subtraction between two sets in MDX.
You already have the logic on how to obtain sets of date corresponding to "last 30 days from now" and "last 60 to last 30 days from now". So, I am going to skip that part.
NOTE - You would have to use the parameter values while building these sets.
What you want to do here is first find the values corresponding to these sets of dates and then perform operations on them.
You can proceed like this -
WITH
SET [~FILTER] AS
{[Created_Date.Created_Hir].[Created_On].[2014-04-01]:[Created_Date.Created_Hir].[Created_On].[2014-04-30]}
SET [~ROWS] AS
{[Sales Order Attributes SO.Sales_order].[Sales Order ID].Members}
SET [Last30Days] AS
...
SET [Last60ToLast30Days] AS
...
MEMBER [~Last30Days - Now] AS
Aggregate
(
[Last30Days],
[Measures].[SomeMeasure]
)
MEMBER [~Last60Days - Last30Days] AS
Aggregate
(
[Last60ToLast30Days],
[Measures].[SomeMeasure]
)
MEMBER [~Measure] AS
([~Last30Days - Now]-[~Last60Days - Last30Days] )/([~Last60Days - Last30Days] * 100), format_string = '#,##0'
SELECT
NON EMPTY {
[Measures].[CONT_AMT_GROSS],
[Measures].[CONT_AMT_NET],
[~Measure]
} ON COLUMNS,
NON EMPTY [~ROWS] ON ROWS
FROM [SALES_ORDER]
Format_String takes care of rounding.
Not sure if I totally agree with Sourav's answer as I think some form of aggregation will be needed; creating tuples with sets in them may raise an exception.
Here is a simple model, against AdvWrks, that is tested and will do a subtraction for you:
WITH
SET [Set1] AS
[Date].[Calendar].[Date].&[20060301]
:
[Date].[Calendar].[Date].&[20070308]
SET [Set2] AS
[Date].[Calendar].[Date].&[20070308]
:
[Date].[Calendar].[Date].&[20080315]
MEMBER [Date].[Calendar].[All].[Set1Agg] AS
aggregate([Set1])
MEMBER [Date].[Calendar].[All].[Set2Agg] AS
aggregate([Set2])
MEMBER [Date].[Calendar].[All].[x] AS
(
[Date].[Calendar].[All].[Set1Agg]
,[Measures].[Internet Sales Amount]
)
MEMBER [Date].[Calendar].[All].[y] AS
(
[Date].[Calendar].[All].[Set2Agg]
,[Measures].[Internet Sales Amount]
)
MEMBER [Date].[Calendar].[All].[x-y] AS
[Date].[Calendar].[All].[x] - [Date].[Calendar].[All].[y]
SELECT
{
[Date].[Calendar].[All].[x]
,[Date].[Calendar].[All].[y]
,[Date].[Calendar].[All].[x-y]
} ON 0
,[Product].[Category].[Category] ON 1
FROM [Adventure Works];
Reflecting against your code maybe something like the following:
WITH
SET [Set1] AS
[Created_Date.Created_Hir].[Created_On].[2014-04-01]
:
[Created_Date.Created_Hir].[Created_On].[2014-04-30]
SET [Set2] AS
[Created_Date.Created_Hir].[Created_On].[2014-03-01]
:
[Created_Date.Created_Hir].[Created_On].[2014-03-31]
MEMBER [Created_Date.Created_Hir].[All].[Set1Agg] AS
Aggregate([Set1])
MEMBER [Created_Date.Created_Hir].[All].[Set2Agg] AS
Aggregate([Set2])
MEMBER [Measures].[~Last30Days - Now] AS
(
[Created_Date.Created_Hir].[All].[Set1Agg]
,[Measures].[SomeMeasure]
)
MEMBER [Measures].[~Last60Days - Last30Days] AS
(
[Created_Date.Created_Hir].[All].[Set2Agg]
,[Measures].[SomeMeasure]
)
MEMBER [Measures].[~Measure] AS
([Measures].[~Last30Days - Now] - [Measures].[~Last60Days - Last30Days])
/
[Measures].[~Last60Days - Last30Days]
* 100
,format_string = '#,##0'
SET [~ROWS] AS
{
[Sales Order Attributes SO.Sales_order].[Sales Order ID].MEMBERS
}
SELECT
NON EMPTY
{
[Measures].[CONT_AMT_GROSS]
,[Measures].[CONT_AMT_NET]
,[Measures].[~Measure]
} ON COLUMNS
,NON EMPTY
[~ROWS] ON ROWS
FROM [SALES_ORDER]
WHERE
[~FILTER];

MDX Multi Level Select

I have
Geography Dimension
-> Admin Hierarchy
-> Province level
-> District level
-> Facility level.
Now I want to find the geography data when province = 'BeiJing' and facility = 'test'. How should I do ?
I write MDX code like this:
SELECT
{} ON 0,
{[Geography.Admin].[BeiJing].[All Geography.Admin].[test]} ON 1
FROM [EIDCube]
but It doesn't work. How can I get all province/district/facility data as table when I set special province and facility ?
In AdvWrks the following throws an error
Query (4, 5) The Customer Geography hierarchy is used more than once
in the Crossjoin function.
This is thrown when I try to run the following:
SELECT
[Measures].[Internet Sales Amount] ON 0
,
[Customer].[Customer Geography].[Country]
*
[Customer].[Customer Geography].[State-Province] ON 1
FROM [Adventure Works];
The way around this is to use the attribute hierarchies which the levels of the [Customer Geography] user hierarchy is made up of, like this:
SELECT
[Measures].[Internet Sales Amount] ON 0
,
[Country].[Country]
*
[State-Province].[State-Province] ON 1
FROM [Adventure Works];
Now if I want Australia and Queensland I can cross-join a couple of single member sets:
SELECT
[Measures].[Internet Sales Amount] ON 0
,
{[Country].[Country].[Australia]}
*
{[State-Province].[State-Province].[Queensland]} ON 1
FROM [Adventure Works];
Results in this:
Edit
Applying the above to your structure you should be able to do something like this:
SELECT
{[Measures].[someMeasureInCube]} ON 0
,
{[Province].[Province].[BeiJing]}
* {[Facility].[Facility].[test]} ON 1
FROM [EIDCube];
Assuming your hierarchichal structure to be
Geography-->Geography.Admin-->Province-->District-->Facility
(dimension) (hierarchy) (level 1) (level 2) (level 3)
Here is the MDX to do it the long way(short way being directly feeding the values in query, which surprisingly didn't work)
select [Geography].[Geography.Admin].CHILDREN
having
[Geography].[Geography.Admin].currentmember.member_caption = "test" and
ancestors([Geography].[Geography.Admin].currentmember, 3).item(0).member_caption = "BeiJing"
on 1,
{} on 0
from EIDCube]
I think you can use this :
SELECT
{} ON 0,
([Geography].[Admin].[BeiJing],[Geography].[Admin].[Province].[District].[test]) ON 1
FROM [EIDCube]
Edit 1 : My previous answer was wrong.
So now I think the better solution is to create a new hierarchy [admin2] for the geography dimension composed of the level [Facility] only and to use this query.
SELECT
{} ON 0,
([Geography].[Admin].[BeiJing],[Geography].[Admin2].[Facility].[test]) ON 1
FROM [EIDCube]