7

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

1 回答 1

8

问题出在 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
于 2015-06-18T22:18:18.797 回答