Please see attached:
- Custom_Content_table_expand.pbix (33.7 KB)
- Company_1.xlsx (16.2 KB)
- Company_2.xlsx (16.3 KB)
- Test.xlsx (18.6 KB)
I’m expanding a list of tables. I only want to expand the table if it contains the correct columns. I’m trying to make this dynamic because Ill be grouping lots of different tables by name. Below is process:
- get all files from a folder
- Inspect each file for specific table names eg. begins with “Company_tbl”
- Keep or add table to list if true.
- Append all tables in the list which meet conditions 2 and 3 above.
How can I iterate over tables using List.Accumulate and keep based on condition?
Here is my current transforms. Note the List.Accumulate function in Company Transform is broken
Company Transform
let
Source = Folder.Files(f_directory),
headers = {"PersonNames", "People", "Location"},
#"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".xlsx")),
Filter_hidden_files = Table.SelectRows(#"Filtered Rows", each [Attributes]?[Hidden]? <> true),
transform = Table.AddColumn(
Filter_hidden_files, "Transform File",
each t_transform([Content], "Company_tbl01", {}, [Name], [Folder Path])),
expand = Table.ExpandTableColumn(transform, "Transform File", {"Data", "path"}, {"Data", "path"}),
// filter_tables = Table.SelectRows(expand, each [Data]))
_empty_tbl = #table({"PersonNames","People","Location"}, {}),
// BROKEN: List.Accumulate function doesn't work
tbl_filter = List.Accumulate(
[Data], _empty_tbl, (state, current) =>
if Col_check(state, headers)
then Table.Combine(state,current)
else state)
in
tbl_filter
t_transform
let transform_file = (
bContent as binary,
tbltitle as text,
headers as list,
filename as text,
folderpath as text
) =>
let
headers = null, // TODO dynamic headers list e.g. headers = {"PersonNames", "People", "Location"}. Use this to check through tables
filepath = folderpath&filename,
Source = Excel.Workbook(bContent, headers, true),
Title = Source{[Item=tbltitle,Kind="Table"]}[Data],
Add_path = Table.AddColumn(Source, "path", each filepath as text)
in
Add_path
in
transform_file
Any
Column checker to check if table contains headers
let
// checks table columns against given headers (list of col names)
Column_check_dyno = (tbl as table, headers as list) =>
let
col_names = Table.ColumnNames(tbl),
keep_cols = List.Select(col_names,each List.Contains(col_names, headers)),
select_headersonly = Table.SelectColumns(tbl, keep_cols)
in
select_headersonly
in
Column_check_dyno