filter in a mdx query is not correctly applied - ssas

I have a MDX Filter problem, which I think I use correctly however I still get some rows which I don't want to get.
Sample query is :
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All]} ON COLUMNS,
NON EMPTY ( ([dim_ProductModel].[Product Model].&[DSDB])
* FILTER( ([dim_Country].[Country Name].members -[dim_Name].[Country Key].[All]),[Dim_Date].[Date Full].&[2014-01-01]*[Measures].[IB] > 0 AND NOT ISEMPTY ([Dim_Date].[Date Full].&[2014-01-01]*[Measures].[IB] ))
* {[Dim_Date].[Date Full].&[2013-02-01]:[Dim_Date].[Date Full].&[2014-01-01]})
ON ROWS FROM [cub_dashboard_spares]
Now what I need, is to exclude those countries! (Filter) for particular product model [DSDB] in this case , where in january 2014 (dimension) the measure IB was > 0 or not null. Now it seems that it filters out correctly some countries however I still get some results, where either IB is 0 in last month OR IB is (null) in last month (January 2014 in our case).
Could please anybody help me where can be the problem?
Thank you very much

I would add the country to the filter condition, i. e. use
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All]}
ON COLUMNS,
NON EMPTY
[dim_ProductModel].[Product Model].&[DSDB]
* FILTER(([dim_Country].[Country Name].members -[dim_Name].[Country Key].[All]),
([dim_Country].[Country Name].CurrentMember, [Dim_Date].[Date Full].&[2014-01-01], [Measures].[IB]) > 0
AND NOT
ISEMPTY(([dim_Country].[Country Name].CurrentMember, [Dim_Date].[Date Full].&[2014-01-01], [Measures].[IB]))
)
* {[Dim_Date].[Date Full].&[2013-02-01]:[Dim_Date].[Date Full].&[2014-01-01]}
ON ROWS
FROM [cub_dashboard_spares]

this is my problem..: don't understand why the results with AFR 0 appear :(

Related

SSAS : Filter Cube

I have dimension and fact table. The dimension is PATIENTDBOID meanwhile the fact table is Total_Admissions.
Now, I want to filter for Patient that Admission >1
Can someone help me how. ?
You Can use Filter Function to Apply filter on Measures with Logical Expression.
Since I don't know Exact Dimension Name, You can try something like below.
0 - Represents COLUMNS
1 - Represents Rows
SELECT [Measures].[Total_Admissions] ON 0,
FILTER(
[Patient].[Patient].[PatientDBOID].MEMBERS
, [Measures].[Total_Admissions]>1)
ON 1
FROM [Cube Name]

Mondrian MDX - Filter Not Applying to Multiple Members Query

We currently have a query that shows the value of total quantity, or sales across store locations and departments, I intersect the quantity sold aggregated measure against locations, and the results are fine.
When we try to filter by total qty > 500, I am only seeing the sum of quantity evaluated by the location's value, not grouped by location and department.
HAVING is not seemingly supported, and if I include a where filter at the bottom, there is an issue with the same family/members being used multiple times and it's not allowed.
SELECT
{HEAD([dim_productfamily.hier_productfamily].[lvl_department].Members, 5)}
ON ROWS,
FILTER
(
{HEAD([dim_locations.hier_locations].[lvl_location].Members, 5) * [Measures].[total_qty]},
[Measures].[total_qty] > 500
)
ON COLUMNS
FROM
[sales_daily]
WHERE
{[dim_date.hier_date].[lvl_date].[20170521] : [dim_date.hier_date].[lvl_date].[20170730] }
The above query returns fine, but I get values that I've tested only really compare against the location sum(total_qty).
EDIT for Different Grouping
I tried using the below query, which seems to work. I think the way we render a table is improper in this case, as the output does seem to work fine.
SELECT
FILTER
(
{HEAD([dim_productfamily.hier_productfamily].[lvl_department].Members, 5) * HEAD([dim_locations.hier_locations].[lvl_location].Members, 5)},
[Measures].[total_qty] > 26
)
ON ROWS,
[Measures].[total_qty]
ON COLUMNS
FROM
[sales_daily]
WHERE
{[dim_date.hier_date].[lvl_date].[20170521] : [dim_date.hier_date].[lvl_date].[20170730] }
Is this what you were thinking?
The issue is that in your filter you really have not grouped by location and department. You have only grouped by location. An easy fix is the you bring both location and product on the same axis and that the measure on the oposite axis. Then filter it. It would work.
Looking at the problem again, you can query it in the below manner. This will let you use the orignal grouping.
WITH
MEMBER [Measures].[Data Type3] AS ([Geography].[Country].CurrentMember,[Product].[Product].CurrentMember,[Measures].[Reseller Sales Amount])
SELECT
FILTER
(
{[Product].[Product].Members}
,[Measures].[Reseller Sales Amount] > 2000
)
ON rows,
FILTER
(
([Geography].[Country].members, {[Measures].[Reseller Sales Amount]
,[Measures].[Data Type3]}),
[Measures].[Reseller Sales Amount] > 2000
)
ON columns
FROM
[Adventure Works]

