电源查询功能可选参数

gre*_*egn 5 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
Run Code Online (Sandbox Code Playgroud)

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

 = fnDateToFileNameString("XXXXXXXXXXXXXXXXXX", #date(2015, 3, 21), null)
Run Code Online (Sandbox Code Playgroud)

抛出:

"An error occurred in the fnDateToFileNameString" query. Expression.Error: we cannot convert the value null to type Text.
    Details:
    Value=
    Type=Type
Run Code Online (Sandbox Code Playgroud)

Ale*_*SFT 6

问题出在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
Run Code Online (Sandbox Code Playgroud)