PowerBi - Powerquery:如果我们从路径加载的文件夹/文件不存在,请更改路径

问题描述 投票:0回答:2

我正在使用参数从每个客户端文件夹加载文件。某些文件夹不包含 powerquery 加载所需的文件。因为这些文件可能存在也可能不存在。

如果找不到文件,我尝试使用:“尝试 - 否则”将路径更改为“标准文件夹”。

有什么解决办法吗?或者其他方法来解决它? 谢谢!

file error-handling path powerbi powerquery
2个回答
0
投票

以下查询将定义两个源,一个是目标,一个是默认源。如果目标没有返回任何文件,那么它将加载默认文件。

let
    DefaultSource = Folder.Files("C:\DefaultPath"),
    TargetSource = Folder.Files("C:\TargetPath"),
    FileCounter = Table.RowCount(Source),
    DoSomething = if FileCounter > 0 then TargetSource else DefaultSource
in
    DoSomething

如果有效请告诉我


0
投票

基于@Ricardo Diaz 的回答作为开始(谢谢里卡多!),我成功了:

  1. 创建空白查询
  2. 代码:
= let
    Path1 = "D:\OneDrive\",
    Path2 = "C:\Users\OneDrive\",
    DefaultSource = Folder.Files(Path1),
    AlternateSource = Folder.Files(Path2),
    FileCounter1 =   try Table.RowCount(DefaultSource) otherwise 0,
    FileCounter2 =   try Table.RowCount(AlternateSource) otherwise 0,
    WorkingPath = if FileCounter1 > FileCounter2 then Path1 else Path2
in
    WorkingPath
  1. 然后你就可以调用这个Query了。

希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.