Skip to content
Excel Help
Intermediate 10 min read Updated 21/09/2026

Working with Excel Power Query M Code in an IDE

While Excel Power Query uses M language for its queries, there are several methods to export and work with Power Query code in integrated development environments (IDEs) for better version control and code management.

In this tutorial:

  • Extracting Power Query M code
  • Setting up VS Code for M language
  • Managing Power Query scripts
  • Version control integration

You'll need:

  • Excel (2016 or newer)
  • Visual Studio Code or similar IDE
  • Basic Power Query knowledge

Working with Power Query M Code in an IDE

Extracting Power Query M Code

1

Access Advanced Editor

In Excel, select the query under Data > Queries & Connections and choose Edit. In Power Query Editor, open Home > Advanced Editor. Copy the M expression for that query; saving an external file does not make Excel read or refresh it automatically.

2

Copy M Code

Select all code (Ctrl+A) and copy it to your clipboard (Ctrl+C).

Setting Up VS Code for Power Query Development

1

Install Extensions

  • Power Query SDK
  • An M syntax extension if you only need editing
2

Create M Files

Save the copied expression as an .m or .pq text file and set the editor's language mode to Power Query M. Microsoft's Power Query SDK for VS Code is primarily for developing custom connectors; a copied Excel query is not a connector project and is not automatically synchronized with its workbook.

Organizing Power Query Files

Recommended Structure:

project/
├── queries/
│   ├── common/
│   │   └── shared_functions.pq
│   ├── data_sources/
│   │   └── database_connections.pq
│   └── transformations/
│       └── main_query.pq
└── README.md

Treat this as a source archive. After editing, paste the expression back into each query's Advanced Editor in Excel and refresh a copy of the workbook to check it.

Version Control Integration

Git Setup:

  • Initialize Git repository in your project folder
  • Create .gitignore file for Excel temporary files
  • Commit Power Query files separately from Excel workbooks

Best Practices

Do's:

  • Comment your M code thoroughly
  • Use consistent naming conventions
  • Modularize common functions

Don'ts:

  • Store sensitive credentials in M files
  • Mix query logic with data source configs
  • Ignore error handling in scripts

Common Issues and Solutions

  • Syntax Highlighting Not Working:

    Ensure files have .pq extension and M Language extension is properly installed.

  • Connection String Issues:

    Use parameters in M code instead of hardcoded connection strings.