diff --git "a/Examples-Blog/Custom Functions\\Part 1 - Using Optional Parameters.pq" "b/Examples-Blog/Custom Functions\\Part 1 - Using Optional Parameters.pq" new file mode 100644 index 0000000..aeb7828 --- /dev/null +++ "b/Examples-Blog/Custom Functions\\Part 1 - Using Optional Parameters.pq" @@ -0,0 +1,49 @@ +let + /* + About: Compare different optional parameter types + This is from the post: https://ninmonkeys.com/blog/2024/06/05/power-query-functions-part1-using-optional-parameters + + lets call Text.Combine() to test declaring optional parameters + + + + For this version: + - you can pass a null value for a separator + - always requires you pass 2 parameters + */ + Join_Nullable = (texts as list, separator as nullable text) => + Text.Combine( texts, separator ), + + /* + For this version: + - you can pass a null value for a separator + - you can skip the second parameter + - 'optional' parameters are automatically 'nullable', + so you can drop the 'nullable' part + + This is how library functions have multiple call signatures + Power Query defines one function + + Other languages let you define multiple functions with shared name + Based on the argument types, it'll call a different overloaded function + */ + Join_Optional = (texts as list, optional separator as text) => + Text.Combine( texts, separator ), + + Summary = [ + chars = { "a".."h" }, // example array of strings + + // this version lets you pass an explicit null value + Nullable_1 = Join_Nullable( chars, ", " ), + Nullable_2 = Join_Nullable( chars, null ), + + // but it requires you to pass something. It doesn't let you omit a parameter + Nullable_3 = Join_Nullable( chars ), + + // this version lets you pass an explicit null value + // or drop the parameter completely + Join_Optional_1 = Join_Optional( chars, ", " ), + Join_Optional_2 = Join_Optional( chars, null ), + Join_Optional_3 = Join_Optional( chars ) + ] +in Summary