【问题标题】:Power query function optional arguments电源查询函数可选参数
【发布时间】:2015-06-18 11:30:12
【问题描述】:

如何创建带有可选参数 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

【问题讨论】:

    标签: excel powerquery


    【解决方案1】:

    问题出在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2020-09-08
      • 1970-01-01
      相关资源
      最近更新 更多