Power Query - Controlling M Query functions with user-controlled parameters (2023)

Ever had a user run a query on one of your largest spreadsheets, only to have Excel immediately filter the results to show data for the last few years? All this data is transmitted over your network and then immediately filtered out. Or maybe Excel just can't handle the amount of unfiltered data they're trying to return.

In this post, I want to show you a way to solve this problem using Power Query in a solution that can dynamically filter your returned data based on user-controlled parameters.

In the scenario I'm going to demonstrate, instead of returning a full result of all employees in the company, I just want to return a list showing employees with a hire date within a specific date range that I or a user will specify. With Power Query's ability to build queries in functions, I give my users the ability to provide the scope of their choice

Connect to the data

  • In my example, I'm using a SQL Server table as the data source, but it could be any type of table. How to connect to a SQL Server table frompower querySelect tab in ExcelFrom Database > From SQL Server Database. For my example I use theAdventureWorksDWpattern database.

Power Query - Controlling M Query functions with user-controlled parameters (1)

  • Next you will be asked to provide your server and database name where the table resides. Enter this and then clickOK.
  • Once you have provided the server and database name, you will also be prompted for the credentials you will use to access the data, then clickConnect.
  • The navigation pane appears, showing all available tables. If you follow my example using the AdventureWorksDW sample database, select DimEmployee and then clickTo edit. This returns the table to the Power Query Editor. **Note** This can be done for any table from any database.

Make a function out of the query

  • I want my users to return this list of employees, but only if the HireDate column falls within a range of values ​​that they provide. To do this, we first apply a hard-coded filtering value to the HireDate column. Locate the HireDate column and apply a filter by clicking the down arrow next to the column and thenDate Filter > Between

Power Query - Controlling M Query functions with user-controlled parameters (2)

(Video) Populate a Power BI parameter list using a query

  • The range of values ​​you filter by depends on your table, but for the DimEmployee table in AdventureWorksDW I used the following filter and then clickedOK.

Power Query - Controlling M Query functions with user-controlled parameters (3)

  • This simply puts a filter on the query. If we want to make the filter dynamic, we need to change the M query which is behind the UI. On the Query Editor ribbon, go to the View tab and selectAdvanced editor. This will open the query window where you can make changes

Power Query - Controlling M Query functions with user-controlled parameters (4)

  • Next, modify this query to add a start date and end date parameter with the code highlighted in red below.

( start date , end date ) =>
Leave
Quelle = Sql.Database("localhost", "AdventureWorksDW2012"),
dbo_DimEmployee = Quelle{[Schema="dbo",Item="DimEmployee"]}[Daten],
#"Filtered Rows" = Table.SelectRows(dbo_DimEmployee, jeweils [HireDate] >= #date(2000, 1, 1) und [HireDate] <= #date(2002, 1, 1))
in
#"Filtered rows"

  • After the parameters are created, you can reference them in the query to replace the hard-coded value in the filter with a dynamic value from the parameters. Modify the query using the red code below, then clickCompleted.

( start date , end date ) =>
Leave
Quelle = Sql.Database("localhost", "AdventureWorksDW2012"),
dbo_DimEmployee = Quelle{[Schema="dbo",Item="DimEmployee"]}[Daten],
#"Filtered rows" = Table.SelectRows(dbo_DimEmployee, each [HireDate] >= #date(Date.Year(StartDate),Date.Month(StartDate),Date.Tag(Start date)) and [hired date] <= #date(date .year ( date),date.month (end date),Date .Day ( Date)))
in
#"Filtered rows"

  • This turns the query into a function. You can test this function by clickingcalland then you will be prompted for date values ​​to filter by.

Power Query - Controlling M Query functions with user-controlled parameters (5)

Power Query - Controlling M Query functions with user-controlled parameters (6)

(Video) Create A Parameter Table For Your Power Queries

  • Once you've invoked the function, make sure you remove the Invoke step before proceeding. You can do this by clicking the delete icon in theApplied StepsFeld.

Power Query - Controlling M Query functions with user-controlled parameters (7)

  • This should return the query to a function ready to be called. Now go to the Home tab in the Query Editor ribbon and selectClose & Load.
  • This saves the M-Query function in the workbook, but no results are returned yet. Just the way we want it! Our next step is to pass the desired values ​​to the function.

Power Query - Controlling M Query functions with user-controlled parameters (8)

Make the query user-interactive

  • Go to an empty spreadsheet and create a simple Excel spreadsheet that has a StartDate and Endate column with a range of values ​​like this:

Power Query - Controlling M Query functions with user-controlled parameters (9)

  • In order for our users to enter a value into this Excel spreadsheet and pass it to our function, we need to bring this little spreadsheet into Power Query. Select one of the cells in the table and select on the Power Query tabFrom table. This will bring the contents of that table into the Power Query Editor.

Power Query - Controlling M Query functions with user-controlled parameters (10)

  • To pass these two values ​​to our function, go toAdd Columntap and selectAdd custom column.
  • To connect the previously created DimEmployee function to the data we have now defined in the Excel spreadsheet, write the following formula, and then clickOK:

DimEmployees([Start Date],[End Date])

