MDX Query - Non Empty on 1 Axis - mdx

I am trying to eliminate a null value from a result set. I am querying only first level dimension data to add to my selection box.
There is a #null value that gets returned with the query.
select {} on columns,
NON EMPTY{[Markets].[All Markets].Children}) on rows
from [SteelWheelsSales]
And the above query does not work since its not against any measure. But i want only the list Markets in the first level to be displayed in my selection box.

Rather than NON EMPTY try NonEmpty:
SELECT
{} ON 0,
NonEmpty([Markets].[All Markets].Children) ON 1
FROM [SteelWheelsSales];

Try filtering instead,
SELECT
{} ON COLUMNS,
Filter( [Markets].[All Markets].Children, not IsEmpty ( [Measures].[Sales]) ) ON ROWS
FROM [SteelWheelsSales];

Related

MDX Filter based on a Dimension field and Measure field not returning expected result

I'm using SSAS 2019 and have the following MDX query to determine data errors - i.e. in this case where a given score ([Measures].[Score]) is above what the maximum score ([PerformanceLevel].[MaxScore]) is
SELECT NON EMPTY {
[Measures].[FactTestScoreCount]
} ON COLUMNS,
NON EMPTY
{(
[TestEvent].[Key].[Key].Members
)} ON ROWS
FROM [TestScore]
WHERE
(
FILTER(
[PerformanceLevel].[MaxScore].[MaxScore].Members,
[PerformanceLevel].[MaxScore].CurrentMember.MemberValue < [Measures].[Score]
)
)
...you'll note it uses a Measure value to compare against a Dimension value in the filter
However the above query incorrectly returns all rows (it should return zero rows as we have no scores in the current database that are above the maximum score)
I have tried using a HAVING and also tried to add the filter into the FROM however I get the same result. I have also tried converting the data-type to int on both sides of the expression but to no avail. I tried temporarily changing the condition to hard-code (numeric) values and this seems to narrow down the issue as being the [Measures].[Score] in the FILTER - i.e. as far as I can see putting the Measure within the FILTER doesn't seem to be working as expected but I can't work out why
Any ideas would be much appreciated
I didn't fully understand what the first parameter of the Filter function represented - specifically the granularity, otherwise it (depending on your aggregation type will SUM the measure). Also moving the Filter up to the select allows more flexibility when joining/displaying rows - so I came up with the below which works well
SELECT NON EMPTY {
[Measures].[FactTestScoreCount]
} ON COLUMNS,
Filter(
NonEmpty(
{(
[TestEvent].[Key].[Key].members
)}
), [Measures].[Score] > [PerformanceLevel.[MaxScore].CurrentMember.MemberValue
) ON ROWS
FROM [TestScore]

MDX date range with NON EMPTY clause is not slicing the data for the range

I am working to optimize the query time. But in my cases, case 1 and case 2 are returning the same result. case 1 should return result for a particular data range i.e. [time].[2015].[5].[10] : [time].[2015].[6].[9] instead it is returning only for [time].[2015].[6].[9] as the result of case 2. case 1 returns the correct result i.e. data for the range but only if you remove the non empty clause. removing the non empty clause means it will search the entire data sets which is again taking lots of time. again case 3 returns correct result but operation is even more time then the first approach. Anybody facing such issue or can guide me to for the problem.
CASE 1
WITH
MEMBER [Measures].[abc_type] AS
[abc].CurrentMember.Properties("abc_type")
MEMBER [Measures].[abc_desc] AS
[abc].CurrentMember.Properties("abc_desc")
MEMBER [Measures].[abc_class] AS
[abc].CurrentMember.Properties("abc_class")
SELECT
NON EMPTY
{
[Measures].[abc_type]
,[Measures].[abc_desc]
,[Measures].[abc_class]
} ON COLUMNS
,NON EMPTY
Filter
(
{[abc].[abc_id].MEMBERS}
,St_contains
(
[district].[district_id].[1].Properties("the_geom")
,[abc].CurrentMember.Properties("the_geom")
)
) ON ROWS
FROM [analytics_cube]
WHERE
[time].[2015].[5].[10] : [time].[2015].[6].[9];
CASE 2
WITH
MEMBER [Measures].[abc_type] AS
[abc].CurrentMember.Properties("abc_type")
MEMBER [Measures].[abc_desc] AS
[abc].CurrentMember.Properties("abc_desc")
MEMBER [Measures].[abc_class] AS
[abc].CurrentMember.Properties("abc_class")
SELECT
NON EMPTY
{
[Measures].[abc_type]
,[Measures].[abc_desc]
,[Measures].[abc_class]
} ON COLUMNS
,NON EMPTY
Filter
(
{[abc].[abc_id].MEMBERS}
,St_contains
(
[district].[district_id].[1].Properties("the_geom")
,[abc].CurrentMember.Properties("the_geom")
)
) ON ROWS
FROM [analytics_cube]
WHERE
[time].[2015].[6].[9];
CASE 3
WITH
MEMBER [Measures].[abc_type] AS
[abc].CurrentMember.Properties("abc_type")
MEMBER [Measures].[abc_desc] AS
[abc].CurrentMember.Properties("abc_desc")
MEMBER [Measures].[abc_class] AS
[abc].CurrentMember.Properties("abc_class")
MEMBER [time].[newtime] AS
Aggregate([time].[2015].[5].[10] : [time].[2015].[6].[9])
SELECT
NON EMPTY
{
[Measures].[abc_type]
,[Measures].[abc_desc]
,[Measures].[abc_class]
} ON COLUMNS
,NON EMPTY
Filter
(
{[abc].[abc_id].MEMBERS}
,St_contains
(
[district].[district_id].[1].Properties("the_geom")
,[abc].CurrentMember.Properties("the_geom")
)
) ON ROWS
FROM [analytics_cube]
WHERE
[time].[newtime];
Ranges of time are always evaluated by Mondrian as a full level scan. It will load all of the members of the given time level and will start iterating over the members until it finds the first bound. It will then create a sublist up until the last member of the range.
There is an enhancement request filed to turn ranges into SQL predicates here.
If you get a chance to test the prototype code, let us know how it works for you

MDX - Is it possible to have two unrelated dimension members in one row?

I need to create the table of the following structure in MDX (to be used in SSRS report):
For that I have 2 dimensions and one measure:
Option dimension, with option type and option value attributes
Standard dimension, with IsStandard flag
Price measure
In first column I need to show all option type attributes,
in second all value attributes where IsStandard flag is set to [Y],
in third values chosen by user in parameters and
in fourth prices for components selected by user.
Is it possible to do the above in single MDX? Or will I be better off creating 2 distinct queries and creating 2 tables for them?
EDIT: Since my updates won't fit into the comment, I will add some thoughts here.
EXISTS function from answer below does not filter the result set, I don't get standard values but all possible values concatenated. When I issue the following code:
SELECT
[Measures].[Price] ON 0,
NON EMPTY [Option].[Option Type].children
*
[Option].[Option Value].children ON 1
FROM [Cube]
WHERE
(
[Standard].[IsStandard].&[Y],
[Configurations].[Configuration].&[conf1]
)
It returns the default values correctly, but if I use
SELECT
[Measures].[Price] ON 0,
[Option].[Option Type].children
*
EXISTS(
[Option].[Option Value].[Option Value].members
,([Standard].[IsStandard].&[Y],[Configurations].[Configuration].&[conf1])
) ON 1
FROM [Cube]
It does not filter the results.
If you can accept a slightly different order of columns, then this can be done in MDX, using a calculated measure which is actually a string (as you want to see a list of attributes values in column). This avoids having the same attribute twice in the rows:
WITH Member Measures.[Standard Value] AS
Generate(NonEmpty([Option].[Option Type].[Option Type].Members,
{([Standard].[IsStandard].&[Y],
Measure‌​s.[Price]
)}
),
[Option].[Option value].CurrentMember.Name,
", "
)
SELECT { Measures.[Standard Value], Measures.[Price] }
ON COLUMNS,
NON EMPTY
[Option].[Option Type].[Option Type].Members
*
{ #chosenValues } // the parameters value should be a comma separated list like "[Option].[Option value].[AMD], [Option].[Option value].[INTEL]"
ON ROWS
FROM [Your Cube]
WHERE [Configurations].[Configuration].&[conf1]
You can adapt the list separator (the last argument of the Generate function) to anything you like.
And in case there is more than one measure group that is related to the dimensions [Option], [Standard], and [Configurations], you should add the name of the measure group to use for determining the relationship as additional last parameter to the Exists, so that you and not the engine determines that. Just use the name of the measure group in either single or double quotes.
Yes it is, dimension will just be ignored. This is assuming you've all in the same schema / cube.
Note, depending on the OLAP Server you're using it's possible you've to change a flag that sends an error if you're using a dimensions that is not defined at Measure Group level.

How filter only the children members in MDX?

When I run this mdx query, works fine (get the children members from a hierarchy level):
select {} on columns,
[Dimension].[hierarchy].[level].children on rows
from [Cube]
But, when I add some tuple on rows, doesn't filter filter the children members (shows all the members) :S
select {} on columns,
[Dimension].[hierarchy].[level].children
* [Dimension2].[hierarchy2].[level2].allmembers on rows
from [Cube]
* is a cross join - you will get the Cartesian product of [Dimension].[hierarchy].[level].children and [Dimension2].[hierarchy2].[level2].allmembers because they are different dimensions.
If they were two hierarchies from the same dimension then auto exist behaviour would limit the results e.g. Year2014 crossed with month should just show the months in 2014.
Try using DESCENDANTS function + you might not require NULLs so try the NON EMPTY
SELECT
{} ON COLUMNS,
NON EMPTY
DESCENDANTS(
[Dimension].[hierarchy].[level].[PickAHigherUpMember],
[Dimension].[hierarchy].[PickTheLevelYouWantToDrillTo]
)
*
[Dimension2].[hierarchy2].[level2].allmembers ON ROWS
FROM [Cube]
if you look at the mdx language reference for children, you will also find another example of how to use the function with a hierarchy in stead of a member_expression.
http://msdn.microsoft.com/en-us/library/ms146018.aspx
but it won't work with a hierarchy level.
Maybe the row expression was initialy a hierarchy that you've have changed into a level expression.
in the following a similar working mdx with a hierarchy on rows:
select {} on 0,
[Product].[Model Name].children
*
[Geography].[Country].[All Geographies]
on 1
FROM [Adventure Works
Philip,
I guess you want only those rows where the children have a value on the default measure. In that case you could try the following:
select {} on columns,
Nonempty([Dimension].[hierarchy].[level].children
* [Dimension2].[hierarchy2].[level2].allmembers) on rows
from [Cube]
Now if, for the children, you'd need all the members from Dimension2 then you could try:
select {} on columns,
Nonempty([Dimension].[hierarchy].[level].children, [Dimension2].[hierarchy2].[level2].allmembers)
* [Dimension2].[hierarchy2].[level2].allmembers) on rows
from [Cube]
In the second case the Nonempty function takes a second parameter and the cross join is done with the result of the Nonempty function. For the documentation on Nonempty including the usage of the second parameter see https://learn.microsoft.com/en-us/sql/mdx/nonempty-mdx

How to get the last, non empty 10 members with MDX?

I have an MDX query:
SELECT
NON EMPTY {Hierarchize({[Measures].[Rating]})} ON COLUMNS,
NON EMPTY {Hierarchize({[Response].[Response Name].Members})} ON ROWS
FROM [Ratings]
That returns a table of Response Names to Response count. This query returns a row for ALL responses, though, and I just want the most recent 10 responses.
I tried to do this using HEAD like so:
SELECT
NON EMPTY {Hierarchize({[Measures].[Rating]})} ON COLUMNS,
HEAD(NON EMPTY {Hierarchize({[Response].[Response Name].Members})}, 10) ON ROWS
FROM [Ratings]
but it gives an error "Syntax error at line 3, column 18, token 'NON'"
If I remove the NON EMPTY, then it works as I would expect (returns only 10 members), but then it includes the empty Ratings.
How can I use NON EMPTY and HEAD together? (Or accomplish the same thing another way)
I believe it is already a bit late for the answer, but here's the solution I used (I was having the same problem):
select {[Measures].[Percentage Present]} ON COLUMNS,
Head(Filter([Student].[Student].AllMembers, not isEmpty([Measures].[Percentage Present])),10) ON ROWS
from [Attendance]
Head + Filter(, not isEmpty([measure])) did the trick!
Not sure to understand what you mean by 'last' but here is a statement using the NonEmpty function instead of the NON EMPTY keywords:
SELECT
NON EMPTY [Measures].[Rating] ON COLUMNS,
HEAD( NonEmpty(
[Response].[Response Name].Members,
[Measures].[Rating]
),
10
) ON ROWS
FROM [Ratings]