Forum Discussion
Power Query Underlying Problem
- 10 months ago
Hi Klik,
Please try the updated M-Query Below
let Source = SharePoint.Contents("https://miscbhd.sharepoint.com/sites/xxxx", [ApiVersion = 15]), Library = Source{[Name="Shared Documents"]}[Content], FilteredFiles = Table.SelectRows(Library, each Text.Contains([Name], "ENGINEERING") and Text.EndsWith([Name], ".xlsx")), SortedFiles = Table.Sort(FilteredFiles, {{"Date modified", Order.Descending}}), LatestFile = Table.FirstN(SortedFiles, 1), Binary = LatestFile{0}[Content], ExcelContent = Excel.Workbook(Binary, false), PossibleNames = {"Dashboard", "DASHBOARD"}, FilteredSheet = Table.SelectRows(ExcelContent, each List.Contains(PossibleNames, [Name])), Extracted = if Table.IsEmpty(FilteredSheet) then #table({}, {}) else FilteredSheet{0}[Data], #"Removed Top Rows" = Table.Skip(Extracted, 4), #"Removed Other Columns" = Table.SelectColumns(#"Removed Top Rows", { "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14" }), #"Promoted Headers" = Table.PromoteHeaders(#"Removed Other Columns", [PromoteAllScalars = true]), #"Removed Top Rows1" = Table.Skip(#"Promoted Headers", 1), #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Top Rows1", 2), #"Renamed Columns" = Table.RenameColumns(#"Removed Bottom Rows", { {"%#(lf)PLAN ", "PLAN"}, {"%#(lf)ACTUAL ", "ACTUAL"}, {"%#(lf)VAR. ", "VAR"}, {"%#(lf)BAL.", "BAL"}, {"TOTAL", "TOTAL"} }, MissingField.Ignore), #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns", { {"TOTAL", Int64.Type}, {"PLAN", Percentage.Type}, {"ACTUAL", Percentage.Type}, {"VAR", Percentage.Type}, {"BAL", Percentage.Type} }) in #"Changed Type"Thank you.
The error happens because Table.RemoveLastN (and similar row removal steps) fails in Power BI Service when the Excel sheet has fewer rows than expected after filtering/skipping. Preview works since your local file matches expectations, but the latest SharePoint file likely has different row count.​
Quick Fix
Replace the failing steps with safer versions:
This checks row count before removing.​​
Better Approach
Add Table.Buffer(Extracted) right after the Extracted step to lock data in memory and prevent folding issues during service refresh. Also verify the latest ENGINEERING.xlsx file on SharePoint has the expected "Dashboard"/"DASHBOARD" sheet with enough rows (>7 total after skips). Test by manually refreshing each step in Power Query Editor.​