Blog Post

Power BI Community Blog
2 MIN READ

Use Column as Parameter for Custom Function in Power Query

V-lianl-msft's avatar
V-lianl-msft
Icon for Community Support rankCommunity Support
5 years ago

Scenario:   

In query editor, you can use parameter as a filter option. 

You can use parameters when defining connection properties. For example, if you retrieve data from SQL Server, you can use one parameter for the SQL Server instance and one parameter for the target database. And you can also use parameter to SQL queries by  Value.NativeQuery(). 

You can create a new function from a query and reference parameters as the arguments of your custom function. 

 

In this article, I will show you how to use column (the column can be obtained from other queries) as parameter to get data from API. 

 

API Used:  

 

https://holidayapi.com/countries/cn/2020 

 

Detailed steps: 

 

Create a custom function (In general, we need to covert query to function in power query without using M script: 

 

  • Convert query to function 

Select New Parameter from New Parameters menu in Query Editor:  

 

Create a parameter with all default selections, set the Current Value to  2020 and name it as YEAR: 

 

 

Create a query in Power BI Desktop by ‘Get Data’ 

 

Type the API in the url: 

 

Select Table0 with holiday information and click Transform Data: 

 

Change the original query to use the parameter: 

 

 

Replace 2020 with parameters by adding new part and selecting the parameter:  

 

Convert query to function by right-clicking Query and selecting Create Function: 

 

Enter a name for new created function: 

 

 

This completes the creation of our function without using M script: 

 

  • Use the column as parameter for the custom function to combine the query with the same data structure: 

 

Create a new table with Year column: 

 

 

Click ‘Invoke Custom Function’ in ‘Add Column’ menu to use the custom function: 

 

 

Select your custom function and select Column Name in the parameter:

 

 

Select the Year column in the new table : 

 

Expand all the columns in the Holiday Function column and you will get all the holidays in 2018, 2019, and 2020: 

 

Summary 

 

This article describes how to create custom functions using the GUI. And use column as the parameter of the function to combine queries with similar data structures 

Please refer to the attachment for details. 

 

Author: Dedmon Dai

Reviewer:  Kerry and Ula

 

Published 5 years ago
Version 1.0

1 Comment

  • menglaro78's avatar
    menglaro78
    Icon for Microsoft Employee rankMicrosoft Employee

    Hi,

    I tried the same technique having "Sharepoint Online List" as source.

    In this case the whole URL must become a parameter.

     

    When I invoke the function all it's ok, after when i try to expande a table, it returns this error: 

     

    I tried to clear the permission, but after having entered the credentials again I still get the error.

    Obviously if I don't use the function, I log in without problems.

     

    Any Idea?

    Thanks