How to filter on mdx dimension - mdx

I have a query which i want to filter it on max of one of its dimension fields like this:
SELECT
{
[Measures].[F Sra Quantity],
[Measures].[F Sra Gross],
[Measures].[F Sra Disc TOTAL]
}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME
ON COLUMNS,
NON EMPTY
CrossJoin(
{[Branch Dim].[b Name].Children},
{[Date Dim].[d Year Month].Children},
{[Date Dim].[Full Date].Children},
{[Customer Dim].[c Name].Children},
{[Customer Dim].[c Path].Children},
{[Order Specification Dim].[Os Type No].Children},
{[Sales Team Dim].[Id].Children},
{[Sales Team Dim].[St Visitor].Children}
)
ON ROWS
FROM [D Sys Warehouse]
I want to filter it on the max value of [Os Type No] which its members are changing always. would you help me please?

Not tested as I'm not near SSAS or the AdvWrks cube but something along these lines might work, assuming I have interpreted your request correctly:
WITH MEMBER [Measures].[MAX_No] AS
MAX(
[Order Specification Dim].[Os Type No].MEMBERS,
[Order Specification Dim].[Os Type No].CURRENTMEMBER.MEMBER_VALUE
)
SELECT
{
[Measures].[F Sra Quantity],
[Measures].[F Sra Gross],
[Measures].[F Sra Disc TOTAL]
}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME
ON COLUMNS,
NON EMPTY
CrossJoin(
{[Branch Dim].[b Name].Children},
{[Date Dim].[d Year Month].Children},
{[Date Dim].[Full Date].Children},
{[Customer Dim].[c Name].Children},
{[Customer Dim].[c Path].Children},
{[Order Specification Dim].[Os Type No].Children},
{[Sales Team Dim].[Id].Children},
{[Sales Team Dim].[St Visitor].Children}
)
HAVING
[Order Specification Dim].[Os Type No].CURRENTMEMBER.MEMBER_VALUE = [Measures].[MAX_No]
ON ROWS
FROM [D Sys Warehouse]

Related

MTD Function not working with SCOPE

SCOPE ([Measures].[Net IMS Volume]);
( [Time].[Time Calculations].[MTD] )
= SUM(
MTD([Time].[Time Hierarchy 1].CURRENTMEMBER)
,[Time].[Time Calculations].[Current Time]
);
END SCOPE;
I was expecting my MTD to be displayed across the dates of the month. but the result is that I just get data at the Month Level.
When I do the same thing over at SSMS w/ the ff.:
WITH MEMBER [Time].[Time Calculations].MIKE2 AS
SUM
(
MTD([Time].[Time Hierarchy 1].CURRENTMEMBER)
,[Time].[Time Calculations].[Current Time]
)
SELECT
{
([Time].[Time Calculations].[MTD],[Measures].[Net IMS Volume])
,([Time].[Time Calculations].MIKE2,[Measures].[Net IMS Volume])
} ON 0
,[Time].[Time Hierarchy 1].[Date] ON 1
FROM [My Cube];
I get data at Date Level. I'm experiencing this on YTD function also.
Your time dimensions may not be typed correctly. Specific typing is required for the YTD and MTD to function as expected.
MSDN reference to YTD function: https://msdn.microsoft.com/en-us/library/ms146039.aspx
In the "remarks" section of the definition:
If a member expression is not specified, the default is the current
member of the first hierarchy with a level of type Years in the first
dimension of type Time in the measure group.
The Ytd function is a shortcut function for the PeriodsToDate function where the Type
property of the attribute hierarchy on which the level is based is set
to Years. That is, Ytd(Member_Expression) is equivalent to
PeriodsToDate(Year_Level_Expression,Member_Expression). Note that this
function will not work when the Type property is set to FiscalYears.
You could test if this is your problem by converting to the use of PeriodsToDate:
WITH MEMBER [Time].[Time Calculations].MIKE2 AS
SUM
(
PeriodsToDate(
[Time].[Time Hierarchy 1].[Month Level]
, [Time].[Time Hierarchy 1].CURRENTMEMBER
)
,[Time].[Time Calculations].[Current Time]
)
SELECT
{
([Time].[Time Calculations].[MTD],[Measures].[Net IMS Volume])
,([Time].[Time Calculations].MIKE2,[Measures].[Net IMS Volume])
} ON 0
,[Time].[Time Hierarchy 1].[Date] ON 1
FROM [My Cube];

