强大的查询函数可选参数
Power query function optional arguments
请问如何创建带有可选参数的强大查询函数?
我已经尝试了创建函数语法的各种排列,目前是这样的:
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
我将它传递给如下所示的测试:
= fnDateToFileNameString("XXXXXXXXXXXXXXXXXX", #date(2015, 3, 21), null)
抛出:
"An error occurred in the fnDateToFileNameString" query. Expression.Error: we cannot convert the value null to type Text.
Details:
Value=
Type=Type
问题出在 Text.Replace,因为第二个参数不能为空:替换文本值中的字符 null
没有意义。如果将 nullCheckedDateFormat 更改为以下内容,您的函数将起作用:
nullCheckedDateFormat = if inDateFormat = null then "" else inDateFormat,
这对下一步来说有点多余,所以你可以这样重写函数:
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
请问如何创建带有可选参数的强大查询函数? 我已经尝试了创建函数语法的各种排列,目前是这样的:
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
我将它传递给如下所示的测试:
= fnDateToFileNameString("XXXXXXXXXXXXXXXXXX", #date(2015, 3, 21), null)
抛出:
"An error occurred in the fnDateToFileNameString" query. Expression.Error: we cannot convert the value null to type Text.
Details:
Value=
Type=Type
问题出在 Text.Replace,因为第二个参数不能为空:替换文本值中的字符 null
没有意义。如果将 nullCheckedDateFormat 更改为以下内容,您的函数将起作用:
nullCheckedDateFormat = if inDateFormat = null then "" else inDateFormat,
这对下一步来说有点多余,所以你可以这样重写函数:
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