cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
sse Frequent Visitor
Frequent Visitor

Import multiple XML files

Is it possible to use the import from folder for multiple XML files? I have manged to extract the data I need from a single XML file using "import from XML" but not with the files from folder. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
alex-potter Regular Visitor
Regular Visitor

Re: Import multiple XML files

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:

  1. Query the folder where your XML files are
  2. Add a column that combines the path and file name to make an absolute path to the XML file and call it [Path]
  3. Create an XML query to one of the XML files in the folder
  4. Open advanced editor and create a parameter by adding (path as text) => at the top
  5. Replace the path specified inside Folder.Files() to path
  6. Close the advanced editor and name the query "getXML"
  7. In the Folder Query, add a custom column and call the getXML function by stating getXML([Path])
  8. 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

Alex Potter
Lead Visualization Engineer
Luxoft Inc.

View solution in original post

13 REPLIES 13
PowerBIGuy Member
Member

Re: Import multiple XML files

I just tested this and it worked for me. 

 

Cheers

Business Intelligence Architect / Consultant
sse Frequent Visitor
Frequent Visitor

Re: Import multiple XML files

The "from XML" and "from folder gives different output and I am not able to nest out the relevant data in "from folder"

 

From XML

 

let
Source = Xml.Tables(File.Contents("C:\337.xml")),
Table0 = Source{0}[Table],
Table1 = Table0{1}[Table],
#"Changed Type" = Table.TransformColumnTypes(Table1,{{"Id", Int64.Type}, {"LglSeqNb", Int64.Type}, {"CreDtTm", type datetime}})
in
#"Changed Type"

 

From Folder

 

    Source = Folder.Files("W:\XML folder")

sse Frequent Visitor
Frequent Visitor

Re: Import multiple XML files

Is it possible to combine Source = Xml.Tables and Source = Folder.Files ?

alex-potter Regular Visitor
Regular Visitor

Re: Import multiple XML files

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:

  1. Query the folder where your XML files are
  2. Add a column that combines the path and file name to make an absolute path to the XML file and call it [Path]
  3. Create an XML query to one of the XML files in the folder
  4. Open advanced editor and create a parameter by adding (path as text) => at the top
  5. Replace the path specified inside Folder.Files() to path
  6. Close the advanced editor and name the query "getXML"
  7. In the Folder Query, add a custom column and call the getXML function by stating getXML([Path])
  8. 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

Alex Potter
Lead Visualization Engineer
Luxoft Inc.

View solution in original post

sse Frequent Visitor
Frequent Visitor

Re: Import multiple XML files

Great, thank you! I got it to work now. Excellent.

amien Member
Member

Re: Import multiple XML files

Hi Alex, could you share the pbix please?

amien Member
Member

Re: Import multiple XML files

 

Edit : Nevermind .. i got it working

 

Can you upload the pbix as example Alex? Thanks

 

 

alex-potter Regular Visitor
Regular Visitor

Re: Import multiple XML files

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.

XML Files Zip Folder

Alex Potter
Lead Visualization Engineer
Luxoft Inc.
hgorina Frequent Visitor
Frequent Visitor

Re: Import multiple XML files

Alex, thanks for your reply, it halped me a lot in understanding how BI works. however I have an additional challenge- i have to combine multiple XML files with the same name and schema but from different folders into one datase. I was thinking of creating separate wueries for each file, but the folders are getting added all the time...

 

any advise?

 

thanks so much!

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

New Badges

Incoming: New and Improved Badges

Exciting news: We've given our badges an overhaul and added brand news ones.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 193 members 1,788 guests
Please welcome our newest community members: