Forum Discussion
Power Query (M) help with creation of priority function
- 8 years ago
It is rather confusing that you post your code with the name of the function as if it is part of the code.
Edit: also confusing is the title of this topic, Power Query has no priority function.
Having said that, if I understand you correctly, you want to get the name of the column that was provided when invoking the function.
One solution is to search the value in the values of your table row and return the first column name where the value is found.
Example query and function:
let Source = #table(2,{{1,2},{3,4}}), #"Invoked Custom Function" = Table.AddColumn(Source, "ColumnName", each ColumnName(_, [Column1])) in #"Invoked Custom Function"let Source = (TableRow as record, FieldValue as any) as text => Record.FieldNames(TableRow){List.PositionOf(Record.FieldValues(TableRow),FieldValue,Occurrence.First)} in SourceThe only way to be absolutely certain (also with duplicate values) is to add the column name as metadata to your table values.
Example query and function:
let Source = #table(2,{{1,2},{3,4}}), MetaData = List.Accumulate(Table.ColumnNames(Source),Source, (t,c) => Table.TransformColumns(t, {{c, each _ meta [Column = c]}})), #"Invoked Custom Function" = Table.AddColumn(MetaData, "GetColumn", each GetColumn([Column2])) in #"Invoked Custom Function"let Source = (ValueWithMeta as any) => Value.Metadata(ValueWithMeta)[Column] in Source
It is rather confusing that you post your code with the name of the function as if it is part of the code.
Edit: also confusing is the title of this topic, Power Query has no priority function.
Having said that, if I understand you correctly, you want to get the name of the column that was provided when invoking the function.
One solution is to search the value in the values of your table row and return the first column name where the value is found.
Example query and function:
let
Source = #table(2,{{1,2},{3,4}}),
#"Invoked Custom Function" = Table.AddColumn(Source, "ColumnName", each ColumnName(_, [Column1]))
in
#"Invoked Custom Function"let
Source = (TableRow as record, FieldValue as any) as text => Record.FieldNames(TableRow){List.PositionOf(Record.FieldValues(TableRow),FieldValue,Occurrence.First)}
in
Source
The only way to be absolutely certain (also with duplicate values) is to add the column name as metadata to your table values.
Example query and function:
let
Source = #table(2,{{1,2},{3,4}}),
MetaData = List.Accumulate(Table.ColumnNames(Source),Source, (t,c) => Table.TransformColumns(t, {{c, each _ meta [Column = c]}})),
#"Invoked Custom Function" = Table.AddColumn(MetaData, "GetColumn", each GetColumn([Column2]))
in
#"Invoked Custom Function"let
Source = (ValueWithMeta as any) => Value.Metadata(ValueWithMeta)[Column]
in
Source
- Anonymous8 years agoNot applicable
Thanks. I was not aware that you can add metadata. That did the job.