Created
February 28, 2025 14:28
-
-
Save m-dekorte/fbaa680e57f6b7a08f027d9ff66e5554 to your computer and use it in GitHub Desktop.
colRenamesAndTypes Example | manage renaming columns and setting column data types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| colTypes = [ | |
| #"any" = Any.Type, | |
| #"date" = Date.Type, | |
| #"time" = Time.Type, | |
| #"datetime" = DateTime.Type, | |
| #"logical" = Logical.Type, | |
| #"number" = Number.Type, | |
| #"currency" = Currency.Type, | |
| #"integer" = Int64.Type, | |
| #"percentage" = Percentage.Type, | |
| #"text" = Text.Type | |
| ], | |
| /* wsInputs can be replaced with an Excel table, loaded into Power Query */ | |
| wsInputs = Table.FromRows( | |
| { | |
| {"LastModified", "Last Modified Date", "date"}, | |
| {"Owner", "Owner", "text"}, | |
| {"NextReview", "Review Date", "date"}, | |
| {"WorkingTitle", "Working Title", "text"}, | |
| {"Revision", "Revision Number", "integer"}, | |
| {"Budget", "Budget Amount", "currency"}, | |
| {"Cost TD", "Cost to date Amount", "number"} | |
| }, | |
| type table [oldColName = text, newColName = text, colType = text] | |
| ), | |
| Source = Table.FromRows( | |
| { | |
| {#date(2024, 1, 21), "Melissa", #date(2025, 1, 21), "To be determined", 1, 1000, 156.89}, | |
| {#date(2011, 5, 2), "Sam", null, "Close encounters", 12, 500, 873.03} | |
| }, | |
| Table.Column(wsInputs, "oldColName") | |
| ), | |
| colNames = Table.ColumnNames(Source), | |
| ValidNames = Table.SelectRows(wsInputs, each List.Contains(colNames, [oldColName])), | |
| Renamed = Table.RenameColumns(Source, List.Zip({ValidNames[oldColName], ValidNames[newColName]})), | |
| Transform = Table.TransformColumnTypes( | |
| Renamed, | |
| List.Zip({ | |
| ValidNames[newColName], | |
| List.Transform(ValidNames[colType], each Record.FieldOrDefault(colTypes, _, Any.Type)) | |
| }) | |
| ) | |
| in | |
| Transform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment