Yes, you can transform your messy Power Query code into clean, readable text instantly, but you need to know exactly where to look. The Power Query Editor in Excel and Power BI has a built-in "Format Query" command that automatically indents formulas, aligns brackets, and adds line breaks to make complex M code readable. This formatting does not change the logic or results of your query; it only changes how the code looks. To use it, open the Power Query Editor, click anywhere inside the query you want to format, and navigate to the "Home" tab in the ribbon. Look for the "Format" button in the Tools group on the far right of the ribbon. Clicking "Format Query" will instantly restructure your code. If you are working with a large data model and cannot easily click on the specific query, you can also use the "Advanced Editor" window. Press the "Advanced Editor" button in the Home tab, which opens a text window. You can then select all the code inside that window and use the "Format Query" button in the Excel ribbon again, or simply close the window to save the changes. This process works for both desktop versions of Excel and Power BI Desktop, though the ribbon layout may differ slightly by version. The formatting is based on standard M language rules, so it will not work on queries that contain syntax errors, such as unmatched quotes or brackets, as the editor cannot parse the code to indent it correctly.
What You'll Need
- A Compatible App: You need Microsoft Excel (2016 or later) or Power BI Desktop. The formatting tools are not available in the standalone Power Query Editor application.
- A Query with Code: The feature requires a query that has been transformed and has generated M code. A simple "Get Data" query that only loads raw data without any steps will not have enough code to format, though the button will still be present.
- Internet Connection (Optional): While the formatting tool itself is local, Power BI Desktop sometimes checks for updates when the ribbon loads. An internet connection ensures you have the latest version of the software.
Step-by-Step
- Open the Power Query Editor: Launch your Excel workbook or open Power BI Desktop. Go to the "Data" tab in Excel (or the "Home" tab in Power BI) and click "From Table/Range" or "Get Data" to load your data. Once the data is loaded, the Power Query Editor window will open automatically.
- Locate the Target Query: In the "Queries and Connections" pane on the left side of the window, you will see a list of your queries. Click on the name of the specific query you want to format. The main query preview pane on the right will update to show the data for that query.
- Select the Entire Query (Optional but Recommended): While the "Format Query" button formats the active query, it is best practice to select the entire query code to ensure no hidden steps are missed. In the "Queries and Connections" pane, right-click on the query name and select "Advanced Editor." This will open a text window containing the M code for that query. You can also select the query name in the main preview pane and press Ctrl+A to select all the steps in the query tree.
- Open the Format Menu: Look at the top of the window for the "Home" tab in the ribbon. On the far right side of this tab, in the "Tools" group, you will find the "Format" button. Click on this button to reveal the menu options.
- Trigger the Formatting: In the dropdown menu that appears, click on the "Format Query" option. If you have the Advanced Editor open, you can also select all the code in the text window and click the ribbon's "Format Query" button. The editor will now process the M code.
- Review the Result: The code in the text window or the query steps pane will instantly restructure itself. Indentation will be added to make the nesting of functions clear. For example, a complex step like
Table.AddColumn(Source, "Total", each [Price] * [Qty])might be expanded and indented to show that theeachfunction is a lambda expression. The brackets will be aligned vertically to improve readability. - Save the Workbook: Once you are satisfied with the formatting, simply close the Advanced Editor window to save your changes in the Power Query Editor. In Excel, you must also save the Excel workbook to save the changes to the query definition.
Common Mistakes
One of the most common mistakes is attempting to format a query that contains a syntax error. If your Power Query code has a typo, such as a missing comma or a mismatched quote, the "Format Query" button will not work. Instead, you may receive an error message indicating that the query is not valid. You must fix the syntax error in the Advanced Editor before you can successfully format the query. Another mistake is relying on manual formatting. While you can use the indent and outdent buttons in the Advanced Editor to manually adjust code, this is time-consuming and error-prone. The "Format Query" button is a much faster and more reliable way to achieve consistent results. Some users also mistakenly believe that formatting the query will change the data. It is important to understand that formatting is purely cosmetic; it does not alter the logic, the data types, or the final output of the query. It simply changes how the code is written and displayed. Finally, some users try to format a query that is completely blank or has no steps. If a query has not been transformed yet, there will be no code to format, and the button will appear grayed out or have no effect.
Tips
When working with large, complex queries, it is often helpful to format the code section by section rather than all at once. You can achieve this by using the "Advanced Editor" and commenting out the sections of code you do not want to format yet. This allows you to isolate and format specific steps for easier debugging. Another helpful tip is to use the "Format Query" button frequently. As you build your query, you can format it at the end of each major step. This keeps your code clean and readable throughout the development process, making it much easier to find and fix errors later on. When sharing your query with others, you can use the "Format Query" button to ensure the code is presented in a standard, readable format. This makes it easier for others to understand the logic of your query and modify it if necessary. You can also use the formatting to align your code with the official Microsoft M language style guide, which is a best practice for professional data engineering. Finally, remember that the formatting is reversible. If you accidentally format a query and are unhappy with the result, you can simply revert to the previous version of the code using the Excel Undo command (Ctrl+Z) or by restoring a previous save of the workbook.
Troubleshooting
If the Format Query button is grayed out and cannot be clicked, it is usually because no query is selected or the query is empty. Ensure you have clicked on a query in the "Queries and Connections" pane. If the query has no steps or has only one step, the button will not do anything. If you receive an error message when trying to format, check the Advanced Editor for syntax errors. Look for common issues like unclosed strings (missing quotation marks), mismatched brackets, or incorrect function names. Fix these errors first. If you are working in Power BI Desktop and the formatting does not appear to have changed the code, ensure you are looking at the correct view. The ribbon button formats the code in the Advanced Editor, but it does not automatically reformat the step names in the main query preview pane. You must close the Advanced Editor to see the changes reflected in the list of steps. If the formatting seems inconsistent with your expectations, it is likely due to the specific style of the M code you are using. The "Format Query" button applies a standard indentation style, but it may not match the style you are used to. You can always use the indent and outdent buttons in the Advanced Editor to manually adjust the code to your preferred style after the automatic formatting is applied.
Frequently Asked Questions
Does formatting the query change the data it returns?
No, formatting only changes the appearance of the code. It does not alter the logic, the data types, or the final output of the query.
Where is the Format Query button located?
It is located in the "Home" tab of the ribbon in the Power Query Editor, in the "Tools" group on the far right side.
What should I do if the formatting button is not working?
Ensure your query has steps and is selected. If you receive an error, check the Advanced Editor for syntax errors like missing quotes or brackets.
Can I format a query from a Power BI Service report?
No, the "Format Query" button is only available in the desktop applications. You can only view the code in the Service, not edit or format it.
Does this work on Excel Online or the web version?
No, the formatting tools are not available in the web versions of Excel or Power BI. You must use the desktop application.