Change XML to Excel: Practical Guide Using PowerQuery

Learn how to change xml to excel with Power Query in Excel. This guide covers importing XML, flattening data, handling namespaces, and troubleshooting results for reliable analyses.

XLS Library
XLS Library Team
·5 min read
XML to Excel Guide - XLS Library
Photo by SCYvia Pixabay
Quick AnswerSteps

Learn how to change xml to excel by importing XML with Power Query, expanding nested elements into flat tables, and saving the result as an Excel workbook. You’ll need an XML file, Excel 2016+ or Microsoft 365, and a clear schema. This guide covers basic import, data shaping, namespace handling, and troubleshooting.

Understanding the XML-to-Excel workflow

Converting XML data into a usable Excel format is a common requirement for analysts who need to perform analyses in familiar tools without losing the structure of the source data. The general workflow centers on bringing XML into Excel via Power Query (Get & Transform), interpreting the hierarchical XML into relational tables, and then shaping those tables for reporting. In this context, you’re not just “copying” data; you’re mapping XML elements and attributes to a tabular schema that Excel can manipulate with formulas, charts, and dashboards. According to XLS Library, adopting a deliberate workflow reduces redundancy and error-prone manual edits. A well-planned import makes it easier to refresh data from source XML feeds, compare periods, and reuse the same mapping across similar XML documents.

Key concepts you’ll use include: the XML hierarchy, elements versus attributes, and the concept of “tables” derived from nested branches. If your XML has namespaces, you’ll need to decide how to map those into your Excel columns. The goal is to arrive at clean, columnar data that can be loaded into a worksheet or model for further analysis.

Preparing XML and Excel for Import

Before you start the import, ensure your environment is ready. Use well-formed XML with a single root element whenever possible; inconsistencies in root nodes can complicate mapping. If you’re dealing with large files, consider splitting the XML into chunks or using incremental loading strategies. In Excel, confirm you’re on a version with Power Query (Get & Transform) features—ideally Excel 2016+ or Microsoft 365. Create a dedicated workbook for the transformation so your raw XML stays intact and you can refresh outputs without breaking your analysis. Organization matters: keep the XML files in a clearly named folder and establish a conventional naming scheme for the resulting Excel workbooks. Finally, have a basic plan for how you want the resulting tables to appear (which elements map to which columns) to minimize late-stage rework.

Importing XML with Power Query

In Excel, start by opening a blank workbook and navigating to Data > Get Data > From File > From XML (if available in your version). Power Query will present a Navigator pane showing the XML structure. Select the top-level elements you want to convert into tables, or load the entire structure as a set of related queries. When you click Load, choose whether to load as a table or to only create a connection for later reshaping. If your Excel version does not show From XML, you can still import XML by using the XML Source approach or pre-processing the XML to reduce nesting, then loading via Power Query. Remember to review the applied steps in the Power Query Editor to ensure the import aligns with your planned mapping.

Transforming and Flattening XML into Tables

XML is hierarchical, which means you’ll typically see nested tables and repeated elements. The Power Query Editor lets you expand columns to flatten these hierarchies. Use the Expand button on a column to bring in child elements as new columns, then remove unused columns and rename the resulting fields for clarity. It’s common to perform type conversion (e.g., text to date or number), split combined fields, and create calculated columns to improve downstream reporting. If you encounter multi-level nesting, you may need to create intermediate queries that pivot nested data into a single flat table suitable for your model. Consistency in column names across facts makes later joins, filters, and pivoting far easier.

Handling Namespaces and Attributes

Namespaces can complicate XML imports because Power Query may treat namespaced elements as distinct from non-namespaced ones. If your XML uses namespaces, you can either remove the namespaces during preprocessing or map them in Power Query using M code to normalize element names. Attributes are another common source of confusion: they often appear in a separate column or as part of an element. Decide whether attributes should become separate columns or be concatenated with the element value. A clean approach is to create a mapping rule: element names map to column headers, while attributes map to additional columns with a consistent naming convention. This keeps the resulting dataset predictable for analysts and BI tools.

Troubleshooting Common Issues

Importing XML can fail for several reasons: malformed XML, mismatched namespaces, or overly deep nesting can impede import. If Power Query shows a navigation or parsing error, validate the XML with an XML validator or schema (XSD) when available. Large files may exhaust memory in Excel; consider chunking the data or increasing the machine’s memory where possible. If the data loads but contains missing values, revisit your mapping rules and ensure that every required element has a corresponding column. Finally, ensure you refresh your queries after source XML changes, and maintain a versioned export to avoid surprises in live dashboards.

Real-World Scenario: XML to Monthly Sales Report

