Forum Discussion

KeyserSoze33's avatar
KeyserSoze33
New Member
3 years ago
Solved

Passing multiple values in excel table as parameters into query

Hello,   I am building a workbook for our accounting team that queries the database and returns a table from which several analytics are performed. I have previously made it so that a user can inpu...
  • rubayatyasmin's avatar
    3 years ago

    Hi, KeyserSoze33 

     

    From what you've described, you've set up a parameter table and a function to return single parameter values from that table, which is then used in a SQL query. To modify this to allow for multiple asset IDs to be input and queried, you'll need to change a few things.

     

     

    First, your parameter function currently is set to return a single value for a given parameter name. You'll need to modify this function to return a list of values if there are multiple rows in the parameter table with the same name.

    Here's an example of how you might do this:

     

     
    (ParameterName as text) =>
    let
    ParamSource = Excel.CurrentWorkbook(){[Name="Parameters"]}[Content],
    ParamRows = Table.SelectRows(ParamSource, each ([Parameter] = ParameterName)),
    Values=
    if Table.IsEmpty(ParamRows)=true
    then null
    else Table.Column(ParamRows,"Value")
    in
    Values
     

    This will return a list of values for a given parameter name, or null if no values are found.

    Next, you'll need to modify your SQL query to handle multiple asset IDs. Assuming your database is using SQL Server, you can use the IN keyword to specify multiple values for a condition.

     

    Here's an example of how you might modify your query:

     

     

    let
    Year = fnGetParameter("Year"),
    Assets = fnGetParameter("Asset"),

    dbQuery = Text.Format("

    DECLARE @PeriodYear nvarchar(4)
    SET @PeriodYear = '"&Year&"'
    DECLARE @Assets TABLE (ID nvarchar(5))
    INSERT INTO @Assets (ID) VALUES ('"&Text.Combine(Assets,"'), ('")&"')
    ...

    ",{})

    Source = Sql.Database("...", "...",[Query=dbQuery])
    in
    Source

     

    This creates a table variable @Assets and inserts the asset IDs into it. You can then join or filter based on this table variable in your SQL query.

    Please be aware that this assumes your asset IDs are text (nvarchar) and you might need to modify this to suit your database schema.