Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powerquery

m

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.

like image 483
B D T Avatar asked Nov 04 '25 10:11

B D T


2 Answers

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
like image 152
Alexis Olson Avatar answered Nov 07 '25 14:11

Alexis Olson


As Type isn't strongly enforced (MS type doc) and that the lack of any 'style guide' from Microsoft, which makes this question a matter of opinion. I would use the primitive types (as described in the M language spec, chapter 5) unless you explicitly require a non primitive type like Int16.Type for your solution.

like image 35
Elliot Gross Avatar answered Nov 07 '25 14:11

Elliot Gross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!