Search code examples
ssasmdx

Search AllMembers of User Hierarchy - for corresponding Set return a related members from upper and lower levels


We have a user hierarchy with 3 levels (Level1 being the upper level - Level3 the lower)

Using the FILTER function I state a string, this string could be within the member_name of any member from any (or several) of the levels. This returns a set.

I would then like all members from the hierarchy related to each item in the set; both upper and lower levels.

I've also tried (unsuccessfully) to include a column that is always Level2 in the results as a reference and also an ordering column.

WITH 
    MEMBER [Measures].[LevelName] AS
        [DimensionX].[HierX].Level.Name
    MEMBER [Measures].[LevelNumber] AS
        [DimensionX].[HierX].Level.Ordinal
    MEMBER [Measures].[MemName] AS //<<<<<not working as I want level 2 in the result set
        [DimensionX].[HierX].currentmember.membervalue
    SET [Set_Target] AS
        {
        FILTER(
            [DimensionX].[HierX].AllMembers,
                (
                InStr(
                    1, 
                    [DimensionX].[HierX].currentmember.name, 
                    "jesus") <> 0
                ) 
            )
        }
SELECT
//   ORDER(
//      Exists(
//      [AttributeHierX].members, 
//      [Set_Target]
//      ),
//    [AttributeHierX].[AttributeHierX].MEMBERVALUE
//      )
//*
    ORDER(
        DESCENDANTS(
            [Set_Target],
            [DimensionX].[HierX].[Level1],
            SELF_BEFORE_AFTER
            ),
        [Measures].[LevelNumber],
        BASC
    ) as X
    ON 1,
    {
    [Measures].[MemName], 
    [Measures].[LevelName],
    [Measures].[LevelNumber] 
    } ON 0
FROM [CubeX] 

Solution

  • I suppose a similar query on AdventureWorks delivers what you want:

    WITH 
       MEMBER [Measures].[LevelName] AS
           [Employee].[Employee Department].CurrentMember.Level.Name
       MEMBER [Measures].[LevelNumber] AS
           [Employee].[Employee Department].CurrentMember.Level.Ordinal
       MEMBER [Measures].[MemName] AS
           [Employee].[Employee Department].CurrentMember.Name
       SET [Set_TargetEmp] AS
            {
            FILTER(
                [Employee Department].AllMembers,
                    (
                    InStr(
                        1, 
                        [Employee].[Employee Department].currentmember.name, 
                        "WC4") <> 0
                    ) 
                )
            }
    SELECT
        {
        [Measures].[MemName], 
        [Measures].[LevelName],
        [Measures].[LevelNumber] 
        } ON 0,
        Hierarchize(
            Generate([Set_TargetEmp] as e,
                Ascendants(e.Current)
                +
                Descendants(e.Current, e.Current.Level, SELF_AND_AFTER)
            )
        )
        ON 1
    FROM [Adventure Works] 
    

    I used Hierarchize to sort the result by hierarchy, as that seemed the most easy way for me to check the result. You may want to change that. As - in contrast to the Descendants method - Ascendants does not allow a set as the first argument, I used Generate to iterate along the set. Its default behavior (without a third argument of All), it eliminates duplicates, which I assumed is the behavior you need.