MDX Count Query giving incorrect result - ssas

I need some help with MDX query which returns the count of leaves nodes for a selected node.
Query1 :
Select { {[Dimension1].&[Member1]},{Descendants({[Dimension1].&[Member1]},,LEAVES)} } Dimension Properties [Parent_Unique_Name] On Columns
From RepCube Where ({[attribute1].&[attrmember1],[attribute1].&[attrmember1]},([dimension2].&[1],[dimension3].&[1]))
Query2 :
With MEMBER [Dimension1].[3] AS 'COUNT({{[Dimension1].&[Member1]},{Descendants({[Dimension1].&[Member1]},,LEAVES)}}, EXCLUDEEMPTY)'
SELECT {[Dimension1].[3]} ON 0 FROM RepCube
Where ({[attribute1].&[attrmember1],[attribute1].&[attrmember1]},([dimension2].&[1],[dimension3].&[1]))
The count query Query2 should give the same count as query1 resultset count.
But it is not. Here attribute1 belong to Dimension1 only.
My assumption is that because attribute1 belong to Dimension1, count is not giving correct result. But the first query Query 1 is filtering the results correctly.
Please help me in fixing the second query to give the same count as Query1.

You need to use existing keyword to force your calcuated member to evaluate within your where clause. Use this
COUNT(existing {{[Dimension1].&[Member1]},{Descendants({[Dimension1].&[Member1]},,LEAVES)}}, EXCLUDEEMPTY)

Related

Why does a CASE WHEN give me a different result to a WHERE with the same condition?

I'm trying to use a case when and a pivot to filter some data, but get a result of a total count of 0, however, when I use the same condition in a where statement I get a result that's more what i expected.
SELECT * FROM(SELECT FYEAR,
CASE WHEN (DIAG_3_01 IN ('E10','E11','E12','E13','E14','O24') OR DIAG_4_01 IN ('E232','N251','P702')) AND (OPERTN_3_01 IN ('N26','P22','X07','X09','X10','X11') OR OPERTN_4_01 IN ('Q011','X215','X216','X273','X121')) THEN 'a'
ELSE 'Other' END AS 'Procs',
(FCE)
FROM database
) AS a
PIVOT(COUNT(FCE) FOR [Procs] IN ([a])) AS p;
So this results in a table with column name a and a row value of 0, whereas this code results in a total of about 4000:
SELECT COUNT(FCE)
FROM database
WHERE (DIAG_3_01 IN ('E10','E11','E12','E13','E14','O24') OR DIAG_4_01 IN ('E232','N251','P702')) AND (OPERTN_3_01 IN ('N26','P22','X07','X09','X10','X11') OR OPERTN_4_01 IN ('Q011','X215','X216','X273','X121'));
Unfortunately I cant share the database contents, but would appreciate any insight as to why this might be happening.
The CASE statement is like an If-statement, which only returns the very first value it founds that meets the criteria. You can also check multiple cases with WHEN and if no criteria are met, then the query will show whatever is in the ELSE statement.
The WHERE clause is just filtering the result, meaning it tell the query which records should not be ignored.

How do I get .AllMembers of Hierarchy in olap4j?

I have an MDX query like this:
SELECT {[Gender].[Gender].AllMembers} ON COLUMNS,
{[Geography].[Country].AllMembers} ON ROWS FROM [myCube]
And I am trying to get this query as an olap4j object. I do this to set my dimension:
QueryDimension genderDim = myQuery.getDimension("GENDER");
But that produces just one column of "All Genders" and "All Geographys" respectively.
How do I get them broken down like the MDX query above?
If you switch to the MEMBERS function does that help?
SELECT
{[Gender].[Gender].MEMBERS} ON COLUMNS,
{[Geography].[Country].MEMBERS} ON ROWS
FROM [myCube];

MDX WHERE vs FILTER options

I have a query in which I need to do some filtering. I can do it in a subcube, but I am wondering if I could do this in a WHERE clause without subcube. I think this solution would be faster/cleaner. I need to filter out product models with IB>0 in last month, this is my solution so far (only part of a query):
SELECT {[Measures].[AFR],[Measures].[IB]} ON COLUMNS,
([dim_ProductModel].[ODM].children)*[Dim_Date].[Date Full].children ON ROWS
FROM
(
SELECT
FILTER([dim_ProductModel].[Product Model].children,
([Measures].[IB]*[Dim_Date].[Date Full].&[2014-04-01]>0)) ON COLUMNS FROM
[cub_dashboard_spares]
)
however, I would prefer to have it in one query without subquery something like this (its not working though):
SELECT {[Measures].[AFR],[Measures].[IB]} ON COLUMNS,
([dim_ProductModel].[ODM].children)*[Dim_Date].[Date Full].children ON ROWS
FROM
[cub_dashboard_spares]
WHERE FILTER([dim_ProductModel].[Product Model].children,
([Measures].[IB]*[Dim_Date].[Date Full].&[2014-04-01]>0))
I get some error message kind of:
he MDX function CURRENTMEMBER failed because the coordinate for the ... contains a set..
I basically understand why is he not accepting is as in an WHERE clause I should be more specific but I wonder if there is some possibility to rewrite it so that it works.
I don't want that ProductModel appears in the results set.
SELECT {[Measures].[AFR],[Measures].[IB]} ON COLUMNS,
([dim_ProductModel].[ODM].children)*[Dim_Date].[Date Full].children ON ROWS
FROM
[cub_dashboard_spares]
WHERE
({[dim_ProductModel].[Product Model].children},
[Measures].[IB],
PERIODSTODATE(
[Dim_Date].[Date Full], //<<needs to be a level from your Dim_date
[Dim_Date].[Date Full].&[2014-04-01]) //<<needs to be a member from the levelyou have used in above argument
)

