简体   繁体   中英

Powerquery: table function with variable parameter list length

How to write table function with non fixed parameter list length ?

Particular simplified example : I want to write function trimupper(TableName,ColumnName1,ColumnName2,...) that combines just two steps for given set of columns:

  1. TRIM whitespaces
  2. UPPERCASE text

Example for two columns case:

(tbl as table, cn1 as text, cn2 as text) =>
let    
    #"Trimmed Text"    = Table.TransformColumns(tbl,{{cn1, Text.Trim , type text}, {cn2, Text.Trim , type text}}),
    #"Uppercased Text" = Table.TransformColumns(tbl,{{cn1, Text.Upper, type text}, {cn2, Text.Upper, type text}}),
    trimupperResult = #"Uppercased Text" 
in
    trimupperResult

But how to do it for variable number of ColumnNames?

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
changethem = transform (Source,{"ColumnName1","ColumnName2"})
in changethem

function transform

(Table as table, columnnames as list) =>
let columnnames = if columnnames = null then Table.ColumnNames(Table) else columnnames,
change = Table.TransformColumns( Table, List.Transform(columnnames, each {_, Text.Trim, type text} ) ),
change1 = Table.TransformColumns( change, List.Transform(columnnames, each {_, Text.Upper, type text} ) )
in change1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM