Home > database >  extract data from another excel in power query advanced editor
extract data from another excel in power query advanced editor

Time:01-29

I'm trying to extract the data from another excel file, but I tried to make it dinamyc. This is the context so I'm extractig data with a survey for different cities and the questions are the same but the data is different in each city, so I'm trying to create visualisations for each city but just replacing the data in the file, so this data is exported as a file we can call it "results-city.xlsx" and my goal is just placing this document with another with the same name and columns but obviusly different responses in each column so I'm trying to use power query and the advanced editor and this is my formula but is not getting success also the path will be dynamic that's the reason I included folder in the formula. Help please to achive this

   let
    Source = Excel.Workbook(File.Contents("C:\Users\iotal\OneDrive\Desktop\stack\folder\results-city.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Sheet1_Sheet,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}})
in
    #"Changed Type"

Update 2:

Example of data: enter image description here

And the second image is when I replace the data for city2 should look like this just replacing the file for another one with the same name enter image description here

And what is not working is the formula that doesn't import to a sheet the data as a table

When I replace the file from city1 for the one from city2 I got this error:

enter image description here

CodePudding user response:

In powerquery, something like this will combine all tabs in all xlsx files in a specified hardcoded directory

let Source = Folder.Files("C:\subdirectory\directory"),
#"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".xlsx")),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Name", "Content"}),
#"Added Custom" = Table.AddColumn(#"Removed Other Columns", "GetFileData", each Excel.Workbook([Content],true)),
#"Expanded GetFileData" = Table.ExpandTableColumn(#"Added Custom", "GetFileData", {"Data", "Hidden", "Item", "Kind", "Name"}, {"Data", "Hidden", "Item", "Kind", "Sheet"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded GetFileData",{"Content", "Hidden", "Item", "Kind"}),
List = List.Union(List.Transform(#"Removed Columns"[Data], each Table.ColumnNames(_))),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", List,List)
in  #"Expanded Data"

Alternatively, give a cell a range name, like path in excel and put your filepath in that cell, like C:\temp\a.xlsx

Then in powerquery, use that range name in place of hardcoding the file name like

let location= Excel.CurrentWorkbook(){[Name="path"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(location), null, true),                           
...

this second alternative assumes that the tab name is constant. Otherwise I recommend the first method

You can combine the two alternatives if you want

CodePudding user response:

To make the folder location dynamic with respect to a cell with named range "file_path" (B2, below/ Exampe file - linked


(optional)

Include VB code to refresh query every time the drop down list changes in the cell with named range ("file_path") (i.e. cell B2 above gif) - as follows: (you don't have to do this - you could just as easily make the query refreshable periodically via the properties UI associated with the query, for instance)..

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("file_path")) Is Nothing Then
    Application.ScreenUpdating = False
    Calculate
    ActiveWorkbook.Connections("Query - dynamic_file").Refresh
    Application.ScreenUpdating = True
    Calculate
    
    
End If


End Sub

Kudos to Cristiano Galvão (here) for more details RE: dynamic links per above.

ta

  • Related