MDX: How to get rows count - mdx

How to get the row count in new column, please click the above image.
enter image description here
WITH MEMBER DimName AS [DimHealthPlan].[Health Plan Key].CurrentMember.Member_Caption
MEMBER DimKey AS [DimHealthPlan].[Health Plan Key].CurrentMember.Member_Key
MEMBER [Measures].[Test] AS ([Measures].[DrPatientKeyCnt] + [Measures].[NrPatientKeyCnt])
MEMBER RowCount AS COUNT(NONEMPTY([DimHealthPlan].[Health Plan Key].[Health Plan Key]))
SELECT {[Measures].DimKey ,
[Measures].DimName,
[Measures].[HealthPlanPatientCnt],
[Measures].[CareProviderPatientCnt],
[Measures].[PCPPatientCnt],
[Measures].[Test],
RowCount} ON COLUMNS ,
NonEmpty([DimHealthPlan].[Health Plan Key].MEMBERS) ON ROWS
FROM [PopulationReportCube]

Try this - they should all say 6:
MEMBER RowCount AS
COUNT(NONEMPTY([DimHealthPlan].[Health Plan Key].MEMBERS))
But if you want something very dynamic then you'll need to explore the Axis function:
https://msdn.microsoft.com/en-us/library/ms145531.aspx
Which could be implemented like this:
WITH
SET [Rows] AS
{Axis(1)}
MEMBER [Number of rows] AS
[Rows].Count
...
...

Related

MDX : how to create a 2x2 array with a query, using hard coded values

in MDX I would like to create a select which returns a 2x2 array, filled with hard coded values.
Here is an attempt:
WITH
MEMBER a AS 1
MEMBER b AS 2
SET un AS { a , b }
SELECT { un} ON 0,
[Dim misc].[Gender].[(All)] ON 1
FROM [my cube]
it returns:
but I would like it to return an other row, with other values than 1,2.
You have solved half the problem already. However defining a set is not necessary.
You need to do the following
Use defaultmember,
Use Case
Define a dummy member for the dimension you use.
Instead of Set just define members.
Take a look at the example below based on adventure works.
with
member Column1Row1 as 1
member Column2Row1 as 2
member Column1Row2 as 3
member Column2Row2 as 4
member Column1 as case when [Product].[Product].currentmember is [Product].[Product].defaultmember then Column1Row1 else Column1Row2 end
member Column2 as case when [Product].[Product].currentmember is [Product].[Product].defaultmember then Column2Row1 else Column2Row2 end
member [Product].[Product].[Row2]
as [Product].[Product].defaultmember
select
{Column1,Column2} on 0,
{
[Product].[Product].defaultmember,
[Product].[Product].[Row2]}
on 1
from
[Adventure Works]
Result

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]

Where Clause using another Dimension Value

I'm trying to create a Calculated Member in my cube with where clause but couldn't figure how to achieve the proper result.
I created a calculated member "Outlook" using the below code to display only Forecast values.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS SUM(([Source Profile].[Source Profile Hierarchy].CurrentMember,
[Source Profile].[Profile Level01].&[Outlook]),
[Measures].[USD Amount]),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS';
Cube Result
Now I would like to filter the results dynamically based on another hidden dimension "Current_Month". This dimension always has current financial period value and it's corresponding outlook profile
Year_Month Outlook_Profile
2015010 10 + 2
Expected result should be "Outlook" measure showing value based on Current_Month dimension, which is '10 + 2' and rest of them should be 0
Expected result
Just to explain the requirement in SQL terms, I would like to achieve the below in MDX
Where Fact.Source_Profile=Dimension.Source_Profile
instead of
Where Fact.Source_Profile='10 + 2'
I'm not sure how to achieve this in Where Clause or by another means. I could see examples of hard coding values, like year.&[2015] but haven't seen one using dynamic values.
I found a solution myself and thought of sharing the same. Used StrToMember function to pass hidden dimension's member as a variable here.
CREATE MEMBER CURRENTCUBE.[Measures].[Outlook]
AS (
Sum
(
(
[Source Profile].[Source Profile Hierarchy].CurrentMember,
strtomember("[Source Profile].[Source Name].&[" + [Outlook Profile].[Outlook Profile].&[1].Member_Caption + "]")
)
,[Measures].[USD Amount]
)
),
FORMAT_STRING = "#,##0;(#,##0)",
VISIBLE = 1 , DISPLAY_FOLDER = 'USD' , ASSOCIATED_MEASURE_GROUP = 'CARS' ;

UNION multiple MDX queries in SSAS (powerpivot)

