Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Combine all sheets with different names from multiple files

Hi,   I have multiple cash book files representing different companies, each file has multiple worksheets, and each worksheet has a different name to represent a different bank account. All worksh...
  • Jimmy801's avatar
    Jimmy801
    5 years ago

    Hello Anonymous 

     

    read from a folder, where you have all your files. On the folder data add this formula (hopefully you are reading from a sheet and not from a table, otherwise you would need to adapt the code a little bit

    Table.Combine( List.Transform( Excel.Workbook([Content])[Data], each Table.PromoteHeaders(_)))

    this formula gives you combines tables form every single file. Right click on your step on the right side and choose insert step after. User here now again a table.combine where you are reference your previous step and your newly created column

    Table.Combine(#"Added Custom"[TablesFromFile])

    here a complete example

    let
        Source = Folder.Files("YourFolderWithFilesToMerge"),
        #"Added Custom" = Table.AddColumn(Source, "TablesFromFile", each Table.Combine( List.Transform( Excel.Workbook([Content])[Data], each Table.PromoteHeaders(_)))),
        CombineAllFiles = Table.Combine(#"Added Custom"[TablesFromFile])
    in
        CombineAllFiles

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. 

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • Jimmy801's avatar
    Jimmy801
    5 years ago

    Hello Anonymous 

     

    this code now adds the sheet name before combining

    let
        Source = Folder.Files("YourFolderWithFilesToMerge"),
        #"Added Custom" = Table.AddColumn(Source, "TablesFromFile", each Table.Combine(Table.AddColumn(Excel.Workbook([Content]), "WithSheetName", (add)=> Table.AddColumn(Table.PromoteHeaders(add[Data]),"Sheet name", each add[Name] ))[WithSheetName])),
        CombineAllFiles = Table.Combine(#"Added Custom"[TablesFromFile])
    in
        CombineAllFiles

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy