Martin sherk
Board Regular
- Joined
- Sep 11, 2022
- Messages
- 94
- Office Version
- 365
- 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.
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")