How can I get MIN and MAX values for dimension members in a set that uses NON EMPTY - mdx

I have a problem where I need to get the high and low values for a set from the dimension members (not the measures) for a specific intersection (one customer and time period). We need to reference these for report parameters downstream. I can only find examples for pulling the min and max measures. I need the actual dimension values.
Any ideas?

Assuming the KEY0 is a numerical value, you could do something like :
select
topCount( [Rent Range].[Rent Range Floor].members, 1, [Rent Range].[Rent Range Floor].currentMember.properties( 'KEY0', TYPED ) )
+ bottomCount( [Rent Range].[Rent Range Floor].members, 1, [Rent Range].[Rent Range Floor].currentMember.properties( 'KEY0', TYPED ) )
on 0
from [Sales]
Otherwise any other numerical property would be fine.

I had a similar requirement, see if the following helps you out.
SELECT TOPCOUNT(NONEMPTY(DESCENDANTS([Hit Time].[Date], 1, LEAVES)),1) ON 0
FROM [cube]

For the max you case use TopCount(your set, 1, your measure)
For the min you case use BottomCount(your set, 1, your measure)

Related

TopCount filtered on a date dimension value

I'm attempting to get the top 10 countries by value from any measures where the discount rate is -20, and for a specific date. I've created a simple dynamic set defined as:
CREATE DYNAMIC SET CURRENTCUBE.[Top10CountryBy20Disc]
AS
TopCount
( [Customer].[Country].members,
10, [Discount].[Discount].&[-20%])
;
This gives me the top 10 customers summed by the overall measure value, but I need to filter to a specific date. The overall top 10 won't be the same as the top 10 on a particular date, so I need to include some date context. Currently the numbers I get back for each country are correct, but it is the list of countries (the top 10 list) that is incorrect.
I use the set [Latest] listed below in a lot of other reports, and this works just fine, but am struggling to combine this when using the above set.
CREATE DYNAMIC SET CURRENTCUBE.[Latest]
AS FILTER(
[Date].[Date Key].[Date Key].members, IsDate([Date].[Date Key].currentmember.membervalue)
AND (
DateDiff("d", [Date].[Date Key].currentmember.membervalue, IIF(Weekday(Now()) = 2, 3, 1)
);
Logically I am trying to do something along the lines of the below, which is invalid syntax:
TopCount
( [Customer].[Country].members,
10, ([Discount].[Discount].&[-20%], [Latest]))
;
Would be grateful for some suggestions/direction on this.
This approach solved my issue:
GENERATE ( [Latest],
{TopCount
( descendants([Customer].[Country],,AFTER),
10,
VBA!Abs(([Discount].[Discount].&[-20%], ([Measures].[MeasureName],[Date].[Date Key].currentmember)))
)
}
);

DAX formula for - MAX of COUNT

I have the below dataset:
using the measure:
BalanceCount := COUNT(Balances[Balance])
which gives me the result:
However, I want the Grand Total to show the maximum amount of the BalanceCount, which is 2.
NewMeasure:=
MAXX(
SUMMARIZE(
FactTable
,FactTable[Account]
,FactTable[MonthEnd]
)
,[BalanceCount]
)
SUMMARIZE() groups by the columns specified, and MAXX() iterates through the table specified, returning the maximum of the expression in the second argument evaluated for each row in its input table.
Since the filter context will limit the rows of the fact table, we'll only have the appropriate subsets in each column/row grand total.
I found a solution that works for this particular case. It will not work if columns other than Account and MonthEnd are included in the filter context.
MaxBalanceCount:=
MAXX ( SUMMARIZE (
Balances,
Balances[Account],
Balances[MonthEnd]
),
CALCULATE ( COUNTROWS ( Balances ) )
)

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]

Set 0 for specific value MDX query

I've been looking around for the answer but I didn't find anything. Sorry if the answer has been given elsewhere.
Here is my problem :
I have a calculated member which is the number of items (of the current member) divided by the total number of items (sumitem).
with
member
sumitem
as
SUM ([FailureReason].[FailureReason].[All],[Measures].[Items])
member
Impact
as
[Measures].[Items]/[Measures].[SumItem]
But for a specific member of my dimension FailureReason, the result of Impact has to be 0. So I tried to add this :
member
ImpactFinal
as
iif ([FailureReason].CurrentMember = [FailureReason].[FailureReason].&[127],
0,
Impact
)
and I select my data like this :
select
{[Measures].[Items],
ImpactFinal
} on columns,
[FailureReason].members on rows
from
NoOTAR
But instead of getting 0 only for this specific member, every members of this dimension have their ImpactFinal equals to 0. What is strange is if I replace 0 by any other value, the result is good.
Just use
[FailureReason].CurrentMember IS [FailureReason].[FailureReason].&[127]
instead of
[FailureReason].CurrentMember = [FailureReason].[FailureReason].&[127]
and it will work.
Update: Several tips:
There is also not necessary to use SUM function, since you can define only tuple, this will be enough for server: ([FailureReason].[FailureReason].[All],[Measures].[Count])
It's quite reasonable to check sumitem measure for dividing by zero in ImpactFinal calculation. Because once some filters are applied, this may cause zeroing this measure and errors in reports.
If you have an opportunity not only to query, but update cube, SCOPE ([FailureReason].[FailureReason].&[127],[Measures].[Impact]) with THIS = 0 is better than additional member because of performance.
Best of luck!
UPDATE to fix totals:
If total should be w/o FailureReason 127, you can substitute your measures with:
member Impact
as
iif ([FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].&[127],
0,
[Measures].[Items]
)
member ImpactFinal
as
iif ([FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].[All]
,[Measures].[Items]-([FailureReason].[FailureReason].&[127],[Measures].[Items])
,[Measures].[Impact])/[Measures].[SumItem]
But I have another solution, which is more readable:
member v2_ImpactUncountableFailure
as
iif ([FailureReason].[FailureReason].CurrentMember.Level.Ordinal=0
or
[FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].&[127]
,([FailureReason].[FailureReason].&[127],[Measures].[Items])
,null)
member v2_ImpactFinal
as
([Measures].[Items]-[Measures].[v2_ImpactUncountableFailure])
/
([FailureReason].[FailureReason].[All],[Measures].[Items])
Use only this two measures instead of set of measures sumitem,Impact,ImpactFinal. First one will show result on failure-127 and total. Second subtracts it from clean unfiltered measure, so in the end we have clean members, zeroed failure-127 and corrected total.
Please let me know if it isn't work, I've tested on my DB and everything is OK.
A simple CASE statement would solve your problem: Try this
With
Member ImpactFinal As
CASE
WHEN [FailureReason].[FailureReason].CurrentMember IS [FailureReason].[FailureReason].&[127] THEN 0
ELSE
[Measures].[Items]
/
([FailureReason].[FailureReason].[All], [Measures].[Items])
END
SELECT
{ [Measures].[Items], ImpactFinal } On Columns
[FailureReason].[FailureReason].Members On Rows
From NoOTAR
Try
with
member sumitem
as
SUM ([FailureReason].[FailureReason].[All],[Measures].[Items])
member LeaveOut
as
[FailureReason].[FailureReason].CurrentMember.Properties("Key")
member Impact
as
IIf([Measures].[LeaveOut]= "127", 0, [Measures].[Items]/[Measures].[SumItem])

Calculating percentile values in SSAS

I am trying to calculate percentile (for example 90th percentile point of my measure) in a cube and I think I am almost there. The problem I am facing is, I am able to return the row number of the 90th percentile, but do not know how to get my measure.
With
Member [Measures].[cnt] as
Count(NonEmpty(
-- dimensions to find percentile on (the same should be repeated again
[Calendar].[Hierarchy].members *
[Region Dim].[Region].members *
[Product Dim].[Product].members
,
-- add the measure to group
[Measures].[Profit]))
-- define percentile
Member [Measures].[Percentile] as 90
Member [Measures].[PercentileInt] as Int((([Measures].[cnt]) * [Measures].[Percentile]) / 100)
**-- this part finds the tuple from the set based on the index of the percentile point and I am using the item(index) to get the necessary info from tuple and I am unable to get the measure part
Member [Measures].[PercentileLo] as
(
Order(
NonEmpty(
[Calendar].[Hierarchy].members *
[Region Dim].[Region].members *
[Product Dim].[Product].members,
[Measures].[Profit]),
[Measures].[Profit].Value, BDESC)).Item([Measures].[PercentileInt]).Item(3)**
select
{
[Measures].[cnt],
[Measures].[Percentile],[Measures].[PercentileInt],
[Measures].[PercentileLo],
[Measures].[Profit]
}
on 0
from
[TestData]
I think there must a way to get measure of a tuple found through index of a set. Please help, let me know if you need any more information. Thanks!
You should extract the tuple at position [Measures].[PercentileInt] from your set and add the measure to it to build a tuple of four elements. Then you want to return its value as the measure PercentileLo, i. e. define
Member [Measures].[PercentileLo] as
(
[Measures].[Profit],
Order(
NonEmpty(
[Calendar].[Hierarchy].members *
[Region Dim].[Region].members *
[Product Dim].[Product].members,
[Measures].[Profit]),
[Measures].[Profit], BDESC)).Item([Measures].[PercentileInt])
)
The way you implemented it, you tried to extract the fourth (as Item() starts counting from zero) item from a tuple containing only three elements. Your ordered set only has three hierarchies.
Just another unrelated remark: I think you should avoid using complete hierarchies for [Calendar].[Hierarchy].members, [Region Dim].[Region].members, and [Product Dim].[Product].members. Your code looks like you are including all levels (including the all member) in the calculation. But I do not know the structure and names of your cube, hence I may be wrong with this.
An alternate method could be to find the median of the last 20% of the records in the table. I've used this combination of functions to find the 75th percentile. By dividing the record count by 5, you can use the TopCount function to return a set of tuples that make up 20% of the whole table sorted in descending order by your target measure. The median function should then land you at the correct 90th percentile value without having to find the record's coordinates. In my own use, I use the same measure for the last parameter in both the Median and TopCount functions.
Here's my code:
WITH MEMBER Measures.[90th Percentile] AS MEDIAN(
TOPCOUNT(
[set definition]
,Measures.[Fact Table Record Count] / 5
,Measures.[Value by which to sort the set so the first 20% of records are chosen]
)
,Measures.[Value from which the median should be determined]
)
Based on what you've supplied in your problem definition, I would expect your code to look something like this:
WITH MEMBER Measures.[90th Percentile] AS MEDIAN(
TOPCOUNT(
{
[Calendar].[Hierarchy].members *
[Region Dim].[Region].members *
[Product Dim].[Product].members
}
,Measures.[Fact Table Record Count] / 5
,[Measures].[Profit]
)
,[Measures].[Profit]
)