Forum Discussion
Thomas_123
1 year agoNew Member
Replace fixed column names in a function
Hello I try to replace fixed column names in a function to reuse the function in other queries. Unfortunately I can't get any further with two replacements. The function calculates an averag...
- 1 year ago
Hi Thomas_123
You can update those pieces of code using
Table.Column( ... , ValueColumn )
and
Record.Field ( row, YearColumn)
The full M code (reformatted slightly) would be:
let MyFunction = ( Table as table, NewColumn as text, YearColumn as text, ValueColumn as text, Years_back as number ) => Table.AddColumn( Table, NewColumn, (x) => List.Average( Table.Column( Table.SelectRows( Table, (row) => List.Contains( List.MaxN( List.Select(Table.Column(Table, YearColumn), (y) => y <= x{YearColumn}), Years_back ), Record.Field(row, YearColumn) ) ), ValueColumn ) ) ) in MyFunctionDoes this work for you?
OwenAuger
1 year agoSuper User
Hi Thomas_123
You can update those pieces of code using
Table.Column( ... , ValueColumn )
and
Record.Field ( row, YearColumn)
The full M code (reformatted slightly) would be:
let
MyFunction = ( Table as table, NewColumn as text, YearColumn as text, ValueColumn as text, Years_back as number ) =>
Table.AddColumn(
Table,
NewColumn,
(x) =>
List.Average(
Table.Column(
Table.SelectRows(
Table,
(row) =>
List.Contains(
List.MaxN(
List.Select(Table.Column(Table, YearColumn), (y) => y <= x{YearColumn}),
Years_back
),
Record.Field(row, YearColumn)
)
),
ValueColumn
)
)
)
in
MyFunction
Does this work for you?
- Thomas_1231 year agoNew Member
Hello Owen
Great! It is working. You saved my Sunday.