PQ - Declaring Data Type in Defined Data Table

johnny51981

Active Member
Joined
Jun 8, 2015
Messages
366
I use Defined Data Tables in my Power Query Excel files so that I can updated the column headers to different names (they aren't uniform across sources). Like so:
1705431335706.png


I bring it into Power Query Editor, M Code Magic, wham bam, I have consistent column header names. Wonderful.

My ask: is it possible to declare the Data Types that will be used in the Power Query from this same table? And if so, how would the M Code be written to apply these types in the Changed Type step?
The updated table would look something like this:
1705431607190.png
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,
I'd say it is possible.

To changes data types, the signature in M is a list of list, where the inner list are {"column name", type}.
You can create this list of inner lists with List.Zip({TableNameOfDefinitionTable[ColColNames, TableNameOfDefinitionTable[ColColTypes]}).
 
Upvote 0
Hi johnny51981

Hi,
I'd say it is possible.

To changes data types, the signature in M is a list of list, where the inner list are {"column name", type}.
You can create this list of inner lists with List.Zip({TableNameOfDefinitionTable[ColColNames, TableNameOfDefinitionTable[ColColTypes]}).
So I tried my own dog food and I'm failing. If I find some time today, I might give it another go. Duty calling now though...
 
Upvote 0
I did it slightly different then in the video, without needing to create an example, still I feel there could be a shorter way.
The code below is on some data I used to understand some of the M-code and how it behaves and to test the technique mentioned by @johnny51981.

My sample data table
Col1Col2Col3Col4Col5Col6Col7
CON-548712,517/01/202310:245FALSE
CON-87458,455/09/202315:394TRUE
CON-26580,2920/12/20230:573FALSE
CON-357180,736/01/202421:024FALSEWork in progress


My definition table
Orginal ColumnNew columnType
Col1Order Referencetext
Col2Amountnumber
Col3Date of Purchasedate
Col4Time Order Intime
Col5Number of Itemsinteger
Col6Returnedboolean
Col7Free commenttext
Order NumberOrder Referencetext
AmountAmountnumber
TotalAmountnumber


Power Query to define the data type
Power Query:
let
    Source = tbl_definition,
    Define_types = Table.AddColumn(Source, "TypeCol", each if [Type] = "text" then Type.ListItem(type {text})
else if [Type] = "number" then Type.ListItem(type {number})
else if [Type] = "integer" then Type.ListItem(type {Int64.Type})
else if [Type] = "date" then Type.ListItem(type {date})
else if [Type] = "time" then Type.ListItem(type {time})
else if [Type] = "datetime" then Type.ListItem(type {datetime})
else if [Type] = "duration" then Type.ListItem(type {duration})
else if [Type] = "boolean" then Type.ListItem(type {logical})
else Type.ListItem(type {any})),
    Keep_col_name_with_type = Table.RemoveColumns(Define_types,{"Orginal Column", "Type"}),
    Create_list_of_lists = Table.ToRows(Keep_col_name_with_type),
    remove_duplicates = List.Distinct(Create_list_of_lists)
in
    remove_duplicates

Power Query to apply the definition table on the data
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="raw"]}[Content],
    Rename_with_query_renames = Table.RenameColumns(Source,Renames, MissingField.Ignore),
    Set_types_with_query_typing = Table.TransformColumnTypes(Rename_with_query_renames, Typing)
in
    Set_types_with_query_typing
 
Upvote 0
Could you not use something like:

Code:
Expression.Evaluate("type " & [Type])
 
Upvote 0
Tried that before and it failed.
I must be doing something wrong with Expression.Evaluate. don't see what.
 
Upvote 0
It wouldn't work with integer of course (since you need Int64.type not 'type integer') but should do with others.
 
Upvote 0
Something like:

Code:
Table.AddColumn(Source, "TypeCol", each try Expression.Evaluate("type " & [Type]) otherwise if [Type] = "integer" then Int64.Type else Logical.Type)
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top