Skip to content

Commit 233fc4d

Browse files
authored
Create Custom Functions\Part 1 - Using Optional Parameters.pq
1 parent 6fbf842 commit 233fc4d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
let
2+
/*
3+
About: Compare different optional parameter types
4+
This is from the post: https://ninmonkeys.com/blog/2024/06/05/power-query-functions-part1-using-optional-parameters
5+
6+
lets call Text.Combine() to test declaring optional parameters
7+
8+
9+
10+
For this version:
11+
- you can pass a null value for a separator
12+
- always requires you pass 2 parameters
13+
*/
14+
Join_Nullable = (texts as list, separator as nullable text) =>
15+
Text.Combine( texts, separator ),
16+
17+
/*
18+
For this version:
19+
- you can pass a null value for a separator
20+
- you can skip the second parameter
21+
- 'optional' parameters are automatically 'nullable',
22+
so you can drop the 'nullable' part
23+
24+
This is how library functions have multiple call signatures
25+
Power Query defines one function
26+
27+
Other languages let you define multiple functions with shared name
28+
Based on the argument types, it'll call a different overloaded function
29+
*/
30+
Join_Optional = (texts as list, optional separator as text) =>
31+
Text.Combine( texts, separator ),
32+
33+
Summary = [
34+
chars = { "a".."h" }, // example array of strings
35+
36+
// this version lets you pass an explicit null value
37+
Nullable_1 = Join_Nullable( chars, ", " ),
38+
Nullable_2 = Join_Nullable( chars, null ),
39+
40+
// but it requires you to pass something. It doesn't let you omit a parameter
41+
Nullable_3 = Join_Nullable( chars ),
42+
43+
// this version lets you pass an explicit null value
44+
// or drop the parameter completely
45+
Join_Optional_1 = Join_Optional( chars, ", " ),
46+
Join_Optional_2 = Join_Optional( chars, null ),
47+
Join_Optional_3 = Join_Optional( chars )
48+
]
49+
in Summary

0 commit comments

Comments
 (0)