This post may contain affiliate links. Please read my disclosure for more info.

Auto refresh Power Query on parameter change

Auto-refresh Power Query Featured Image

A few days ago, a reader asked a question about using parameters in Power Query; they wanted to auto-refresh Power Query whenever a parameter changes.

“One question, how do you set the query to refresh automatically when a value changes.”

I’ve not tried this before, but I can understand the benefits.  It reduces the need to click Refresh and creates behavior that is closer in nature to standard Excel formulas.  So, it sounded like a question that deserved its own post.

This is the result we are trying to achieve:

Auto refresh Power Query

Download the example file

I recommend you download the files which support this post, as you’ll be able to work along with examples.  This is the best way to learn.  You’ll be able to see the solutions in action, plus the file will be useful for future reference.  The support files are available for FREE to newsletter subscribers.

Click below to subscribe and gain access to the subscriber area.  You will also receive:

  • My favorite tips and tricks direct to your inbox
  • Exclusive content (which is only available to subscribers)
  • FREE tools and downloads

Download Icon

If you’re already a subscriber, click here to log-in to the subscriber downloads area.

The filename for this post is 0013 Auto refresh Power Query.zip

Auto refresh power query with a macro

There are two ways for Excel to trigger events when cell values change:

  1. User Defined Function (UDF)
  2. Worksheet change event

I love using UDFs to control objects in Excel, such as changing tab colors, or setting the min and max of a chart axis.  However, I tried a UDF for this auto-refresh scenario, but it just wasn’t stable and caused Excel to crash.  If you manage to achieve this, please let me know in the comments so we can all learn from your VBA mastery 🙂

That leaves option 2, the worksheet change event.

  1. Go into the Visual Basic Editor by clicking Developer -> Visual Basic (if you’ve not got the Developer ribbon visible, you can use the keyboard short cut Alt + F11).
    Developer - Visual Basic
  2. Open the code module for the worksheet that contains the parameter.   In the example file, the Query Output tab contains the cell with the parameter.
    Visual Basic Editor
  3. Copy the following code into the sheet module.
    Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = Me.Range("G6").Address _
        Or Target.Address = Me.Range("G3").Address _
        Then
    
        ActiveWorkbook.Connections("Query - SalesData").Refresh
    
    End If
    
    End Sub
  4. Next, adapt the code to your scenario by changing the following:
    The cell references (highlighted purple in the code above) are the cells containing your parameters.  In the example file, there are two parameters in cells G6 and G3.
    The name of the query (highlighted green in the code above) will be the string “Query – “ + name of the query to refresh.  In the example file, the query is called SalesData.  Therefore the full string is Query – SalesData
    If you need more or less parameters, then add or remove instances of the following line of code:

    Or Target.Address = Me.Range("G3").Address _
  5. Close the Visual Basic Editor

That’s it; now the macro is ready for use.  Change the parameter and watch the macro automatically update the query.

Notes:

Workbooks that contain macros cannot be saved as .xlsx files. Instead, we need to use the .xlsm format.

As standard Excel behavior, when a macro executes, the Undo stack is cleared.  Therefore you will not be able to use the Undo feature after the macro runs.

Another option – refresh every n minutes

Another option, which doesn’t quite meet the reader’s requirements, but which might meet yours, is to automatically refresh the query at set intervals.  Doing this:

    • Avoids macros and therefore we can use the standard .xlsx file type
    • Still avoids the need to click refresh
    • Won’t provide an instant update, so does not emulate formula behavior of automatically recalculating

To implement this:

  1. From the ribbon select Data -> Queries & Connections
    Data - Queries and Connections
  2. Right-click on the query and select Properties… from the menu.
    Query Properties
  3. Tick the Refresh Every n Minutes option and enter a time interval.
    Refresh every x minutes
  4. Click OK to close the Query Properties dialog box.

The query will now automatically refresh at the set interval.  It if meets your requirements, it is a better option than a macro.

What next?

If your query is loaded directly into a Pivot Table, then this post by Jon Acampora will be a good next step.

Now that you’ve learned how to auto-refresh Power Query when a parameter changes, you should check out the following posts, which will help you along your Excel journey.


Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet your situation.  We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs.  By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community.  Remember, the people on these forums are generally giving their time for free.  So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

Leave a Reply

Your email address will not be published. Required fields are marked *