try
我正在编写 Power Query M 脚本,以便根据特定条件重命名表中的列。但是,当我尝试在函数内使用表达式时,遇到了“需要标记文字”错误List.MatchesAny
。以下是我的代码的相关部分:
RenameColumnsFunction = (table as table) as table =>
let ColumnNames = Table.ColumnNames(table),
//Apply transformation logic to each column name
RenamedColumns = List.Transform(ColumnNames, (colName) =>
// If any column values have a length of 4 and are numeric, rename the column to "Org level 2 Code"
if
List.MatchesAny(Table.Column(table, colName), each try
Text.Length(Text.From(_)) = 4 and **try **
Number.FromText(Text.From(_)) <> null otherwise false)
then
{colName, "Org Level 2 Code"}
// If the column name matches one of the predefined names, rename it to "Headcount Label"
else if
List.Contains({"ACCTG", "ADMIN", "ASM", "AWE", "CFO", "CROCOL", "CUSTSRV", "DEV", "ENGIN","FIN","FLDSP", "HOM", "HR","IT", "MERCH", "PROSVC"}, colName)
then
{colName, "Headcount Label"}
else {colName, colName} // Keep the original name if no match
),
RenamedTable = Table.RenameColumns(table, RenamedColumns, MissingField.Ignore)
in RenamedTable,
// Step 4: Apply the renaming function to each table query
ApplyRename = Table.AddColumn(FilteredQueries, "Transformed Data", each RenameColumnsFunction([Content])),
// Step 5: Expand the renamed tables (ensure there's data to extract)
RenamedTables = Table.ExpandTableColumn(ApplyRename, "Transformed Data", Table.ColumnNames(ApplyRename[Content]{0})),
// Step 6: Combine all queries into one table if needed
CombinedData = Table.Combine(RenamedTables[TransformedData])
in
CombinedData
重命名表中的列的函数
并使用它