How to eliminate Column wise null values in MDX?
WITH MEMBER [ReportName] AS [DimReport].[Report Key].CurrentMember.Member_Caption
MEMBER [ReportKey] AS [DimReport].[Report Key].CurrentMember.UniqueName
MEMBER [ReportWiseLatestDate] AS TAIL(NonEmpty([DimAnchorDate].[Date Key].[Date Key],[Measures].[DrPatientKeyCnt]),1).Item(0).MemberValue
SELECT
{[ReportName],[ReportKey],[ReportWiseLatestDate]} ON COLUMNS,
{[DimReport].[Report Key].[Report Key]} ON ROWS
FROM
[PopulationReportCube]
The NonEmpty() function does the trick:
WITH MEMBER [ReportName] AS [DimReport].[Report Key].CurrentMember.Member_Caption
MEMBER [ReportKey] AS [DimReport].[Report Key].CurrentMember.UniqueName
MEMBER [ReportWiseLatestDate] AS TAIL(NonEmpty([DimAnchorDate].[Date Key].[Date Key],[Measures].[DrPatientKeyCnt]),1).Item(0).MemberValue
SELECT
{[ReportName],[ReportKey],[ReportWiseLatestDate]} ON COLUMNS,
NonEmpty({[DimReport].[Report Key].[Report Key]}, [ReportWiseLatestDate]) ON ROWS
FROM
[PopulationReportCube]
Related
I am trying to write a simple MDX query to get employee counts that are more than one. I am able to use filter in the rows but how can I get the query to only return values whose count is more than one?
select NON EMPTY {[Measures].[Employee Count]} ON COLUMNS,
[Employee].[Employee ID].[Employee ID] ON ROWS
from [Human Capital]
Try this:
WITH MEMBER [Measures].[Employee Count 2+] as
IIf(
[Measures].[Employee Count]>1,
[Measures].[Employee Count],
Null
)
select {[Measures].[Employee Count 2+]} ON COLUMNS,
NON EMPTY [Employee].[Employee ID].[Employee ID].Members ON ROWS
from [Human Capital]
I need only those highlighted records.
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY([DimAssessment].[Assessment Text].&[Employee Wellness HRA],
[DimAssessment].[Question Text].&[Do you use tobacco products?],
[DimPatient].[Patient Key].[Patient Key],
Generate(
[DimAssessment].[Answer Text].[Answer Text].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
)
,[DimAssessment].[Question Text].&[Do you use tobacco products?]
)
)
)ON ROWS
FROM
[Care];
This probably isn't the answer as I've tried to help with this before but the following is a little mysterious:
The second argument of the function Tail is usually an integer e.g. 2 or 3 which means you would like the last 2, or last 3, members from the set specified in first arg of Tail.
I prefer to use cross-join rather than a tuple for members on rows but I don't think this will give you the rows you require:
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY
{[DimAssessment].[Assessment Text].&[Employee Wellness HRA]}
*{[DimAssessment].[Question Text].&[Do you use tobacco products?]}
*[DimPatient].[Patient Key].[Patient Key].members
*Generate(
[DimAssessment].[Answer Text].[Answer Text].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
)
,1 //[DimAssessment].[Question Text].&[Do you use tobacco products?]
)
)
ON ROWS
FROM
[Care];
We will need to move more of the logic inside the generate.
First try the following to see if it returns two correct columns?
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY
Generate(
[DimPatient].[Patient Key].[Patient Key].MEMBERS
,[DimPatient].[Patient Key].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimPatient].[Patient Key].CURRENTMEMBER
)
,1
)
ON ROWS
FROM
[Care];
I have just started to work with OLAP Cubes. I have some questions about MDX queries. I have a query like:
WITH
MEMBER [Balance].[NegEXPENSE] AS '-[Balance].[Type].[EXPENSE]'
SET BalanceTypeSet AS {[Balance].[Type].[INCOME], [Balance].[NegEXPENSE]}
MEMBER [Balance].[TypeSum] AS AGGREGATE(BalanceTypeSet)
SELECT {Measures.[Sum]} ON COLUMNS,
{[Balance].[Type].[INCOME], [Balance].[Type].[EXPENSE], [Balance].[TypeSum]} ON ROWS
FROM [Balance Cube]
The result of this query like:
RESULT
This result doesn't have a name of the last row(TypeSum). How can I add a name for TypeSum?
Thanks.
You need to add calculated dimension member, try this:
WITH
MEMBER [Balance].[Type].[TypeSum] AS AGGREGATE(BalanceTypeSet)
SELECT {[Measures].[Sum]} ON COLUMNS,
{[Balance].[Type]} ON ROWS
FROM [Balance Cube]
Can any one tell me how to get the record count that is a result of a MDX query?I have tried various methods and I haven't really got a solution for that.I am a beginner in MDX queries.
WITH
MEMBER [Measures].[amount] as
COUNT(
[your_dimension].[your_dimension_property].Members
)
SELECT {[Measures].[amount]} ON COLUMNS
FROM [your_awesome_cube]
A code like this will return to you the amount of members in your dimension, the COUNT method have this syntax
Count(Set_Expression [ , ( EXCLUDEEMPTY | INCLUDEEMPTY ) ] )
so you can do a lot of things, like filter your search
Create a measure in the cube that is a count or distinct count.
1) open the cube
2) Right click on the fact table on which that measure sits
3) Select New Measure...
4) Dropdown the list and select the aggregation
5) In the source column section, select the column you want the aggregation on (if u cant find it, click on show all columns on the bottom- this depends on what you are aggregating)
with member
Measures.Counts as
[your_dimension].[your_dimension_property].Children.COUNT
select Measures.Counts on 0
FROM [your_awesome_cube]
When you say record count, you basicly are saying the valid combinations of your row axis.
Lets take a basic example, the query returns 3637 rows, of which 1 row is parctically the column name row.
select [Measures].[Sales Amount] on columns,
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
Now to get the row count without running the query, lets put the combinations in count function and put the count function in a runtime measure
This Returns 3636 row.
with member [Measures].[rowCount]
as
count(([Customer].[Country].[Country],[Product].[Product].[Product]))
select [Measures].[rowCount] on columns from [Adventure Works]
Notice I have not eleminated the null combinations on rows. Lets do that next
The query returns 2101 rows , again one row from column headers.
select [Measures].[Sales Amount] on columns,
non empty
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
Now lets count the rows
This returns 2100 rows.
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
Till now we had measure from just one measure group, now lets try with multiple measure groups.
select {[Measures].[Sales Amount],[Measures].[Internet Sales Amount]} on columns,
non empty
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
//Cell set consists of 2101 rows and 3 columns.
//Wrong way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount] on columns from [Adventure Works]
//935
//Right way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
///2100
Notice when we use just a single measure the result may not be correct . If the measure we use has a null value then the combination would be removed. Where as in our rows the other measure will ensure that the combination appears.
Now Lets add a filter to the picture.
select {[Measures].[Sales Amount],[Measures].[Internet Sales Amount]} on columns,
non empty
filter(
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
)
,[Measures].[Internet Sales Amount]>5000) on rows
from [Adventure Works]
//Cell set consists of 586 rows and 3 columns.
//Wrong way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
//2100
//Right way
with member [Measures].[rowCount]
as
count(nonempty(
filter(([Customer].[Country].[Country],[Product].[Product].[Product]),[Measures].[Internet Sales Amount]>5000)
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
///585
Again till i gave the RowCount measure the exact senario that I have on my row axis it fails.
I'm trying to write a query to give me the total number of users for each customer per day.
Here is what I have so far, which for each customer/day combination is giving the total number of user dimension entries without splitting them up by customer/day.
WITH MEMBER [Measures].[MyUserCount]
AS COUNT(Descendants([User].CurrentMember, [User].[User Name]), INCLUDEEMPTY)
SELECT
NON EMPTY CrossJoin([Date].[Date].Members, [Customer].[Customer Name].Members) ON ROWS,
{[Measures].[MyUserCount]} on COLUMNS
FROM
[Users]
The problem with your calculated member is that [User].CurrentMember is set to the All member for every row tuple, and thus the count is the total. What you need is a way for the [Customer].CurrentMember and [Date].CurrentMember to effectively filter the [User] dimension.
You need to use a measure that makes sense, i.e. that will have a non-empty value for meaningful joins of the dimension members that you're interested in.
To find this out, you could start by running a query like this:
SELECT
NON EMPTY CrossJoin(
[User].[User Name].Members,
[Measures].[Some measuse]
) ON COLUMNS,
NON EMPTY CrossJoin(
[Date].[Date].Members,
[Customer].[Customer Name].Members
) ON ROWS
FROM [Project]
You would have selected Some measure adequately. The results of that query will be a lot of empty cells, but in a given row, the columns that do have a value correspond to the Users that are related to a given Customer x Date tuple (on the row). You want to count those columns for every row. COUNT and FILTER are what you need, then the query with the calculated member will be
WITH MEMBER [Measures].[User count] AS
COUNT(
FILTER(
[User].[User Name].Members,
NOT ISEMPTY([Measures].[Some measure])
)
)
SELECT
NON EMPTY {[Measures].[User count]} ON COLUMNS,
NON EMPTY CrossJoin(
[Date].[Date].Members,
[Customer].[Customer Name].Members
) ON ROWS
FROM [Users]
I am assuming a fair bit here, but with some experimentation you should be able to work it out.