Edit the Power Query of a Power BI Project (PBIP) in VS Code: lint your M, preview the data, and write the queries back to the model safely.
M Lens works on top of the official Power Query / M Language extension, which is installed with it and provides IntelliSense, syntax highlighting and formatting.
A interface também está em português: o M Lens segue o idioma do VS Code. A documentação em português vem junto com a extensão, no arquivo README.pt-BR.md.
Getting started
- In Power BI Desktop, use File > Save as > Power BI Project (.pbip).
- Open the project folder in VS Code and select the M Lens icon in the Activity Bar.
- Select Extract queries to edit them. Every query becomes a
.pq file in mlens-queries/.
- Edit the queries. When you are done, select Changes to apply to review the diff, then Apply Queries to PBIP.
Close the project in Power BI Desktop before applying, and reopen it afterwards to see the changes.
Features
The M Lens view shows where your project stands and what to do next: queries to extract, edits waiting to be applied, possible secrets, and whether data preview is set up. It also lists every data source (grouped by connector, select one to jump to it) and which queries depend on which.
Safe write-back to PBIP
- Queries are matched by name, not by line number, so editing the
.tmdl files in between does not break anything.
- Only the M code of the queries you edited is replaced. Everything Power BI keeps around it (
lineageTag, queryGroup, annotations, line endings) stays as it was.
- Review PBIP Changes opens a diff of each
.tmdl file: current content next to the result after applying.
- Before writing, M Lens saves a copy of every changed
.tmdl file in .mlens/backups/<timestamp>/.
- Extracting again asks before it overwrites
.pq files with edits you have not applied yet.
Linter
Rules that the Power Query editor does not check:
| Rule |
Default |
What it finds |
unused-step |
warning |
A step that is declared but never used, with a quick fix to remove it |
table-buffer |
info |
Table.Buffer, List.Buffer and Binary.Buffer, which load data into memory and stop query folding |
hardcoded-path |
warning |
Local or network file paths (C:\..., \\server\...) outside parameters |
generic-step-name |
hint |
Names generated by the editor, in English or Portuguese (Custom1, Changed Type2, Tipo Alterado1), with a quick fix to rename |
sensitive-literal |
warning |
Text that looks like a secret: Password=..., api_key=..., Bearer tokens, known key formats |
localhost-source |
warning |
Sources that point to localhost or 127.0.0.1 |
web-contents-options |
info |
Web.Contents with a hardcoded query string instead of the RelativePath and Query options |
remove-columns-risk |
hint |
Table.RemoveColumns where Table.SelectColumns would be more explicit |
Change the severity of each rule with the mlens.lint.* settings (off, hint, info, warning, error). Check All Queries runs the rules on every .pq and .m file in the workspace.
Data preview (Windows)
Preview Query Data (the play button in the editor title) runs the current query with PQTest.exe and shows the result in a table you can filter, sort and resize. Select a row to read long values in full.
PQTest.exe comes with the Power Query SDK extension. M Lens finds it automatically, or you can set mlens.preview.pqTestPath.
Credentials:
- Local files and folders use your Windows account automatically.
- SQL Server: write the real server and database, for example
Sql.Database("sql01", "Sales"). The first time, M Lens asks for a login and saves it for that server and database only. A login is never sent to another server.
- Web sources: choose anonymous or Windows authentication when asked.
- APIs: add a credential with Manage Preview Credentials and read it with
MLens_GetCredential("Name"). This function only exists in previews, so M Lens refuses to apply a query that uses it to your PBIP project.
Secrets are kept in the VS Code secret storage. Only the credentials a query references go into the temporary preview file, which is deleted right after the run.
Read queries from a PBIX
Right-click a .pbix file and select Open Queries from PBIX (Read-only). Files saved in the newer format do not include the queries in a readable form; for those, M Lens explains how to convert the file to PBIP.
Workspace report
Show Workspace Report opens a single read-only report with data sources, locations of possible secrets, query dependencies and suggested next steps.
Commands
All commands are in the Command Palette under M Lens.
| Command |
What it does |
| Extract Queries from PBIP |
Copies every query from the .tmdl files into .pq files |
| Review PBIP Changes |
Shows the diff of what applying will change |
| Apply Queries to PBIP |
Writes the edited queries back, with a backup |
| Preview Query Data |
Runs the current query and shows the data |
| Manage Preview Credentials |
Lists, changes and removes saved logins and API credentials |
| Check All Queries |
Runs the linter on the whole workspace |
| Show Workspace Report |
Sources, possible secrets and dependencies |
| Open Queries from PBIX (Read-only) |
Reads the queries of a .pbix file |
| Show Log |
Opens the M Lens output channel |
Settings
| Setting |
Default |
Description |
mlens.lint.enabled |
true |
Check Power Query files with the M Lens rules |
mlens.lint.<rule> |
see table above |
Severity of each rule |
mlens.files.lintMFiles |
true |
Treat .m files as Power Query (turn off for MATLAB or Objective-C) |
mlens.preview.pqTestPath |
"" |
Path to PQTest.exe; empty to find it automatically |
mlens.preview.maxRows |
500 |
Maximum rows in the preview |
mlens.preview.injectCredentials |
true |
Make API credentials available through MLens_GetCredential |
Language
M Lens follows the display language of VS Code. English and Portuguese (Brazil) are included.
Known limitations
- Data preview needs Windows and the Power Query SDK.
- Sources with organizational sign-in (SharePoint Online, OAuth) need a credential set up with the Power Query SDK.
- Query dependencies are matched by file name, so the map is best-effort.
- The linter does not model shadowed identifiers in nested
let expressions.
Privacy
M Lens does not collect telemetry. It only reads and writes files in your workspace and the M Lens storage folder.
License
MIT