-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Custom Functions\Part 1 - Using Optional Parameters.pq
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Examples-Blog/Custom Functions\Part 1 - Using Optional Parameters.pq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |