Forum Discussion
Dynamically Change Column Type based on Column Name/Type from another table
- 4 years ago
Hi KNP ,
funny, my understanding of Anonymous s request is just about a topic that I intended to blog about for some time (but too busy currently). Done that in some customer projects as well lately and it works really good. I find it especially useful when working with dataflows.
But I'm attaching the file here.
The transformations are done using this function:(TableToBeTransformed as table, TableName as text) => let FilterMatchingTableFromMapping = Table.SelectRows(ColumnMapping, each [Table] = TableName), TablesColumnNames = Table.FromColumns({Table.ColumnNames(TableToBeTransformed)}), FilterRelevantColumns = Table.NestedJoin( FilterMatchingTableFromMapping, {"old"}, TablesColumnNames, {"Column1"}, "TablesColumnNames", JoinKind.Inner ), RenameColumns = Table.RenameColumns( TableToBeTransformed, List.Transform(Table.ToRecords(FilterRelevantColumns), each {_[old], _[new]}) ), #"Changed Type" = Table.TransformColumnTypes( RenameColumns, List.Transform( Table.ToRecords(FilterRelevantColumns), each {_[new], fnReplaceTypes(_[Type])} ) ) in #"Changed Type"Hope this is what you've been looking for.
Thanks ImkeF - you always make that look so simple. I look forward to the blog.
Question - can you see a way to make the TableName dynamic? e.g.
| = fnTransformFromMappingTable( Source, "Table2" ) |
the "Table2" to populate based on the query that the function is invoked in.
Soheil's function uses
let
Source = #sections,
Section1 = Record.FieldNames(Source[Section1])
...
...
to identify if a table exists, I wonder, is it possible to use a variation of this (or something else) to identify the current query?