I have some sort of difficulties trying to join 2 MDX queries together. When running them separately they work fine. The script below
WITH
MEMBER [Measures].[ParameterCaption] AS
[Main_Incidents].[Priority].CurrentMember.Member_Caption
MEMBER [Measures].[ParameterValue] AS
[Main_Incidents].[Priority].CurrentMember.UniqueName
MEMBER [Measures].[ParameterLevel] AS
[Main_Incidents].[Priority].CurrentMember.Level.Ordinal
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,[Main_Incidents].[Priority].ALLMEMBERS ON ROWS
FROM [Model];
WITH
MEMBER [Measures].[ParameterCaption] AS
[Main_Incidents].[usr_directorate].CurrentMember.Member_Caption
MEMBER [Measures].[ParameterValue] AS
[Main_Incidents].[usr_directorate].CurrentMember.UniqueName
MEMBER [Measures].[ParameterLevel] AS
[Main_Incidents].[usr_directorate].CurrentMember.Level.Ordinal
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,[Main_Incidents].[usr_directorate].ALLMEMBERS ON ROWS
FROM [Model];
The most important bit for me is that I need the label column to show. So I want to UNION the 2 queries together so that the ParameterCaption captures values from "Priority" dimension and "Directorate" dimension....
Please someone help me to achieve this?
This is a bit complex, but definitely possible.
Union in MDX only works for members of the same hierarchy, so to achieve this we need to make the row members into Tuples that combine the two hierarchies. We can do this by cross joining each of the ALLMEMBERS sets to the [All] member for the other hierarchy. Then we just need to change the Parameter Caption, Value and Level to conditionally get the value from the appropriate hierarchy.
This could look something like the code below:
WITH
MEMBER [Measures].[ParameterCaption] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.Member_Caption, [Main_Incidents].[Priority].CurrentMember.Member_Caption)
MEMBER [Measures].[ParameterValue] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.UniqueName, [Main_Incidents].[Priority].CurrentMember.UniqueName)
MEMBER [Measures].[ParameterLevel] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.Level.Ordinal , [Main_Incidents].[Priority].CurrentMember.Level.Ordinal)
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,{
[Main_Incidents].[Priority].ALLMEMBERS * [Main_Incidents].[usr_directorate].[All],
[Main_Incidents].[Priority].[All] * [Main_Incidents].[usr_directorate].ALLMEMBERS
} ON ROWS
FROM [Model];

MDX - icCube - How to get a DYNAMIC topcount/toppercent with other aggregated?

Using the following mdx, I'm able to retrieve correct data dynamically.
create CATEGORY MEMBER [Stats].[Top].[All Etabs].[Top 5 Etablissements]
as topcount( [Etablissement].[Etablissement].[Etablissement].allMEMBERS, 5, [Measures].[Nbsejours]),ADD_CHILDREN=true
create CATEGORY MEMBER [Stats].[Top].[All Etabs].[Autres Etablissements (>5)]
as Except([Etablissement].[Etablissement].[Etablissement].members, TopCount( [Etablissement].[Etablissement].[Etablissement].MEMBERS, 5, [Measures].[Nbsejours])),ADD_CHILDREN=false
create dynamic set [Top 5 & Others]
as {[Stats].[Top].[Top 5 Etablissements], [Stats].[Top].[Autres Etablissements (>5)]}
Select {[Measures].[NbSejours]} on 0,
nonempty ([Top 5 & Others]) on 1
From //[Cube]
( SELECT { {[Geographique].[Zone].[All-M].&[1006]} } ON 0 FROM [Cube])
But, the topCount is not dynamic itself. In this example, the top 5 etablissement never change, only the values do change...
Is there a way to get this with dynamic topCount/topPercent ?
Txs,
Bertrand.
Categories (*) have not yet the dynamic flag so it's not possible to define a category that will be calculated for each MDX request once as it happens for a set.
So it's going to be something more like (note I've use the SubCubeComplement that is a lot - may be really a lot - faster )
create dynamic set [Top 5] as
topcount( [Etablissement].[Etablissement].[Etablissement].members, 5, [Measures].[Nbsejours])
*** End script ***
WITH
CATEGORY HIERARCHY [Stats].[Top], DEFAULT_MEMBER_NAME = "All Etabs"
CATEGORY MEMBER [Stats].[Top].[All Etabs].[Top 5 Etablissements] as
[Top 5],ADD_CHILDREN=true
CATEGORY MEMBER [Stats].[Top].[All Etabs].[Autres Etablissements (>5)] as
SubCubeComplement([Top 5]),ADD_CHILDREN=false
SELECT
{[Measures].[NbSejours]} on 0,
{ [Stats].[Top].[Top 5 Etablissements],
[Stats].[Top].[Autres Etablissements (>5)] } on 1
From [Cube]
(*) For the people that are not used to icCube, Categories is a way defining a 'new' member as a set of members (they might have different dimensionalities). This ensures for complex calculations, schemas with many-to-many relations that the values are correctly calculated. Otherwise it might be a little nightmare to ensure calculation correctness.