MDX where clause in subquery does not slice cube - how to understand?

This query gives me sales of one store:
select
[measures].[sales] on 0
from [MyCube]
where [store].[store].[042]
However, if I move the slicer to inside of the subquery, it gives me sales of all stores.
select
[measures].[sales] on 0
from (select
from [MyCube]
where [store].[store].[042]
)
How to understand the mechanisms behind this difference?
This is also noted in this article, but without much explanation.
----EDIT----:
I tried various things and read around for a while. I'd like to add a question: is there a scenario in which the where clause in sub-select does filter the result?
This query gives me sales of all stores in state MI (store [042] belongs to MI):
select
[measures].[sales] on 0
from (select
[store].[state].[MI] on 0
from [myCube]
where [store].[store].[042]
)
Thinking of 'inner query only filters if the filtered dimension is returned on an axis', the theory is proved wrong if I do this:
select
[measures].[sales] on 0
from (select
[store].[state].members on 0
from [myCube]
where [store].[store].[042]
)
The sub-select still returns one state MI, but the outer query returns sales of all stores (of all states).
----EDIT 4/13----:
Re-phrasing the question in AdventureWorks cube with screenshot.
Query 1: sales of one store
Query 2: it returns sales of all stores if where clause is in the sub-select.
Query 3: the two answers I got suggested that we select the dimension in an axis - here is the result - we get all cities.
select
[measures].[sales] on 0
from (select
from [MyCube]
where [store].[store].[042]
)
The above query reduces the scope of stores just to the member [042]. Make note that sub-select is executed before the actual select. So, when it comes to the select, the engine just sees a cube which has all the members in all the dimensions; but only the member [store].[store].[042] in the store dimension. It's as if the cube has been kept intact every where else but sliced off on the Store dimension.
If you go a step ahead and add the store on to one of the axes, like
select
[measures].[sales] on 0,
[store].[store].members on 1
from (select
from [MyCube]
where [store].[store].[042]
)
you would see that although the member [All] appears in the output, it actually is just comprised of only one store.
In essence, the [All] is a special member which is calculated with respect to scope of the cube. It reflects the combined effect of all the members in the cube.
In SQL terms, it is similar to:
select sales, store as [All] from
(select sales, store from tbl where store = '042') tbl
Even though you see Sales----All, it is but a reflection of sales for store [042]
Here are some other good references concerning sub-select and slicer debate:
http://bisherryli.com/2013/02/08/mdx-25-slicer-or-sub-cube/
https://cwebbbi.wordpress.com/2014/04/07/free-video-on-subselects-in-mdx/
Chris Webb's video being located here:
https://projectbotticelli.com/knowledge/what-is-a-subselect-mdx-video-tutorial?pk_campaign=tt2014cwb
This should still leave an All member:
SELECT
[measures].[sales] ON 0
FROM
(
SELECT
FROM [MyCube]
WHERE
[store].[store].[042]
);
...but the member [All] of the Store hierarchy will only now be made up of [store].[store].[042].
You can see this by adding the Store hierarchy onto ROWS:
SELECT
[measures].[sales] ON 0,
[store].MEMBERS ON 1
FROM
(
SELECT
FROM [MyCube]
WHERE
[store].[store].[042]
);
This is the AdvWorks version similar to the reference in your question:
SELECT
{[Measures].[Order Count]} ON 0
,[Subcategory].MEMBERS ON 1
FROM
(
SELECT
{
[Subcategory].[Subcategory].&[22]
} ON 0
FROM [Adventure Works]
);
It returns the member from the sub-select and the All member adjusted to take account of the subselect:
In the references article why is the [All] less than the sum of the other two - this is not down to the subselect but is in connection with the measure that he has chosen [Measures].[Order Count] which is a distinct count. If you take away the subselect you see exactly the same behaviour of the All member being less than the sum of the other subcategory members (I've marked the point at which the total of the parts becomes higher than the All member):
SELECT
{[Measures].[Order Count]} ON 0
,Order
(
[Subcategory].MEMBERS
,[Measures].[Order Count]
,bdesc
) ON 1
FROM [Adventure Works];
Order Count: on 1 order there might be several Product Subcategories - hence this behaviour.
Edit
This query of yours:
select
[measures].[sales] on 0
from (select
[store].[state].members on 0
from TestCube //<< added this!
where [store].[store].[042]
)
This inner script is not valid? Using the same dimension on an axes and the WHERE clause is not valid:
select
[store].[state].members on 0
from TestCube
where [store].[store].[042]
Edit2
An mdx script returns a cube, which may be sliced or not sliced, but nevertheless it returns a cube. The WHERE clause is used to slice the cube that is returned. If we were using a third party tool then the dimension added to the WHERE clause would go into a combobox - with say Cliffside selected. BUT the user could effectively select Ballard from that combobox - it is just a slicer. The WHERE clause is not changing the cube that is returned by the mdx script, it is just affecting what is displayed in the cellset.
WHERE is valid within a subselect. It is part of the definition:
https://msdn.microsoft.com/en-us/library/ff487138.aspx
I've never found a use case for a subselect's WHERE clause.
Edit3
This link will explain things:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ccb66ac3-0f9a-4261-8ccc-b6ecc51b6f07/is-where-clause-pointless-inside-a-subselect?forum=sqlanalysisservices
As Darren gosbell says in the answer to this question:
https://msdn.microsoft.com/en-us/library/ff487138.aspx it says that:
The WHERE clause does not filter the subspace.

MDX Range with non existing members

I have a MDX query where the string is parsed from a front-end application with forms. End users can restrict a query based on a free text field where they can input a range from invoice number and to invoice number. The query gets build based on this 2 parameters:
SELECT
{[Measures].[Amount]} ON COLUMNS,
NON EMPTY
(
(
[Invoices].[Invoice Number].[Invoice Number].[100000000]:[Invoices].[Invoice Number].[Invoice Number].[222222222])
) ON ROWS
FROM [MyCube]
However the range operator fails if an end user types in a non existing member. I think that I need to convert these fields somehow to a decimal number and than do the check with > and <.
I already have some ideas. However I am not able to get it to work. Here I try to just filter on numbers > 0 (If this works I can fill the the parameter for > and add one for <.
SELECT
{[Measures].[Amount]} ON COLUMNS,
(
FILTER(
[Invoices].[Invoice Number].[Invoice Number].members
, Cdec([Invoices].[Invoice Number].Currentmember.Properties("Key")) > 0
)
) ON ROWS
FROM [MyCybe]
However after 5 minutes I still have no response.. so cancelled the query.
I've calculated this way (it's for dates, but the idea is the same):
with
member [Measures].[RD_Key] as CDec([Report Date].[Report Date ID].Currentmember.Member_Key)
member [Measures].[ResultFilter] as [RD_Key]>20130801 and [RD_Key]<20131013
select {[Measures].[Count],[Measures].[RD_Key]} on 0
,Filter([Report Date].[Report Date ID].members,[Measures].[ResultFilter]) on 1
FROM [DATA]
If FILTER is slow maybe try using the HAVING clause. I'm assuming that no conversion of the Member_Key is needed as keys are usually numeric:
WITH
MEMBER [Measures].[Inv_Key] as
[Invoices].[Invoice Number].Currentmember.Member_Key
SELECT
{
[Measures].[Amount]
,[Measures].[Inv_Key]
} ON COLUMNS
,[Invoices].[Invoice Number].[Invoice Number].members
HAVING
[Measures].[Inv_Key] > 100000000
AND
[Measures].[Inv_Key] < 222222222
ON ROWS
FROM [DATA]

MDX Query Join Two Dimensions

I have an MDX query as follows:
WITH
MEMBER [MatCode] AS [Product].[Material]
SELECT
([MatCode]) on 0,
([Activity].[ActivityCode].[T-50051151]) ON 1
FROM
[Cube]
This returns a value such as:
MatCode
T-50051151 Null
Which tells me it is not joining the activity code to the description when I know they match up
How can I correct my MDX query to join activity code to material?
thanks
Why not try something like the following to look for areas of the cube with data? You'll can use the WHERE clause to slice by a specific measure in your cube.
SELECT
{[Activity].[ActivityCode].[T-50051151]} ON 0,
//NON EMPTY //<<include to hide nulls
{[Product].[Material].members} on 1
FROM
[Cube]
WHERE
([Measures].[someMeasure])
Your query returns the _ default _ value / cell for the tuple :
( [Activity].[ActivityCode].[T-50051151], [Product].[Material].defaultMember )
as well as the .defaultMember for every other dimension not mentionned in your query. There is nothing wrong with it.