MDX Query to Filter by Date Dimensions

I am new to MDX queries and am trying to figure out how to filter a result set using date dimensions.
Let's take a cube structured like this (contrived example):
I would like to give the user a list of projects to select, and display the cost of all events that occurred during the selected projects (i.e. between start date and end date). However, the events are not linked to projects.
Using the query:
SELECT NON EMPTY
{
[Measures].[Cost]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
FROM [Cube]
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I can get a list of items like this:
Project Start Date End Date Date of Occurrence Cost
------------------------------------------------------------------
Project 1 01-Jan-15 31-Jan-15 27-Dec-14 750
Project 1 01-Jan-15 31-Jan-15 01-Jan-15 680
Project 1 01-Jan-15 31-Jan-15 02-Jan-15 320
Project 1 01-Jan-15 31-Jan-15 03-Jan-15 150
Project 1 01-Jan-15 31-Jan-15 01-Feb-15 700
Project 1 01-Jan-15 31-Jan-15 05-Feb-15 175
If I run the query for Project 1 only, it should exclude the first event and last 2 events.
Would the best approach be to use a WHERE or FILTER? And because these are dimensions and not measures, how would I do a comparison of WHERE [Date of Occurrence] BETWEEN [Start Date] AND [End Date]?
Any help is much appreciated.
I would try something like this:
WITH MEMBER [Measures].[Cost in period] AS
IIF(
[Event Details].[Date of Occurrence].CurrentMember.Properties('Key') >=
[Project Details].[Start Date].CurrentMember.Properties('Key') &&
[Event Details].[Date of Occurrence].CurrentMember.Properties('Key') <=
[Project Details].[End Date].CurrentMember.Properties('Key'),
[Measures].[Cost], NULL)
SELECT NON EMPTY
{
[Measures].[Cost in period]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
ON ROWS
FROM [Cube]
Basically, you create a calculated measure which is NULL when the Date of Occurrence lies outsite the Start Date - End Date interval. Thanks to NON EMPTY on the Row members, the data should be filtered out of the result.
If [Event Details] and [Project Details] are role playing dimensions, you can put the LINKMEMBER MDX function to use to help you. Also, I am assuming you would be using some sort of front end(maybeSSRS) to give the user the freedom to choose the start and end dates(as parameters or calendar control). In that case, they will enter the MDX query as strings. STRTOMEMBER function converts those string to members.
Using LINKMEMBER, once I generate a set of dates, I am using AGGREGATE function to get the aggregated value of measure for this set of dates.
with set [Start Date] as
linkmember(STRTOMEMBER('[Project Details].[Start Date].[Start Date].&[01/01/2014]'), [Event Details].[Date of Occurrence])
set [End Date] as
linkmember(STRTOMEMBER('[Project Details].[End Date].[End Date].&[01/01/2015]'), [Event Details].[Date of Occurrence])
set ListOfDate as
{[Start Date].item(0):[End Date].item(0)}
member [Measure.NetCost] as
aggregate(ListOfDates, [Measures].[Cost])
SELECT NON EMPTY
{
[Measure.NetCost]
}
ON COLUMNS,
NON EMPTY [Project Details].[Project].[Project].ALLMEMBERS
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
FROM [Cube]
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
DISCLAIMER: As stated, it would work only if the [Event Details] and [Project Details] are role playing dimensions.
I am not sure if this code would work in your scenario, but I have found this function quite handy at times. To read more on LINKMEMBER function, see here.
Not tested. I've not used && before - maybe just the keyword AND would suffice:
SELECT NON EMPTY
{
[Measures].[Cost]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
HAVING [Event Details].[Date of Occurrence].CurrentMember.MEMBERVALUE >=
[Project Details].[Start Date].CurrentMember.MEMBERVALUE
&& //<< OR IS THIS JUST "AND"?
[Event Details].[Date of Occurrence].CurrentMember.MEMBERVALUE <=
[Project Details].[End Date].CurrentMember.MEMBERVALUE
ON ROWS
FROM [Cube]

Adding further information in the context of the cell set

I'd like to be able to search the [Employee Department] hierarchy - within any of it's levels using a string: the following does this ok and finds all members related to the string "Control"
Now I have tried to add a cross join so that I can always see the department name for each of the rows and then ORDER by the department. If the commented section is uncommented I unfortunately get a full cartesian product - I only want the departments in the context of the members found by the filter - is this possible?
WITH
MEMBER [Measures].[LevelName] AS
[Employee].[Employee Department].Level.Name
MEMBER [Measures].[LevelNumber] AS
[Employee].[Employee Department].Level.Ordinal
SET [Set_TargetEmp] AS
{
FILTER(
[Employee Department].AllMembers,
(
InStr(
1,
[Employee].[Employee Department].currentmember.name,
"Control") <> 0
)
)
}
SELECT
// ORDER(
// [Department].members,
// [Department].[Department].MEMBERVALUE
// )
// *
ORDER(
DESCENDANTS(
[Set_TargetEmp],
[Employee].[Employee Department].[Department],
SELF_BEFORE_AFTER
),
[Measures].[LevelNumber],
BASC
) as X
ON 1,
{
[Measures].[LevelName],
[Measures].[LevelNumber]
} ON 0
FROM [Adventure Works]
Assuming you use Department the Department dimension, and the Employee Department is in a different dimension named Employee, you get a cross product. Analysis Services only applies "autoexists" within the same dimension. Across dimensions, you must apply this logic explicitly like this:
ORDER(
Exists([Employee].[Department Name].[Department Name].members,
[Set_TargetEmp]
),
[Department].[Department].MEMBERVALUE
)
for the commented block in your code should deliver what you want.
In case you have more than one measure group that relate to both the department and the employee dimensions, you should state the name of the measure group to use as the third argument of Exists. This is a string argument, hence this name should be included in quotes.

MDX Calculate SUm at a fixed level in hierarchy

I Have a hieracy Defined a Level1 to Level5 and beneath that an Id.
I would like to create a calculated member that always makes the sum at the Level4.
currentMember.Parent works at Level5 but not at the Id Level.
What is the better way ?
Ok, Found it, With the Ancestors you can specify at what leve you want it.
WITH MEMBER Measures.Temp AS
SUM(Ancestors([Master Product].[Product Tree].CurrentMember,[Master Product].[Product Tree].[IBS Level 4]), [Measures].[Qty Master Product])
SELECT NON EMPTY { [Measures].[Qty Master Product], [Measures].[Qty Cross Product], Measures.Temp } ON COLUMNS,
NON EMPTY { [Master Product].[Product Tree].[IBS Level 5] } ON ROWS
FROM [ITS Cross Sales]
WHERE ( [Complex].[By Country].[Complex].&[3],
[Dates].[Calender].[Date].&[2013-03-17T00:00:00] )

calculate percentage of individual categories in MDX

I want to calculate percentage of individual Categories, Here is my mdx code.
WITH
MEMBER [Measures].[Individual_Total] AS ([DIM RATING STRINGS].[LABEL]*[Measures].AGG_RATING Count])
SELECT
NONEMPTY {
[Measures].[AGG_RATING Count],[Measures].[Individual_Total]
} ONColumns,
([DIM STRUCTURE].[PARENT CODE].&[M01]&[M]:[DIM STRUCTURE].[PARENT CODE].&[M11]&[M],
[DIM TAILORING].[TAILORING_LABEL].[TAILORING_LABEL],
{[DIM RATING STRINGS].[LABEL].[LABEL],[DIM RATING STRINGS].[LABEL]}
) onrows
FROM [Cube]
Here is the output
In this output we have 4 categories like ""External Drivers ,stretegy,Business operation and governance.
I need to calculate percentage of different color with in the same category.
For example if we take "External Drivers" then Calculation should be like
amber = 15/28 * 100, green = 5/28/*100 etc because 28 is the sum of external drivers.
Please tell me how to do this thing in mdx.
thanks
Here you can compare with my solution, it will give you the percent of the parent.
with
member [Measures].[Percent of parent] as
([Measures].[Order Quantity]) /
([Product].[Product Categories].currentmember.parent,
[Measures].[Order Quantity])
,format_string = "percent"
SELECT
{([Measures].[Order Quantity]),
([Measures].[Percent of parent])} ON COLUMNS,
{[Product].[Product Categories].[Category].&[3]} *
{[Product].[Subcategory].[Subcategory].Members} *
{[Product].[Style].[Style].Members} *
{[Product].[Product].members}
ON ROWS
FROM [Cube]
I donĀ“t know if i read your dimensions correctly but maybe your member should look something like:
with member [Measures].[Percent] as
[Measures].[AGG_RATING Count] /
([DIM RATING STRINGS].[LABEL].CURRENTMEMBER.PARENT,
[Measures].[AGG_RATING Count])
, format_string = "percent"