Forum Discussion
XML import problem
- 10 months ago
Hi GrahamR ,
You are correct this is the expected behavior.
The reason you are seeing only null values is that the plain text “Site A” is not retained when Power BI parses the XML. The Xml.Tables function processes only structured elements and attributes, and in your file, “Site A” appears as loose text before the <Shelf> and <Box> elements. As a result, it is considered mixed content and omitted during import.
Since this text does not make it into Power Query, it cannot be retrieved by any M or DAX expressions after the XML is loaded.
The best solutions are to either update the XML so that “Site A” is within its own tag or attribute, or to read the file as raw text and extract the relevant section before <Shelf> using text functions prior to converting it to XML.
In summary, your understanding is correct the data loss occurs during the XML parsing stage, and is not due to any issues with your process.
Thank you,Tejaswi
Hi GrahamR ,
You're correct the XML you received is valid, but it contains mixed content (plain text combined with child elements within the same tag). As a result, Power Query does not automatically display the “Site A” text in the standard columns when expanding the XML.
To extract this text, you can use a custom column in Power Query. The following method is compatible with your XML structure:
let
xml = Xml.Tables([ObjectLocation]),
loc = try xml[Location]{0} otherwise null,
textNode = try Record.Field(loc, "#text") otherwise null
in
if textNode = null then null
else if Value.Is(textNode, Binary.Type) then Text.Trim(Text.FromBinary(textNode))
else Text.Trim(Text.From(textNode))
This approach retrieves the text node preceding the <Shelf> and <Box> elements, providing “Site A” as plain text in a new column.
The “Invalid Identifier” error occurred because [#text] is not a valid identifier in M. To access this field correctly, use Record.Field(loc, "#text") or loc["#text"].
I hope this solution helps you achieve the desired outcome.
Best regards,
Tejaswi.
Community Support Team
Hi Tejaswi
Many thanks for your suggested approach however this is giving me nulls. I tried simplifying the method but still get nulls all the time.
My latest thought is whether the XML input process is filtering out the 'extra' text so that the Xml.Tables does not contain the text string at all.
Regards
- v-tejrama10 months agoCommunity Support
Hi GrahamR ,
You are correct this is the expected behavior.
The reason you are seeing only null values is that the plain text “Site A” is not retained when Power BI parses the XML. The Xml.Tables function processes only structured elements and attributes, and in your file, “Site A” appears as loose text before the <Shelf> and <Box> elements. As a result, it is considered mixed content and omitted during import.
Since this text does not make it into Power Query, it cannot be retrieved by any M or DAX expressions after the XML is loaded.
The best solutions are to either update the XML so that “Site A” is within its own tag or attribute, or to read the file as raw text and extract the relevant section before <Shelf> using text functions prior to converting it to XML.
In summary, your understanding is correct the data loss occurs during the XML parsing stage, and is not due to any issues with your process.
Thank you,Tejaswi
- GrahamR10 months agoFrequent Visitor
Hi Tejaswi
This is in some ways a relief (!) after 4 days of getting nowhere. I am going to use a workaround along the lines you suggest so I can get these reports out to the client.
My other takeaway is say thank you to yourself and to the wider community for responding with ideas and suggestions. This has been a life-saver!
Very best wishes, Graham