To import an XML file into Power Query, navigate to the Data tab in Excel or Power BI, select Get Data, choose From File, and then select From XML. This process converts hierarchical, nested XML data into a flat, tabular format that you can use for analysis and reporting.
What You'll Need
- Microsoft Excel (Desktop version recommended) or Power BI Desktop.
- A valid XML file stored on your local drive, a network share, or a reachable URL.
- A basic understanding of how data rows and columns work in a spreadsheet format.
Step-by-Step
- Locate the Data Source: Open your Excel workbook or Power BI Desktop application. Navigate to the top ribbon and click on the Data tab in Excel or the Home tab in Power BI. Note that menu labels and ribbon layouts can vary slightly depending on your specific version of Office or Power BI.
- Initiate the Import: Look for the Get Data button. Click it to open the dropdown menu. Hover over From File and then select From XML. This tells the engine to look for the specific markup language structure used in XML files.
- Select Your File: A file explorer window will appear. Navigate through your computer's folders to find the specific XML file you wish to import. Once you have highlighted the file, click Import.
- The Navigator Window: Once the file is selected, the Navigator window will appear. This window is a visual representation of the XML tree. On the left pane, you will see the different levels or elements detected within the XML hierarchy. XML is structured like a tree; clicking on different branches (elements) will show you a preview of the data contained within that specific level in the preview pane on the right.
- Choose Your Entry Point: XML files are often deeply nested, meaning data is tucked inside multiple layers of tags. You must decide which level of the hierarchy contains the actual data rows you want to analyze. Usually, you are looking for the repeating element that represents a single record, such as <Employee>, <Order>, or <Product>. Select that element in the Navigator to ensure you are starting at the correct depth.
- Enter the Power Query Editor: Do not click Load immediately. If you click Load, Power Query will attempt to bring in the data exactly as it sees it, which is often not useful for spreadsheets. Instead, click the Transform Data button. This opens the Power Query Editor window, which is the workspace required for flattening the XML structure.
- Expand Nested Tables: This is the most critical phase of the process. When you import XML, Power Query represents nested elements as columns containing specialized values like Table, List, or Record. To see the actual data, look for the expansion icon at the top right of the column header—it looks like two arrows pointing away from each other in opposite directions. Click this icon to expand the column.
- Select Columns to Expand: After clicking the expansion icon, a list of all available sub-elements (the child tags in the XML) will appear. You can check or uncheck specific fields. It is highly recommended to uncheck the box labeled "Use original column name as prefix". If you leave this checked, your column headers will become long and cumbersome, such as Customer.Address.City instead of just City. Select the specific fields you need and click OK.
- Repeat for Deep Nesting: If your XML has multiple layers of nesting (for example, a Customer tag contains an Address tag, which in turn contains a City tag), you may need to repeat this expansion process multiple times. You will keep seeing Table or Record columns until you have reached the "leaf" level of the tree where the actual text or number values reside.
- Set Data Types: Once the data is flat and visible, check the icon to the left of each column header. You might see ABC for text, 123 for whole numbers, or a small calendar icon for dates. XML is a text-based format, so Power Query often defaults everything to the Text type. You must manually change these to Decimal Number, Date, or Currency to ensure you can perform mathematical operations or time-based filtering later.
- Load the Data: Once the table looks correct and the columns are properly typed, click Close & Load in the top left corner of the Power Query Editor to bring the cleaned, flattened data into your Excel worksheet or Power BI data model.
Common Mistakes
The most frequent error is clicking Load immediately from the Navigator window. Because XML is hierarchical and spreadsheets are flat, loading without transforming results in a single row containing a single cell of data, or a table filled with the word Table instead of your actual values. You must use the Transform Data path to "unwrap" the layers of the XML.
Another common mistake is failing to handle List columns correctly. In XML, a single parent tag can contain multiple child tags of the same name (a one-to-many relationship). Power Query represents this as a List. If you try to expand a List as if it were a Record, you will encounter an error. To fix this, click the expansion icon and select Expand to New Rows. This will create a new row for every item in that list, duplicating the information in the other columns to maintain the relationship. This is necessary to prevent data loss, but it will increase your total row count.
Users also frequently forget to verify data types. If a column contains numbers but is formatted as Text, you will not be able to use the Sum or Average functions in Excel or Power BI. Always perform a final check of the data types in the Power Query Editor before the final load.
Tips for Advanced XML Handling
When working with complex XML files, use the Remove Other Columns feature to keep your queries clean. Once you have expanded the specific nested elements you need, select those columns, right-click, and choose Remove Other Columns. This strips away all the unnecessary metadata and parent tags that you don't need for your report, making the query run faster and making the Applied Steps list much easier to manage.
If you are dealing with multiple XML files that share the same structure, do not import them one by one. Instead, use the Get Data > From File > From Folder option. When you point Power Query to a folder, it can look at all the files inside, identify the XML files, and use a "Combine Files" function to merge them into one single master table. This is the most efficient way to handle weekly or monthly data exports.
Troubleshooting
If you encounter the error "The key didn't match any rows in the table," it usually indicates that the XML schema has changed. This happens if the source system that generates the XML file renamed a tag (for example, changing <Price> to <UnitCost>). To fix this, open the Power Query Editor, look at the Applied Steps pane on the right, and find the Navigation or Expanded step where the error occurs. You will likely need to edit the formula bar to reflect the new tag name.
If your data appears Empty or you see zero rows after importing, open the XML file in a basic text editor like Notepad. Check to see if the file is actually populated with data between the tags or if it is just a header/template. If the file is massive (exceeding several hundred megabytes), Power Query may struggle due to system RAM limitations. In these cases, ensure you are using the 64-bit version of Excel or Power BI, which can access significantly more memory than the 32-bit version.
If you see an error stating "We cannot convert the value null to type Text," this occurs when you attempt to apply a text transformation (like Trim or Upper Case) to a cell that contains a null value. To resolve this, go to the column in Power Query, right-click the header, and select Replace Values. Replace null with an empty string (leave the 'Replace With' box empty) before applying your text transformations.
Frequently Asked Questions
Can I import XML files directly from a website URL?
Yes. Instead of choosing From File, select Get Data > From Other Sources > From Web. Paste the direct URL of the XML file. Power Query will then fetch the data from the web and allow you to use the same expansion steps described above.
Why does my XML import create so many duplicate rows?
This is caused by the Expand to New Rows action. If one customer has three different addresses listed in the XML, expanding that address list will create three separate rows for that one customer. This is the standard way to represent hierarchical data in a flat table, but you must be careful when performing counts or sums to avoid double-counting.
Is there a limit to the size of the XML file I can import?
There is no specific file size limit within the software, but you are limited by your computer's hardware, specifically your RAM. Very large files can cause the application to slow down or become unresponsive during the transformation process.
Do I need to know XML coding to use Power Query?
No. Power Query provides a visual interface that allows you to navigate the XML structure and expand elements without ever writing a single line of XML code. The software handles the parsing and syntax in the background.