Search code examples
powerquerym

Better syntax to declare data types in M - Text.Type or type text?


For M in Power Query/ PowerBI, which syntax should I be using to set data types?
A) type text (or type logical, type date, etc.)
B) Text.Type ( or Logical.Type, Date.Type, etc.)

Now that option B exists, is there any reason to ever use the option A syntax? I tried reading chapter 5 of the Power Query M language specification, but I couldn't find a clear answer.

Here is one example using Table.AddColumn (though data types show up everywhere):

let
   OldTable = #table({"Col1"},{{"This column"}}),
   fMyFunc = (paramText as text ) as text => let returnText = paramText & "_new" in returnText,
   NewTable = Table.AddColumn(OldTable, "NewCol", each "Sample", Text.Type),
   NewerTable = Table.AddColumn(NewTable, "NewerCol", each fMyFunc([NewCol]), Text.Type)
in
   NewerTable

I believe Option B was introduced just to standardize the type definitions; for example, there was Int64.Type but not type Int64. Thus, the answer to my question could be "It doesn't matter at all." But, if one option seems to be the consensus for the future, I'd rather start now with being consistent in my code.


Solution

  • I'd agree that it doesn't matter except from a stylistic perspective.

    As you mentioned, non-primitive types like Int64.Type can't be written as type Int64 like you can with type text, so if you want to keep your style consistent between primitive and non-primitive types, then you want option B.


    The primitive types listed on page 48 and 49 of the document you linked are:

    • type null, which classifies the null value
    • type logical, which classifies the values true and false
    • type number, which classifies number values
    • type time, which classifies time values
    • type date, which classifies date values
    • type datetime, which classifies datetime values
    • type datetimezone, which classifies datetimezone values
    • type duration, which classifies duration values
    • type text, which classifies text values
    • type binary, which classifies binary values
    • type type, which classifies type values.
    • type list, which classifies list values
    • type record, which classifies record values
    • type table, which classifies table values
    • type function, which classifies function values
    • type anynonnull, which classifies all values excluding null