Forum Discussion
Expression.Error: There weren't enough elements in the enumeration to complete the operation
- 10 months ago
Hi Deevo_
Let's try to restructure your approach to handle missing files, first create this helper and call it: getFiles
(substring as text) as table => let Source = Folder.Files("\\internal\Datafiles\Quarters\"), KeepRootOnly = Table.SelectRows(Source, each [Folder Path] = "\\internal\Datafiles\Quarters\"), KeepQuarter = Table.SelectRows(KeepRootOnly, each Text.Contains([Name], substring, Comparer.OrdinalIgnoreCase)), Latest = Table.FirstN(Table.Sort(KeepQuarter, {{"Date modified", Order.Descending}}), 1), NoHidden = Table.SelectRows(Latest, each [Attributes]?[Hidden]? <> true), Invoked = Table.AddColumn(NoHidden, "Transform File", each #"Transform File"([Content])), Renamed = Table.RenameColumns(Invoked, {"Name", "Source.Name"}), KeptCols = Table.SelectColumns(Renamed, {"Source.Name", "Date modified", "Transform File"}) in if Table.IsEmpty(KeepQuarter) then error "no data" else KeptColsNext use a base query to invoke it and test file availability before the append.
let Source = Table.FromColumns( {{"Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"}}, type table [Substring = text] ), Invoke = Table.AddColumn(Source, "Invoked", each getFiles([Substring])), NoErrors = Table.RemoveColumns(Table.RemoveRowsWithErrors(Invoke, {"Invoked"}), {"Substring"}), Expand1 = Table.ExpandTableColumn(NoErrors, "Invoked", {"Source.Name", "Date modified", "Transform File"}), Expand2 = Table.ExpandTableColumn(Expand1, "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))), Typed = Table.TransformColumnTypes(Expand2,{{"Source.Name", type text}, {"Date modified", type date}, {"Date", type date}, {"Product", type text}, {"Costs", type number}}) in TypedIt might need a little tweek but I trust it will get you there...
If you run into problems, report back. Cheers. - 10 months ago
Good observation and no worries Deevo_
Give this updated getFiles function a go.(folderPath as text, substring as text) as table => let Source = Folder.Files(folderPath), KeepRootOnly = Table.SelectRows(Source, each [Folder Path] = folderPath), KeepQuarter = Table.SelectRows(KeepRootOnly, each Text.Contains([Name], substring, Comparer.OrdinalIgnoreCase)), Latest = Table.FirstN(Table.Sort(KeepQuarter, {{"Date modified", Order.Descending}}), 1), NoHidden = Table.SelectRows(Latest, each [Attributes]?[Hidden]? <> true), Invoked = Table.AddColumn(NoHidden, "Transform File", each Excel.Workbook([Content], true, true){[Item="Data",Kind="Sheet"]}?[Data]?), Renamed = Table.RenameColumns(Invoked, {"Name", "Source.Name"}), KeptCols = Table.SelectColumns(Renamed, {"Source.Name", "Date modified", "Transform File"}) in if Table.IsEmpty(KeepQuarter) then error "no data" else KeptColsHere's the Base query as well
let Source = Table.FromColumns( {{"Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"}}, type table [Substring = text] ), Invoke = Table.AddColumn(Source, "Invoked", each getFiles(folderPath, [Substring])), NoErrors = Table.RemoveColumns(Table.RemoveRowsWithErrors(Invoke, {"Invoked"}), {"Substring"}), Expand1 = Table.ExpandTableColumn(NoErrors, "Invoked", {"Source.Name", "Date modified", "Transform File"}), Expand2 = Table.ExpandTableColumn(Expand1, "Transform File", {"Date", "Product", "Costs"} ), Typed = Table.TransformColumnTypes(Expand2,{{"Source.Name", type text}, {"Date modified", type date}, {"Date", type date}, {"Product", type text}, {"Costs", type number}}) in TypedKeep in mind that you'll need to update the folderPath in the Invoke step of the Base query.
Invoke = Table.AddColumn(Source, "Invoked", each getFiles(folderPath, [Substring])),
Good observation and no worries Deevo_
Give this updated getFiles function a go.
(folderPath as text, substring as text) as table =>
let
Source = Folder.Files(folderPath),
KeepRootOnly = Table.SelectRows(Source, each [Folder Path] = folderPath),
KeepQuarter = Table.SelectRows(KeepRootOnly, each Text.Contains([Name], substring, Comparer.OrdinalIgnoreCase)),
Latest = Table.FirstN(Table.Sort(KeepQuarter, {{"Date modified", Order.Descending}}), 1),
NoHidden = Table.SelectRows(Latest, each [Attributes]?[Hidden]? <> true),
Invoked = Table.AddColumn(NoHidden, "Transform File", each Excel.Workbook([Content], true, true){[Item="Data",Kind="Sheet"]}?[Data]?),
Renamed = Table.RenameColumns(Invoked, {"Name", "Source.Name"}),
KeptCols = Table.SelectColumns(Renamed, {"Source.Name", "Date modified", "Transform File"})
in
if Table.IsEmpty(KeepQuarter) then error "no data" else KeptCols
Here's the Base query as well
let
Source = Table.FromColumns(
{{"Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"}},
type table [Substring = text]
),
Invoke = Table.AddColumn(Source, "Invoked", each getFiles(folderPath, [Substring])),
NoErrors = Table.RemoveColumns(Table.RemoveRowsWithErrors(Invoke, {"Invoked"}), {"Substring"}),
Expand1 = Table.ExpandTableColumn(NoErrors, "Invoked", {"Source.Name", "Date modified", "Transform File"}),
Expand2 = Table.ExpandTableColumn(Expand1, "Transform File", {"Date", "Product", "Costs"} ),
Typed = Table.TransformColumnTypes(Expand2,{{"Source.Name", type text}, {"Date modified", type date}, {"Date", type date}, {"Product", type text}, {"Costs", type number}})
in
Typed
Keep in mind that you'll need to update the folderPath in the Invoke step of the Base query.
Invoke = Table.AddColumn(Source, "Invoked", each getFiles(folderPath, [Substring])),
m_dekorte The updated code is now working as expected. I had to make some tweaks to get it to work. Original results against the new results are correct. I can now delete the Helper queries!
I hope this can help others achieve a similar outcome. Thank you so much for your time.
**Update**
FYI only, I published this to the PowerBI Report Server and setup a scheduled refresh it it fails with the error "[Unable to combine data]......Please rebuild this data combination".
How I fixed this: I combined the "getFiles" and "Base Query" into one query and I referred to this post: Solved: [unable to combine data] Please rebuild this data ... - Microsoft Fabric Community
See below query where i added comments:
/*Added "let getFiles = " to the beginning*/
let
getFiles = (folderPath as text, substring as text) as table =>
let
Source = Folder.Files(folderPath),
KeepRootOnly = Table.SelectRows(Source, each [Folder Path] = folderPath),
KeepQuarter = Table.SelectRows(KeepRootOnly, each Text.Contains([Name], substring, Comparer.OrdinalIgnoreCase)),
Latest = Table.FirstN(Table.Sort(KeepQuarter, {{"Date modified", Order.Descending}}), 1),
NoHidden = Table.SelectRows(Latest, each [Attributes]?[Hidden]? <> true),
Invoked = Table.AddColumn(NoHidden, "Transform File", each Excel.Workbook([Content], true, true){[Item="Data",Kind="Sheet"]}?[Data]?),
Renamed = Table.RenameColumns(Invoked, {"Name", "Source.Name"}),
KeptCols = Table.SelectColumns(Renamed, {"Source.Name", "Date modified", "Transform File"})
in
if Table.IsEmpty(KeepQuarter) then error "no data" else KeptCols, /*Added a comma to the end*/
/*Removed the "let" from here*/
Source = Table.FromColumns(
{{"Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"}},
type table [Substring = text]
),
Invoke = Table.AddColumn(Source, "Invoked", each getFiles(folderPath, [Substring])),
NoErrors = Table.RemoveColumns(Table.RemoveRowsWithErrors(Invoke, {"Invoked"}), {"Substring"}),
Expand1 = Table.ExpandTableColumn(NoErrors, "Invoked", {"Source.Name", "Date modified", "Transform File"}),
Expand2 = Table.ExpandTableColumn(Expand1, "Transform File", {"Date", "Product", "Costs"} ),
Typed = Table.TransformColumnTypes(Expand2,{{"Source.Name", type text}, {"Date modified", type date}, {"Date", type date}, {"Product", type text}, {"Costs", type number}})
in
Typed