Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
cristianml
Post Prodigy
Post Prodigy

Powr query editor - change path same file

Hi,

I need to map the source of my data to an Excel TAB instead of a table, in this case called "FT Actual" to use as a table for my model in Power Query. I cannot create the table from power pivot because I have created, in the same file, a macro that paste the new data information above the other. So this makes that any table created to the model disappear.

 

What I have in mind is to use the get the data from Workbook option but the problem is that I would like to change in the advance editor the path so it get the data from the same file instead of the following :

 

File.Contents("C:\Users\cristian.longatti\Desktop\Macro Actual vs Forecast.xlsm"

 

Advance editor:

 

let
Source = Excel.Workbook(File.Contents("C:\Users\cristian.longatti\Desktop\Macro Actual vs Forecast.xlsm"), null, true),
#"FT Actual_Sheet" = Source{[Item="FT Actual",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(#"FT Actual_Sheet", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Category", type text}, {"Category Group", type text}, {"Actual or Forecast", type text}, {"Amount", type number}, {"Currency:", type text}, {"Contract Number: ", type number}, {"Contract Name:", type text}, {"WMU: ", type text}, {"Customer Number:", type number}, {"Customer Name:", type text}, {"Forecast Version:", type text}})
in
#"Changed Type"

 

 

Any ideas ??

 

Thanks.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

To build a query, a person look at the source of the data and the required output to build a transformation. No one (to my knowledge) can look at vba code and build a query. I can only tell that the macro is opening several excel workbooks, picking certain fields and then copy pasting the data into a destination workbook. This process is pretty simple by selecting the From Folder option in Power query.

 

P1.PNG

 

You would then need to modify the resulting sample query to pull in only the fields that you need and load to the Data model. All of this is quite simple to build if you have access to the data sources and are at least an intermediate Power Query user. If you are system saavy and want to learn the tool, then you could try it out. If not, you should probably hire someone to setup the query and the architecture.

 

P2.PNG

 

 

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Instead of using a macro to bring the data into the excel workbook, why don't you use Power query to bring the data directly from the source into the model directly? Power query was built for this purpose.

Hi @Anonymous ,

 

The macro that I mentioned before is not used to bring the information to the tab, this is only one part of the process, the macro does some kind of Special tranformation of some reports from a specific format into a table format and paste that tranformed information into the Excel TAB. I don´t think that Power Query could do that :

 

If you want to look at the Macro VBA script  see below :

 

Sub FTActual()
'PURPOSE: This Macro transform all Forecast Trends into a tabular format.

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False


'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
.Title = "Select a Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""

'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
'DoEvents

'Code

Worksheets.Add.Name = "FT MME Data"

ThisWorkbook.Activate
Sheets(Sheets.Count).Select
Range("A:F").Copy
Workbooks(2).Activate
Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False


Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Columns("B:B").Select
Selection.ClearOutline
Range("B13:C13").Select
ActiveCell.FormulaR1C1 = "Actual or Forecast"
Range("B14:C14").Select
Sheets("FT MME Data").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(HLOOKUP(RC[-2],'MME_FI_Forecast_Trend_Report_NH'!R12C4:R13C112,2,0),"""")"
Range("F2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC[-4],'MME_FI_Forecast_Trend_Report_NH'!R12C2:R180C112,MATCH('FT MME Data'!RC[-3],'MME_FI_Forecast_Trend_Report_NH'!R12C2:R12C112,0),0),0)"

Range("E2").Select
Selection.Copy
ActiveCell.Offset(0, -2).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 2).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

Range("F2").Select
Selection.Copy
ActiveCell.Offset(0, -2).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 2).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False


' Currency


Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("G4").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("G1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("H4").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("G2").Select
Selection.Copy
ActiveCell.Offset(0, -4).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 4).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues


' Copy Contract Number

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("B7").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("H1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("C7").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("H2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("H2").Select
Selection.Copy
ActiveCell.Offset(0, -4).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 4).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues



' Copy Contract Name

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("D7").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("I1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("E7").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("I2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("I2").Select
Selection.Copy
ActiveCell.Offset(0, -4).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 4).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues



' Copy WMU

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("G6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("J1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("H6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("J2").Select
Selection.Copy
ActiveCell.Offset(0, -5).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 5).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues


' Copy Customer Number

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("B6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("K1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("C6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("K2").Select
Selection.Copy
ActiveCell.Offset(0, -5).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 5).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues


' Copy Customer Name

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("D6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("L1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("E6").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("L2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("L2").Select
Selection.Copy
ActiveCell.Offset(0, -5).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 5).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

' Copy Forecast Version

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("B9").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("M1").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("MME_FI_Forecast_Trend_Report_NH").Select
Range("C9").Select
Selection.Copy
Sheets("FT MME Data").Select
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range("M2").Select
Selection.Copy
ActiveCell.Offset(0, -5).Select
ActiveCell.End(xlDown).Activate
ActiveCell.Offset(0, 5).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues


' Copy Column formatting

Columns("B:B").Select
Selection.Copy
Columns("G:M").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False


ActiveSheet.Columns("A:M").AutoFit

ActiveSheet.Columns("C").Delete

Application.DisplayAlerts = False
Columns("E:E").Select
Selection.AutoFilter
ActiveSheet.Range(Selection, Selection.End(xlUp)).AutoFilter Field:=1, Criteria1:="-"
Range("E2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete
Selection.AutoFilter
Range("A1").Activate

Sheets("MME_FI_Forecast_Trend_Report_NH").Delete

ActiveSheet.Name = "FT"

ActiveWorkbook.Close SaveChanges:=True


'Ensure Workbook has closed before moving on to next line of code
'DoEvents

'Get next file name
myFile = Dir
Loop

ThisWorkbook.Activate
Sheets("FT Actual").Cells.Clear


ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
DoEvents

'Code


Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

ThisWorkbook.Activate
Sheets("FT Actual").Select

Range("Z1").Select
ActiveCell.End(xlDown).Activate
ActiveCell.End(xlToLeft).Activate
ActiveCell.End(xlUp).Activate
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False


Workbooks(2).Activate

ActiveWorkbook.Close SaveChanges:=True


'Ensure Workbook has closed before moving on to next line of code
DoEvents

'Get next file name
myFile = Dir
Loop

ThisWorkbook.Activate
Sheets("FT Actual").Select

Range("1:1").Select
Selection.Delete
Application.DisplayAlerts = False
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range(Selection, Selection.End(xlUp)).AutoFilter Field:=1, Criteria1:="Date"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete
Selection.AutoFilter
Range("A1").Activate
ActiveSheet.Columns("A:Z").AutoFit
Range("A1").Activate


Columns("A:A").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ColumnWidth = 120
Selection.Rows.AutoFit
ActiveSheet.Columns("A:Z").AutoFit
Range("A1").Select

ActiveWorkbook.Save

'Message Box when tasks are completed
MsgBox "Task Complete!"

End Sub

 

 

Anonymous
Not applicable

Actually, Power query was designed to do special transormations. My expectation is that you would be able to perform the same transformation in about 10 lines of code at most.

hi @Anonymous ,  so what is the solution here ? can you share the code ?

 

Thanks,

Anonymous
Not applicable

To build a query, a person look at the source of the data and the required output to build a transformation. No one (to my knowledge) can look at vba code and build a query. I can only tell that the macro is opening several excel workbooks, picking certain fields and then copy pasting the data into a destination workbook. This process is pretty simple by selecting the From Folder option in Power query.

 

P1.PNG

 

You would then need to modify the resulting sample query to pull in only the fields that you need and load to the Data model. All of this is quite simple to build if you have access to the data sources and are at least an intermediate Power Query user. If you are system saavy and want to learn the tool, then you could try it out. If not, you should probably hire someone to setup the query and the architecture.

 

P2.PNG

 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors