0
votes

In Excel Power Query, in the M language: How do I pass a column name as a parameter to a function? As a (contrived) example, supposed I have a table:

Fish    Fowl
1   
2       1
        1
2   

I want a function which will take the table and one of the column-names, and return the sum of that column. I tried this implementation

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    SumType = (Tbl, ColumnName) =>
         List.Sum(Tbl[ColumnName]),
    FowlSum = SumType(ChType, "Fowl")
in
    FowlSum

But it fails with an error: "The column 'ColumnName' wasn't found."

So how can I pass a column name (as a string) to a function, and then access that column within the functions?

1

1 Answers

1
votes

Use Table.Column. In your case the line would look like List.Sum(Table.Column(Tbl, ColumnName)).