To insert a query in Excel, go to the Data tab, select Get Data, choose your source, and click Load or Transform Data. This process uses Power Query to connect your spreadsheet to an external data source—such as a SQL database, a website, or another Excel file—allowing you to import and clean data that updates automatically whenever the source changes.

What You'll Need

  • A version of Excel that supports Power Query (included by default in Microsoft 365 and Excel 2016 or later).
  • Access to the data source you wish to import (e.g., a CSV file, a database login, or a URL).
  • Necessary permissions to read the data from the source system.

Step-by-Step

Note: Menu labels may differ slightly depending on your version of Excel, your operating system, or your regional settings.

  1. Open the Data Tab: Launch your Excel workbook and click on the Data tab in the top ribbon.
  2. Initiate Get Data: On the far left of the ribbon, click the Get Data button. This opens a dropdown menu with various connection options.
  3. Select Your Source: Choose the category that matches your data. Common options include:
    • From File: For CSV, XML, JSON, or other Excel workbooks.
    • From Database: For SQL Server, Access, or other database types.
    • From Other Sources: For Web pages, OData feeds, or Blank Queries.
  4. Locate the File or Server: Once you select a source (e.g., From File > From Text/CSV), a file browser or connection window will appear. Select the specific file or enter the server credentials and click Import or Connect.
  5. Preview the Data: A preview window will appear showing a sample of the data Excel has found. You now have two primary choices:
    • Load: Click this to immediately insert the data into your current worksheet as a table.
    • Transform Data: Click this to open the Power Query Editor. This is recommended if you need to remove columns, filter rows, or change data types before the data enters your sheet.
  6. Apply Transformations (Optional): If you chose Transform Data, use the Power Query Editor ribbon to clean your data. Once finished, click Close & Load in the top left corner to insert the query results into Excel.
  7. Refresh the Query: To update the data later, right-click anywhere inside the imported table and select Refresh. Excel will re-run the query and pull the latest information from the source.

Common Mistakes

  • Moving the Source File: If you import data from a local CSV or Excel file and then move that file to a different folder, the query will break because the file path has changed.
  • Changing Column Headers: If you change the name of a column in the source file, Power Query may throw an error because it is looking for the original column name to perform its steps.
  • Loading Too Much Data: Importing millions of rows from a database directly into a worksheet can cause Excel to freeze or crash. Use the "Transform Data" step to filter the data down to only what you need before loading.
  • Overlooking Data Types: Failing to set the correct data type (e.g., changing a "Text" column to "Currency") in the Power Query Editor can lead to errors when performing calculations in Excel.

Tips

  • Use Tables: When importing from another Excel file, format the source data as an official Table (Ctrl+T). This ensures that if you add new rows to the source, the query will pick them up automatically.
  • Name Your Queries: In the Queries & Connections pane, right-click your query and rename it. This makes it much easier to manage when your workbook contains multiple data imports.
  • Filter Early: Always apply filters (like date ranges or category exclusions) inside the Power Query Editor rather than on the Excel sheet. This improves performance by reducing the amount of data Excel has to process.
  • Combine Files: If you have a folder full of CSVs with the same structure, use Get Data > From File > From Folder to combine all of them into a single table with one query.

Troubleshooting

If the query does not work as expected, try these steps in order:

  1. Check the Connection: Ensure you are connected to the internet or the local network where the data source resides.
  2. Verify File Permissions: Ensure the source file is not open in another program that might be locking it, and verify that you have "Read" permissions for that folder.
  3. Update the Source Path: If you moved the file, go to Data > Get Data > Data Source Settings, select the source, and click Change Source to point to the new location.
  4. Clear Cache: If the data isn't updating, try clearing the Data Cache in the Power Query options.
  5. Check for Schema Changes: If you see an "Expression.Error: The column 'X' of the table wasn't found," go back into Transform Data and check if a column name was changed or deleted in the source file.

Frequently Asked Questions

Can I insert a query from a website?

Yes. Go to Get Data > From Other Sources > From Web and enter the URL. Excel will analyze the page and list all available tables it can extract.

Do I need to write SQL code to use queries?

No. While Power Query supports SQL for advanced users, most tasks can be completed using the visual buttons in the Power Query Editor ribbon.

How do I stop a query from loading to a sheet?

In the Queries & Connections pane, right-click the query and select Load To..., then choose Only Create Connection. This keeps the query in the background without filling up your worksheet.

Can I merge two different queries together?

Yes. In the Power Query Editor, use the Merge Queries or Append Queries buttons to join data from two different sources based on a common column.

Will the query work for other people who open the file?

It depends on the source. If the data is from a web URL or a shared network drive, others can refresh it. If the data is from a file on your personal C: drive, others will get an error until the source path is updated to a location they can access.