Reference a sheet in my workbook as my power query data source

Martin sherk

Board Regular
Joined
Sep 11, 2022
Messages
94
Office Version
  1. 365
  2. 2016
After a long search, I found the below M code to reference data in a sheet and use it as a source for my query, Data is found in sheet 1 in the same workbook that contains my queries and data is a simple XLS report exported from SAP. the reason i don't use table, that when people use the sheet they may paste the SAP exported data in the sheet without using table.

please let me know if it is reliable and won't cause errors.

also how to change the below first line to let it use my first sheet in the current workbook as a source instead of a workbook from a folder path as I don't need that.

Power Query:
let Source = Excel.Workbook(File.Contents("C:...\Downloads\Test.xlsx"), null, true), Sheet1_Sheet = Source{[Item="SAP",Kind="Sheet"]}[Data],

fTrimTable = (tbl as table, header as text) =>
    let
        t = Table.Buffer(tbl),
        columns = List.Buffer(Table.ColumnNames(t)),
        rowsToCheck = 100,
        Column = List.Select(columns, each List.PositionOf(List.FirstN(Table.Column(t, _),rowsToCheck), header)>0){0},
        Row = List.PositionOf(Table.Column(t, Column), header),
        ScrollRows = Table.RemoveFirstN (t, Row),
        ScrollColumns = Table.SelectColumns(ScrollRows, List.RemoveFirstN(columns, List.PositionOf(columns, Column))),
        #"Promoted Headers" = Table.PromoteHeaders(ScrollColumns, [PromoteAllScalars=true])
    in
        #"Promoted Headers",

Trimmed = fTrimTable(Sheet1_Sheet, "Header100")
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,723
Messages
6,174,113
Members
452,545
Latest member
boybenqn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top