Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Include Parameters within Power BI to Query a SQL Data Source

Hi,   I have a SQL database I'm using as a datasource.  The database is large and I would like to include two separate parameters within Power BI in an effort to query the database down to size.  I...
  • smpa01's avatar
    6 years ago

    Anonymous 

     

    let's suppose your

    server = x

    database = y

    table name = z

    sample sql query is (as you would put it on SSMS)

     

    select *
    from z
    WHERE [period_number]=2
    AND [fiscal_year]=2020

     

    when you put this on a M

     

    it becomes this

     

     

    let
        Source = Sql.Database("x", "y", [Query="select *#(lf)from zs#(lf)WHERE [period_number]=2#(lf)AND [fiscal_year]=2020"])
    in
        Source

     

     

     

    Now if you want to pass two parameters to the where clauses as P1 and P2, (P1=2020, P2=2)make sure they are text first

    you can create a custom query as below

     

     

    (x as text, y as text)=>let
        Source = Sql.Database("x", "y", [Query="select *#(lf)from z#(lf)WHERE [period_number]="&P2&"#(lf)AND [fiscal_year]="&P1&""])
    in
        Source