MDX query for getting the non null value records

I am new with MDX, Can any one of you help me out for the below queries?
1) From the above result set how can I eliminate rows which are having the null values in the column1 & column3 using below query.
WITH
MEMBER TotalPatientCnt as '[Measures].[HealthPlanPatientCnt]'
MEMBER PrevalencePercent as '([Measures].[PatientCnt]*1.00/[Measures]. [TotalPatientCnt])*100'
SELECT
NON EMPTY {[DimCP_Population].[Dr Key].[Dr Key]}
ON rows,
{
[Measures].[PatientCnt] ,[Measures].[TotalPatientCnt]
,[Measures].[PrevalencePercent]
}
ON columns
FROM [StrategyCompanionDWCube]
WHERE
(
[DimAnchorDate].[Date Key].&[20121231],
[DimCP_PCP].[PCP Key].&[124],
[DimProduct].[Product Key].&[15],
[DimHealthPlan].[Health Plan Key].&[1]
)
;
2) I have 3 dimensions & 3 measures respectively
[DimCP_PCP], [DimProduct], [DimHealthPlan]
pcpCnt, productCnt, HelahtPlanPatientCnt
I would like to build same TotalPatientCnt member based on type of dimension with parameter value, Hope needs to write case statement but i could not write case statement please help us on the below examples.
WITH MEMBER TotalPatientCnt as
case
when [DimProduct].[Product Key].&[if getting anymember] then
'[Measures].[productCnt]'
when [DimHealthPlan].[Health Plan Key].&[if getting anymember] then
'[Measures].[HealthPlanPatientCnt]'
when [DimCP_PCP].[PCP Key].&[if getting anymember] then
'[Measures].[pcpCnt]'
If I get any parameter from [DimCP_PCP] for example [DimCP_PCP].[PCP Key].&[124] needs to show the TotalPatientCnt member like below
WITH MEMBER TotalPatientCnt as '[Measures].[pcpCnt]'
If I get any parameter from [DimHealthPlan] for example [DimHealthPlan].[Health Plan Key].&[1] needs to show the TotalPatientCnt member like below
WITH MEMBER TotalPatientCnt as '[Measures].[HealthPlanPatientCnt]'
If I get any parameter from [DimProduct] example [DimProduct].[Product Key].&[15] needs to show the TotalPatientCnt member like below
WITH MEMBER TotalPatientCnt as '[Measures].[productCnt]'
To answer your first question:
As NON EMPTY does not work for the rows here, as the second column is not empty for the records to exclude, you can use the rarely documented HAVING clause like this:
...
{[DimCP_Population].[Dr Key].[Dr Key]}
HAVING NOT IsEmpty([Measures].[PatientCnt])
AND NOT IsEmpty([Measures].[PrevalencePercent])
ON rows
...
SELECT
(
[DimAnchorDate].[Date Key].&[20121231],
[DimCP_PCP].[PCP Key].&[124],
[DimProduct].[Product Key].&[15],
[DimHealthPlan].[Health Plan Key].&[1],
[Measures].[PatientCnt],
[Measures].[TotalPatientCnt]
,[Measures].[PrevalencePercent]
) On 0,
NON EMPTY {[DimCP_Population].[Dr Key].[Dr Key]}
ON 1
FROM [StrategyCompanionDWCube]
OR
Understand the difference between NON-EMPTY vs Nonempty()
Here, you might get your answer and to do clear concept of NULL and EMPTY

MDX query: filter and sum of filtered

I've just started to learn MDX and i want to do a query like that:
filter data by the cost ( i've already made that query but without the sum) like that:
SELECT [Measures].[SumOfSelled] ON 0,
FILTER ([From].[From].[City].members, [Measures].[SumOfSelled]>7000) ON 1
FROM [BI-Avia]
It's working
and it is OK
BUT!!!
I need also to show the sum of filtered elements under this filtered result by cities
I know how to find it separately:
with member [Measures].FilteredSum as sum(filter([From].From].City].members,Measures].SunOfSelled]>7000),Measures].[SumOfSelled])
select{SumOfSelled} on 0
from [BI-AVIA]
But i have to show this together!! The SUM under Filtered! two in one! I need youe help! I think it's very clear for you!!!
Just define the calculated member on the [From].[From] hierarchy and then combine both queries, using a union of sets (abbreviated with + in MDX):
with member [From].[From].FilteredSum as
sum(filter([From].[From].City].members, Measures].SumOfSelled]>7000))
SELECT [Measures].[SumOfSelled]
ON 0,
FILTER ([From].[From].[City].members, [Measures].[SumOfSelled]>7000)
+
{ [From].[From].FilteredSum }
ON 1
FROM [BI-Avia]
You could possibly define the filter as a set in the WITH clause, which would avoid that Analysis Services evaluates it twice.