Excel powerquery函数可选参数

Excel powerquery函数可选参数,excel,powerquery,Excel,Powerquery,如何使用可选参数pls创建电源查询函数? 我尝试过创建函数语法的各种排列,目前如下所示: let fnDateToFileNameString=(inFileName as text, inDate as date, optional inDateFormat as nullable text) => let nullCheckedDateFormat = Text.Replace(inDateFormat, null, ""), val

如何使用可选参数pls创建电源查询函数? 我尝试过创建函数语法的各种排列,目前如下所示:

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
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
我通过了一个测试,看起来:

 = 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:在文本值中替换字符
null
没有意义。如果将nullCheckedDateFormat更改为以下格式,则函数将正常工作:
nullCheckedDateFormat=如果inDateFormat=null,则“else inDateFormat,

这对于下一步来说有点多余,因此您可以像这样重写函数:

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
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