{"id":2836,"date":"2024-06-04T13:35:26","date_gmt":"2024-06-04T18:35:26","guid":{"rendered":"http:\/\/ninmonkeys.com\/blog\/?p=2836"},"modified":"2024-06-04T13:35:27","modified_gmt":"2024-06-04T18:35:27","slug":"what-not-to-do-in-power-query","status":"publish","type":"post","link":"https:\/\/ninmonkeys.com\/blog\/2024\/06\/04\/what-not-to-do-in-power-query\/","title":{"rendered":"What Not To Do: When Letting Your Code Breathe Goes Bad"},"content":{"rendered":"\n\n\n<h2>White Space before a function call is allowed<\/h2>\n\n\n\n<p>Whitespace between function calls and the name are allowed.  <strong>Including newlines<\/strong><br>These are equivalent statements:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">= DoStuff( args )\n\n\/\/ and\n= DoStuff\n\n                                ( \nargs )<\/code><\/pre>\n\n\n\n<p>Record lookups also allow whitespace. <br>This is totally valid syntax wise. Not necessarily morally though.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">let\n    Func = () =&gt; [ \n        user = [ Name = &quot;bob&quot; ] \n    ]\nin \n    Func  \n\n\n(\n\n              )\n                            \n                                             [    \n     user  \n\n\n\n]<\/code><\/pre>\n\n\n\n<h2>Misleading Comments inside Lists, Records, and Function Calls are allowed<\/h2>\n\n\n\n<p>Comments do not affect parsing or execution.<br>Without syntax highlighting it looks like 4 is the final item in the list<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"\" data-line=\"\">let\n    num = List.Count( { 0\n                     ..\n       5 } &amp; { 3, 4                                                  \/*\n    }) \/* fake ending here  here * \/\n                \n    without syntax highlighting, it looks like the func call ended\n    \n    later, secretly do more\n*\/                                                  ,99 } )\n\nin num<\/code><\/pre>\n\n\n\n<p>Now it&#8217;s slightly easier<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">let\n    num = List.Count( { 0\n                     ..\n       5 } &amp; { 3, 4                                                  \/*\n    }) \/* fake ending here  here * \/\n                \n    without syntax highlighting, it looks like the func call ended\n    \n    later, secretly do more\n*\/                                                  ,99 } )\n\nin num<\/code><\/pre>\n\n\n\n<h2>The list operator <code class=\"\" data-line=\"\">..<\/code> allows a lot of expressions<\/h2>\n\n\n\n<ul><li>See: <a href=\"https:\/\/learn.microsoft.com\/en-us\/powerquery-m\/m-spec-consolidated-grammar#list-expression\">powerquery-m\/m-spec-consolidated-grammar#list-expression<\/a><\/li><\/ul>\n\n\n\n<p>You can use inline comments, resuming the list expression later<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">let\n    num = List.Count( { 0\n..\n           5 } &amp; { 3, 4 \/*\n    now do more *\/ } )\nin num<\/code><\/pre>\n\n\n\n<p> You don&#8217;t have to wrap the try-catch expressions in parenthesis. <\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">let l = { \n          try &quot;foo&quot; + 99 catch (e) =&gt; 3           ..\n          try File.Contents( &quot;invalid&quot; ) catch () =&gt; 7 } \nin \n    l = { 3..7 } \/\/ is true<\/code><\/pre>\n\n\n\n<p>This version gave an interesting error. I thought perhaps try-catch expressions doesn&#8217;t work for inline list indices ?<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powerquery\" data-line=\"\">let g = { \n          try 10 catch (e) =&gt; 3           ..\n          try 27 \/ 0 catch () =&gt; 53 }\n\nin g<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-csharp\" data-line=\"\">Expression.Error: The number is out of range of a 32 bit integer value.\nDetails:\n    Infinity<\/code><\/pre>\n\n\n\n<p>But then realized division by 0 in power query <strong>does not throw<\/strong> an error record. It has the type <strong>number<\/strong>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>White Space before a function call is allowed Whitespace between function calls and the name are allowed. Including newlinesThese are equivalent statements: Record lookups also allow whitespace. This is totally valid syntax wise. Not necessarily morally though. Misleading Comments inside Lists, Records, and Function Calls are allowed Comments do not affect parsing or execution.Without syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2848,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[150,72,50,1,158],"tags":[156,157,7,93],"_links":{"self":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/2836"}],"collection":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/comments?post=2836"}],"version-history":[{"count":17,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions\/2854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media\/2848"}],"wp:attachment":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media?parent=2836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/categories?post=2836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/tags?post=2836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}