Imagine XML data that contains monthly sales records from multiple regions and product lines. You’d map each Region, Month, Product, and SalesAmount to separate columns, expand nested items like RegionDetails, and create a date column from the Month field. With Power Query, you can refresh the workbook each month, automatically update the sales table, and preserve a historical view for trending analyses. By keeping a stable mapping and clean transformation steps, you enable repeatable reporting that scales with your data volume.

AUTHORITY SOURCES

For deeper reading, consult authoritative references on XML and Power Query:

  • https://www.w3.org/TR/xml/
  • https://docs.microsoft.com/en-us/power-query/power-query-reference
  • https://docs.microsoft.com/en-us/power-query/connectors/xml

These sources provide standardized guidance on XML syntax and the Power Query approach to data connectivity and transformation.

Tools & Materials

  • XML file(s) to convert(Source XML you want to import; ensure well-formed structure with a clear root element)
  • Microsoft Excel (365/2019+)(Version with Get & Transform / Power Query features)
  • Computer with internet access(For updates, templates, and remote data sources)
  • Sample XSD/schema (optional)(Helps map XML elements to tables during planning)
  • Reference XML structure diagram (optional)(A diagram can speed up planning the mapping)
  • Folder structure for outputs (optional)(Organize exported workbooks and backups)

Steps

Estimated time: 45-75 minutes

  1. 1

    Prepare Excel workbook

    Open a new workbook in Excel and create a dedicated sheet or model for transformed data. This ensures you keep a clean separation between raw XML data and the final reports.

    Tip: Label your sheets clearly (e.g., RawXML, FlatTables, Metrics) to avoid confusion later.
  2. 2

    Connect to XML via Power Query

    In Excel, go to Data > Get Data > From File > From XML (or use an XML connection). Use the Navigator to select the root element or the specific branches you want loaded as tables.

    Tip: If From XML isn’t visible, use an alternative importer or pre-process the XML to reduce nesting.
  3. 3

    Load as connection or table

    Choose to Load To Table if you want immediate cells, or Load To to create a connection for later shaping. Plan your approach based on whether you’ll need intermediate steps.

    Tip: Loading as a connection keeps the workbook lighter while you build transformations.
  4. 4

    Expand and flatten hierarchical data

    In the Power Query Editor, use the Expand button to flatten nested elements into columns. Remove unused columns and rename headers for clarity.

    Tip: Create intermediate queries when nesting is deep to keep steps manageable.
  5. 5

    Normalize data types

    Convert text to dates, numbers, and booleans as appropriate. Consistent data types improve filtering, joining, and aggregation.

    Tip: Apply type changes early to prevent later casting errors in formulas or pivots.
  6. 6

    Handle namespaces and attributes

    If namespaces appear, normalize names or remove prefixes. Map attributes to dedicated columns if needed for analysis.

    Tip: Maintain a naming convention that makes sense in downstream reports.
  7. 7

    Close & load to worksheet or data model

    Apply final cleanups and load the prepared tables into a worksheet or the Data Model for advanced analytics.

    Tip: Save a copy of the M code in Power Query for reproducibility.
Pro Tip: Always validate the XML against a schema before importing to avoid structural surprises.
Warning: Namespaces can complicate imports; consider preprocessing to simplify names.
Note: If the XML file is large, load in chunks or use incremental refresh strategies when available.
Pro Tip: Document each Power Query step so others can reproduce the results.

People Also Ask

Can I convert XML with namespaces?

Yes. You can remove namespaces during preprocessing or normalize names inside Power Query using M code so all elements map cleanly to columns.

Yes, you can handle namespaces by normalizing names in Power Query or preprocessing the XML.

What if the XML is very large?

For large XML files, split the XML into chunks or load in batches. Use Power Query connections rather than loading everything at once to keep Excel responsive.

For large XML files, try chunking or batching the import to keep Excel responsive.

Can I export results to CSV or other formats?

Yes. After shaping the data, you can Load To to a worksheet and then Save As CSV or use Power Query steps to export to other destinations.

Yes, you can export the transformed data to CSV or other destinations after loading it.

How do I preserve data types during import?

Apply explicit Type changes in Power Query (e.g., Date, Decimal Number, Text) before loading to ensure consistent results.

Apply explicit data types in Power Query before loading.

Can this process be automated on a schedule?

Power Query steps can be saved and refreshed programmatically or via Power Automate in some configurations, enabling regular XML-to-Excel updates.

Yes, using automation tools you can refresh XML-to-Excel on a schedule.

Watch Video

The Essentials

  • Plan your XML-to-Excel mapping before importing
  • Use Power Query to flatten nested XML effectively
  • Normalize data types early for reliable analysis
  • Handle namespaces consistently to avoid surprises
  • Document steps for repeatable reporting
Three-step infographic showing import, flatten, and load steps for XML to Excel
Process: Import XML, Flatten, Load into Excel

Related Articles