Forum Discussion
Import multiple XML files
- Anonymous10 years ago
The way I usually do it is with 2 queries: a folder query to get the paths/names of the files, and a function query to parse them.
Steps:
- Query the folder where your XML files are
- Add a column that combines the path and file name to make an absolute path to the XML file and call it [Path]
- Create an XML query to one of the XML files in the folder
- Open advanced editor and create a parameter by adding (path as text) => at the top
- Replace the path specified inside Folder.Files() to path
- Close the advanced editor and name the query "getXML"
- In the Folder Query, add a custom column and call the getXML function by stating getXML([Path])
- Close and Load
XML Parse Function Query
let Source = Folder.Files("C:\XML Files"), #"Removed Other Columns" = Table.SelectColumns(Source,{"Name", "Folder Path"}), #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Path", each [Folder Path] & [Name]), #"Added Custom1" = Table.AddColumn(#"Added Custom", "XML Data", each getXML([Path])), #"Expanded XML Data" = Table.ExpandTableColumn(#"Added Custom1", "XML Data", {"TITLE", "ARTIST", "COUNTRY", "COMPANY", "PRICE", "YEAR"}, {"TITLE", "ARTIST", "COUNTRY", "COMPANY", "PRICE", "YEAR"}) in #"Expanded XML Data"Folder Query
(path as text) => let Source = Xml.Tables(File.Contents(path)), Table0 = Source{0}[Table], #"Changed Type" = Table.TransformColumnTypes(Table0,{{"TITLE", type text}, {"ARTIST", type text}, {"COUNTRY", type text}, {"COMPANY", type text}, {"PRICE", type number}, {"YEAR", Int64.Type}}) in #"Changed Type"Message me if you would like an example PBIX file that does this
Alex
The way I usually do it is with 2 queries: a folder query to get the paths/names of the files, and a function query to parse them.
Steps:
- Query the folder where your XML files are
- Add a column that combines the path and file name to make an absolute path to the XML file and call it [Path]
- Create an XML query to one of the XML files in the folder
- Open advanced editor and create a parameter by adding (path as text) => at the top
- Replace the path specified inside Folder.Files() to path
- Close the advanced editor and name the query "getXML"
- In the Folder Query, add a custom column and call the getXML function by stating getXML([Path])
- Close and Load
XML Parse Function Query
let
Source = Folder.Files("C:\XML Files"),
#"Removed Other Columns" = Table.SelectColumns(Source,{"Name", "Folder Path"}),
#"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Path", each [Folder Path] & [Name]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "XML Data", each getXML([Path])),
#"Expanded XML Data" = Table.ExpandTableColumn(#"Added Custom1", "XML Data", {"TITLE", "ARTIST", "COUNTRY", "COMPANY", "PRICE", "YEAR"}, {"TITLE", "ARTIST", "COUNTRY", "COMPANY", "PRICE", "YEAR"})
in
#"Expanded XML Data"Folder Query
(path as text) =>
let
Source = Xml.Tables(File.Contents(path)),
Table0 = Source{0}[Table],
#"Changed Type" = Table.TransformColumnTypes(Table0,{{"TITLE", type text}, {"ARTIST", type text}, {"COUNTRY", type text}, {"COMPANY", type text}, {"PRICE", type number}, {"YEAR", Int64.Type}})
in
#"Changed Type"
Message me if you would like an example PBIX file that does this
Alex
- sse10 years agoRegular Visitor
Great, thank you! I got it to work now. Excellent.
- amien10 years agoHelper V
Edit : Nevermind .. i got it working
Can you upload the pbix as example Alex? Thanks
- Anonymous10 years agoNot applicable
Hi amien,
Below is a link to a zip folder containing the example XML files and a PBIX file. If you extract the folder to your C drive, everything should work without editing the path in the query.
- amien10 years agoHelper V
Hi Alex, could you share the pbix please?
- Anonymous10 years agoNot applicable
Hi
what if i need to import from a folder some xlsb files with the same structure but i what to import only one worksheet?
Many many thanks!
- Anonymous9 years agoNot applicable
Is there ever likely to be a more solid solution to this? Don't get me wrong I like your solution but its more of a workaround until a proper solution is put in place.
Is it likely we will see XML given more attention as its a common format I am hit with and the thought of trying to manage to import it into Power Bi especially when the XML reflects a schema of several tables is just plain hairy.
Maybe even just letting powebi hook up to an XML database and create the connection that way would be more optimal.
- Anonymous8 years agoNot applicable
Thank you :)
- benghee7 years agoNew Member
Nice clean solution that still works in 2019!