Search code examples
mdxpowerbim

Parse a List to an MDX Query (List in MDX M Power BI)


I'm looking for a way to integrate a previously retrieved list as an input for a FILTER in a MDX query. I currently have the following code:

segment_var = Segment,
mdxQ = "SELECT 
            NON EMPTY ( { [Measures].[AMT] * [Forecast Type].[Forecast Type].[Forecast Level 2] } ) DIMENSION PROPERTIES MEMBER_NAME ON COLUMNS, 
            NON EMPTY ( {
                [Period].[Year Month Name].[Name] *
                [PL Spec].[PL Spec].[Level 6 Code].allmembers
            } ) ON ROWS
        FROM (
            SELECT ( { [Forecast Type].[Current Indicator].&[Y] } ) ON COLUMNS 
            FROM ( 
                SELECT ( { [PL Spec].[PL Spec].[Level 1 Code].&[101] } ) ON COLUMNS
                FROM [AIR]))
        WHERE (
            FILTER([Management Structure - Segment].[Management Structure - Segment].[MS Level 5].ALLMEMBERS , [Management Structure - Segment].[Management Structure - Segment].currentmember.name=""" & segment_var & """))",

Segment on row 1 used to be a single value parameter but I want to use a list of segments (listSegment) as a reference instead so the result filters multiple segments. I've tried WHERE IN solutions but I can't seem to get it to work. Any help would be appreciated.

Error:

Expression.Error: We cannot apply operator & to types Text and List.

Solution

  • Managed to solve the issue in the following way:

    1. Make the list of values to be filtered in the query
    2. Transpose and combine the list using ", "
    3. Convert to single variable

    Code:

        Convert = 
        Table.ToList(
            Table.Transpose(
                Table.FromList(#"Distinct")), 
            Combiner.CombineTextByDelimiter(", ")),
        Var = List.First(Convert)