Power Query - Controlling M Query functions with user-controlled parameters (11)

(Video) Power Query Solution: Dynamic Column for Lowest Price - Record’s Functions & User Interface (PQC06)

  • If all of your default settings are enabled in Power Query, you'll likely see a privacy warning. This is because you are working with two different data sources (1st SQL Server spreadsheet, 2nd Excel spreadsheet) and there are potential privacy concerns. In our scenario, there are no legitimate privacy concerns, so I go for itContinue. I also set the data sources toOrganizationalbecause the data sources should be included in my company. Read more about Power Query's privacy settingshere.

Power Query - Controlling M Query functions with user-controlled parameters (12)

  • Once the privacy settings are configured, Power Query will add a new column called Custom (we could have renamed it earlier). press theExpandDisable the button next to the Custom columnUse the original column name as a prefixand then clickOK. This will show all rows of settings dates between our date range.

Power Query - Controlling M Query functions with user-controlled parameters (13)

  • Go ahead and now remove the StartDate and EndDate columns from the query but select them multiple times and then right click and select themremove columns.
  • Next, in the Query Settings pane, rename the query to Employee Data, and then clickClose & Loadon the Home tab.
  • You should now have two spreadsheets (it would be a good idea to rename them, of course) in your workbook.
  1. Sheet1 containing the Excel table with the date range values
  2. Sheet2 with the results of the Power Query query. This data could have optionally been sent to the Power Pivot data model
  • Now go back to Sheet1 and change the date range values ​​in the StartDate and/or EndDate columns. After you make this change, the next time the Power Query query is refreshed, the results will be retrieved from the table to filter whether your users are prevented from querying a really large table when they only need a subset of the data.
  • Final touches for Power Query

    If my users are not very familiar with Power Query and don't know how to update their queries, we can create a quick little macro to provide a button that does this for them. Using a technique I learned from Ken Puls (the blog|chirp) in a few steps our macro is ready. In Ken's post entitledUpdate Power Query with VBAHe shows how to update all Power Query queries in a workbook with a small VBA script. I'll adopt the same principles he shows, but just update the query I care about.

    • Press in your workbookAlt + F11
    • Right click on VBAProject(Book1), this may have a different name if you saved with a new name, in the project explorer and select itInsert > Module.

    Power Query - Controlling M Query functions with user-controlled parameters (14)

    • Use the following VBA script to update the workbook connection for our previously created Employee query (If you named your query differently, you may need to customize the section highlighted in red):

    Public Sub UpdateEmployeeQuery()
    ' Macro to update my Power Query scripts

    Dim cn As WorkbookConnection

    (Video) Modifying BI data loading functions/queries and controlling them via parameters in PowerBI

    For each cn in ThisWorkbook.Connections
    if cn = "Power Query - Employees" Then cn.Update
    next cn
    End Sub

    • In the VBA window, click Close.
    • To manually try the new code hitAlt + F8and you will be prompted to run the script. Select the macro you just created and click on itTo run.

    Power Query - Controlling M Query functions with user-controlled parameters (15)

    • You should note that this will trigger our staff query to update.

    Power Query - Controlling M Query functions with user-controlled parameters (16)

    Power Query - Controlling M Query functions with user-controlled parameters (17)

    • Click anywhere in the worksheet where you want the button, and then select the macro we created to assign to the button. clickOK.

    Power Query - Controlling M Query functions with user-controlled parameters (18)

    • Click the button to rename it and you're done!
    • Now all you have to do is change the values ​​in the table and click the button to update the query results. This works when the results are rendered in an Excel spreadsheet or a Power Pivot data model.

    Power Query - Controlling M Query functions with user-controlled parameters (19)

    (Video) Power Query Keyboard Shortcuts Uncover HIDDEN Features!

    As long as the data source and transformation types support it, query folding will continue to be used with this method. If you're curious about what query folding is, read more about it in this Matt MassonPost.

    I've provided this example if you'd like to download it:User Driven Parameter Example.xlsx

    Power Query - Controlling M Query functions with user-controlled parameters (20)

    Videos

    1. Custom Functions Overview - Saturday With Power Query
    (Alejandra Horvath)
    2. Working with Multiple Users in Power Query
    (Macrordinary)
    3. Passing Input Parameters into Power Query
    (Doug H)
    4. Power BI Dev Camp Session 22 - Custom Functions and Documentation in Power Query with Alex Powers
    (Microsoft Power BI)
    5. Excel Magic Trick 1349: Power Query with Input Variables from Excel Sheet to Extract Records
    (ExcelIsFun)
    6. How to use Microsoft Power Query
    (Kevin Stratvert)
    Top Articles
    Latest Posts
    Article information

    Author: Nathanael Baumbach

    Last Updated: 06/02/2023

    Views: 6626

    Rating: 4.4 / 5 (75 voted)

    Reviews: 82% of readers found this page helpful

    Author information

    Name: Nathanael Baumbach

    Birthday: 1998-12-02

    Address: Apt. 829 751 Glover View, West Orlando, IN 22436

    Phone: +901025288581

    Job: Internal IT Coordinator

    Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

    Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.