Forum Discussion

keobrie's avatar
keobrie
Helper I
7 years ago
Solved

Passing parameters to MDX source queries

I have read in several places that Power BI Parameters can be passed to an MDX query script but I cannot get it to work.   My script works well with SQL Server Reporting Services parameters.  The M...
  • keobrie's avatar
    keobrie
    7 years ago

    I was able to resolve this issue successfully.  When implemented in Power BI, the MDX query is stored as a string.  The trick turned out to be that the traditional SSRS/MDX parameter syntax using the @ sign in front of the parameter is not necessary and does not work.  In addition, the STRTOMEMBER function is not necessary.

     

    Instead, the insertion of a parameter is merely a string concatenation.  Thus at the point where the parameter needs insertion, close the string with a quote ("), use the traditional string concatenation function (&) before and after the raw parameter name (in my case varEndPeriod not @varEndPeriod) and then insert another quote (") to begin the next section of the string.

     

    I'm not sure if there is or will be a more advanced approach to parameter driven MDX queries in the future but for now this works.

     

    In the meantime, the references in the Blogs to dynamic query creation with variables for servers, databases, and record sources now make perfect sense.

     

    The key for porting parameter driven MDX queries to Power BI at this point in time is to understand that the query is nothing more than a string and the parameter is nothing more than a variable that can be concatenated into the string using the & for string concatenation.

     

    So the correct syntax for the snippet I posted was:

     

    FROM (SELECT ({"&varEndPeriod&".lag(11) : "&varEndPeriod&"}) ON COLUMNS

     

    Note that this FROM statement is embedded in a more complex statement and the terminal ) is several lines below.