Skip to content

Commit

Permalink
Create Custom Functions\Part 1 - Using Optional Parameters.pq
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey authored Jun 5, 2024
1 parent 6fbf842 commit 233fc4d
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 233fc4d

Please sign in to comment.