电源查询功能可选参数

时间:2015-06-18 11:30:13

标签: excel powerquery

如何使用可选参数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

我将它传递给看起来像的测试:

 = 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

1 个答案:

答案 0 :(得分:5)

问题出现在Text.Replace中,因为第二个参数不能为null:替换文本值中的字符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