Forum Discussion

fsim's avatar
fsim
Responsive Resident
2 years ago
Solved

connection function and error in the argument passed

hello HiveMind
In our infrastructure, I have multiple environments. For each one, I have a data server whose name changes, as well as 4 databases (which are Bronze_crm, Bronze_FO, Silver, and Gold)  whose names also change with the env.. In other words, the database Bronze_crm, for example, is named "dataverse_schprd_####" in the "prod" environment but "dataverse_####_orgad0fc35e" in the test environment.
I have compiled all the environments and database names into a table "connectors". Based on the value of the [env] column, I can retrieve the server address ([Server] field) as well as the names of the 4 DBs . The purpose of my function is to be able to use an ENVDYN parameter to choose the target environment, then to pass as an argument of my function, the database to which I want to connect and optionally, a SQL query that I would like to execute.
The code of my function is
    (DB as text, optional qury as text) =>
    let
        ParametreTrouve = Table.SelectRows(connectors, each [env] = ENVDYN),
        P_Server = if Table.RowCount(ParametreTrouve) > 0 then ParametreTrouve{0}[Server] else null,
        P_DB = if Table.RowCount(ParametreTrouve) > 0 then Record.Field(ParametreTrouve{0}, DB) else null,
        options = if Value.Is(qury) then Record.AddField([], "Query", qury) else [],
        connection = Sql.Database(P_Server, P_DB, options)
    in
    connection

Unfortunately, I keep having this error msg:
An error occurred in the ‘’ query. Expression.Error: 1 arguments were passed to a function which expects 2.
Details:
Pattern=
Arguments=[List]

The error comes from the query option part of the SQL.database() function.  but I don't see how to pass this query in the function. Any idea would be helpful.
Thanks in advance
Fred

 

  • if Value.Is(qury)  is the source of the error. (It took me 2 days to figure it out and, guess what, co-pilot didn't spot it)
    I replaced it by qury <>"" and it works. 
    enjoy the 
    (DB as text, optional qury as text) =>
    let
    ParametreTrouve = Table.SelectRows(connectors, each [env] = ENVDYN),
    P_Server = if Table.RowCount(ParametreTrouve) > 0 then ParametreTrouve{0}[Server] else null,
    P_DB = if Table.RowCount(ParametreTrouve) > 0 then Record.Field(ParametreTrouve{0}, DB) else null,
    connection =
    if qury <>"" then
    Sql.Database(P_Server, P_DB ,[Query=qury])
    else
    Sql.Database(P_Server, P_DB)
    in
    connection

3 Replies

  • fsim's avatar
    fsim
    Responsive Resident

    if Value.Is(qury)  is the source of the error. (It took me 2 days to figure it out and, guess what, co-pilot didn't spot it)
    I replaced it by qury <>"" and it works. 
    enjoy the 
    (DB as text, optional qury as text) =>
    let
    ParametreTrouve = Table.SelectRows(connectors, each [env] = ENVDYN),
    P_Server = if Table.RowCount(ParametreTrouve) > 0 then ParametreTrouve{0}[Server] else null,
    P_DB = if Table.RowCount(ParametreTrouve) > 0 then Record.Field(ParametreTrouve{0}, DB) else null,
    connection =
    if qury <>"" then
    Sql.Database(P_Server, P_DB ,[Query=qury])
    else
    Sql.Database(P_Server, P_DB)
    in
    connection

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for sharing this solution, it's very helpful!

      • fsim's avatar
        fsim
        Responsive Resident

        Anonymous 
        unfortunately the report cannot be refreshed by powerBi service anymore. 
        https://aka.ms/dynamic-data-sources
        Am I the only one that need to change the sources of the queries  when publishing in different environments ?