Estou trabalhando em um script do Power Query M para renomear colunas em uma tabela com base em certas condições. No entanto, estou encontrando um erro "Token Literal Expected" ao tentar usar a try
expressão dentro de uma List.MatchesAny
função. Aqui está a parte relevante do meu código:
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
Função para renomear colunas em uma tabela
E para usá-lo