Optional arguments to the power request function
How do I create a power request function with an additional pls argument? I've tried various permutations of creating the function syntax, currently like this:
let
fnDateToFileNameString=(inFileName as text, inDate as date, optional inDateFormat as nullable text) =>
let
nullCheckedDateFormat = Text.Replace(inDateFormat, null, ""),
value = if nullCheckedDateFormat = ""
then inFileName
else Text.Replace(inFileName, inDateFormat, Date.ToText(inDate, inDateFormat ))
in
value
in
fnDateToFileNameString
and I pass it to a test that looks like this:
= fnDateToFileNameString("XXXXXXXXXXXXXXXXXX", #date(2015, 3, 21), null)
What is throwing:
"An error occurred in the fnDateToFileNameString" query. Expression.Error: we cannot convert the value null to type Text.
Details:
Value=
Type=Type
+3
source to share
1 answer
The problem arises in Text.Replace, since the second argument cannot be null: there is no point in replacing the character with a null
text value. Your function will work if you change nullCheckedDateFormat to the following:
nullCheckedDateFormat = if inDateFormat = null then "" else inDateFormat,
This is a bit redundant with a subsequent step, so you can rewrite this function like this:
let
fnDateToFileNameString=(inFileName as text, inDate as date, optional inDateFormat as nullable text) =>
if inDateFormat = null or inDateFormat = ""
then inFileName
else Text.Replace(inFileName, inDateFormat, Date.ToText(inDate, inDateFormat ))
in
fnDateToFileNameString
+3
source to share