Forum Discussion

jaryszek's avatar
jaryszek
Super User
1 year ago
Solved

Importing table with predefined data type from metadata

Hi Guys, I do not want Power Query to automatically detect data changes.  If it is possible to read column data types from medata table like: Table1, Column1, String Table1, Column2, Integer ...
  • jaryszek's avatar
    1 year ago

    Solution code:

    let
        types = [Text = Text.From, #"Decimal number" = Number.From, #"Whole number" = Int64.From],
        type_table = Table.Group(
            Excel.CurrentWorkbook(){[Name="ColumnDataTypes"]}[Content], 
            "TableName", 
            {
                "transform", 
                (x) => List.Zip(
                    {
                        x[ColumnName], 
                        List.Transform(x[ColumnDataType], (w) => Record.FieldOrDefault(types, w, each _))
                    }
                )
            }
        ),
        type_record = Function.Invoke(Record.FromList, List.Reverse(Table.ToColumns(type_table))),
        result = ((table_name) => Table.TransformColumns(
            Record.Field(#shared, table_name), 
            Record.Field(type_record, table_name), 
            null, 
            MissingField.Ignore
        )) ("PetTable")
    in
        result

     

    approach taken from https://www.excelforum.com/office-365/1433993-power-query-to-import-column-data-types-from-table.html 

    Best,
    Jacek