如何为来自SharePoint的文件创建动态文件路径,以查询power查询?

发布于 2025-02-13 19:28:49 字数 1339 浏览 0 评论 0原文

上下文:我最近接受了一份新工作,并且正在培训我的替代品。我已经为该公司建立了许多报告,其中大多数已保存给我的OneDrive。我一直在将这些文件移至SharePoint网站上,学员可以使用,这需要我在报告中修复电源查询,以便当她刷新工作簿中的链接时,它可以从同一工作簿中的另一个工作簿中吸取数据SharePoint。

问题:我正在使用SharePoint中的“添加快捷方式”功能,因此我可以从文件资源管理器访问文件,因此,当我选择文件时,它会从我的用户名中构建路径,以防止她刷新她由于文件路径是无法访问的,因此该工作簿。

我采取的步骤:我读了这篇文章 https://excel.tv/how-to-to-to-create-a-relative-file-path-path-in-power-query/ ,我认为这使我朝着正确的方向朝着正确的方向从硬盘驱动器上的所有文件的角度来看,而不是来自SharePoint网站。当我在SharePoint上共享该公式中输入此公式时:

=LEFT(CELL(“filename”,$A$1),FIND(“[“,CELL(“filename”,$A$1),1)-1)

它没有在其中返回用户名返回filepath,而是返回https链接(例如 https://sharepoint.com/teams/sharedhub ),哪个)然后,Power查询会呼叫错误,因为它不是有效的绝对路径。另外,要参考,这是高级编辑器的代码块。

let
Filepath = Excel.CurrentWorkbook(){[Name="Filepath"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(Filepath & "The SharePoint File.xlsx"), null, true),
tbl_nexidia_Table = Source{[Item="tbl",Kind="Table"]}[Data]
in
    tbl_Table

我还尝试使用获取数据构建新查询 - >从文件 - >从SharePoint文件夹中,但这似乎可以从SharePoint中提取所有文件夹名称,我还没有弄清楚如何钻到特定的工作簿以从特定表中获取数据。

非常感谢您的帮助!

Context: I recently accepted a new job and I'm training my replacement. I've built a lot of reports for the company and a majority of them have been saved to my OneDrive. I've been moving these files over to a SharePoint site the trainee can use which has required me to fix the power queries in my reports so that when she refreshes the links in the workbook that it pulls in data from another workbook present on the same sharepoint.

Problem: I'm using the "Add shortcut to OneDrive" feature from SharePoint so I can access the files from my file explorer, so when I go to select the file it builds a path from my username, preventing her from being able to refresh the workbook since the file path is inaccessable.

Steps I've Taken: I read this article https://excel.tv/how-to-create-a-relative-file-path-in-power-query/ and I think this is taking me in the right direction except its from the perspective of all the files being located on your hard drive and not from a SharePoint site. When I enter in this formula into the file I'm sharing on the SharePoint:

=LEFT(CELL(“filename”,$A$1),FIND(“[“,CELL(“filename”,$A$1),1)-1)

Instead of returning the filepath with my username in it, it returns a https link (eg. https://sharepoint.com/teams/SharedHub), which power query then calls an error on since it's not a valid absolute path. Also, for reference, here's the code block from the advanced editor.

let
Filepath = Excel.CurrentWorkbook(){[Name="Filepath"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(Filepath & "The SharePoint File.xlsx"), null, true),
tbl_nexidia_Table = Source{[Item="tbl",Kind="Table"]}[Data]
in
    tbl_Table

I also tried building a new query using Get data --> From File --> From SharePoint Folder but that seems to pull in all the folder names from the SharePoint and I haven't figured out how I drill down to a specific workbook to get data from a specific table.

Would very much appreciate some help on this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2025-02-20 19:28:49

尝试一下。将Excel Workbook放入SharePoint之后,请从SharePoint内部打开工作簿,然后选择编辑下拉列表, 在桌面应用程序中打开

“在此处输入图像说明”

然后,在工作簿中,单击 file > info > 复制路径。 (这将为您提供电源查询所需的路径才能直接检索工作簿。如果您尝试使用SharePoint的功能复制路径的路径,这与您所获得的路径不同。)

“在此处输入图像说明”

“在此处输入图像说明”

粘贴该路径以安全地使用。然后关闭您的工作簿并打开电源查询。

在电源查询中,创建两个新参数:

单击 home > 管理参数的底部按钮> 新参数

”在此处输入图像描述

将名称从 parameter1 更改为路径并将类型更改为 text ,然后将您复制的工作簿复制的路径粘贴到当前值 box 。然后,从刚粘贴的路径中删除了最后一个前向斜线(/)之后的所有文本。确保您做< / strong>将最后 /包含在路径中。您将在单独的参数中放置以下内容的文件名,并且不需要?web = 1 < / em>。

不要单击确定。,请单击 new ,在管理参数窗口的顶部。这将设置您创建文件名参数。

将名称从 parameter1 更改为 fileName ,然后将 type 更改为 text ,然后粘贴您复制的文件名工作簿进入当前值框。同样,您不需要原始路径中的?web = 1

单击确定。

现在,您可以在源行中使用参数,路径和文件名。这是这样做的方法:

Source = Excel.Workbook(Web.Contents(path & filename), null, true)

这将为您提供这样的表:

”在此处输入图像描述”

------------------------------------------------------------------------ -

对于Mirza的后续操作,关于从SharePoint文件夹中提取文件,请尝试以下操作:

  1. 获取数据,选择SharePoint文件夹连接器,然后单击
    连接。
  2. 对于SharePoint文件夹站点URL,输入您的URL
    您的 root 在SharePoint中使用文件夹,然后单击“确定”。
  3. 您可能必须输入您的凭据。如果是这样,请这样做。
  4. 此时,您应该看到SharePoint中文件的列表。单击转换数据按钮。
  5. 在您接下来看到的表中:
    • 将名称列过滤到的名称,只有一个您的文件
      想打开。 (使用顶端的下拉箭头。)
    • 将文件夹路径列过滤到仅一个路径。 (同样命名
      文件可以存储在多个文件夹中,您需要确保
      您正在通过与正确的文件匹配正确的文件
      文件夹,这为此设定了阶段。)
  6. 文件夹,这为此设定了 但这一次:
    • 对于路径的当前值,请从您当前在表格的文件夹路径列中粘贴值;和
    • 对于文件名的当前值,请从您当前的表的名称列粘贴值。
  7. 然后,在“过滤行”的“公式”框中,将文件名(包括报价)更改为文件名,然后更改路径的名称(包括引号)到路径。

您应该以这样的代码结束(yourdomain.sharepoint.com/your/root/folder/url将是您的 root root 在SharePoint中的文件夹):

let
    Source = SharePoint.Files("https://yourdomain.sharepoint.com/your/root/folder/URL", [ApiVersion = 15]),
    #"Filtered Rows" = Table.SelectRows(Source, each (([Name] = filename and [Folder Path] = path)))
in
    #"Filtered Rows"

您应该看到类似的东西此:

“在此处输入图像说明”

在内容列中单击二进制文件。

------------------------------------------------------------------------ -

对于Mirza的第二次随访,关于从SharePoint文件夹中摘取多个文件而不是仅一个文件,请尝试以下操作:

  1. 在获取数据中,选择SharePoint文件夹连接器,然后单击
    连接。
  2. 对于SharePoint文件夹站点URL,输入您的URL
    在SharePoint中使用文件的root文件夹,然后单击“确定”。你可能有
    输入您的凭据。如果是这样,请这样做。
  3. 此时,您应该看到SharePoint中文件的列表。单击转换数据按钮。

  1. 您在表中看到的下一个:将文件夹路径列滤除到仅一个路径 -您想要的文件所在的文件夹的路径。 (请注意,这与我以前的答案中解决的单个文件不同,因为此文件通过名称列跳过过滤。)

”输入图像描述在这里“

”在此处输入图像描述”

  1. 接下来,设置一个称为路径的参数。对于路径的当前值,请从您当前使用的表路径列复制并粘贴值。
  • 要从文件夹路径列复制路径:

”图像描述在此处“

  • 粘贴您刚刚从文件夹路径列复制到的路径
    路径参数的当前值:

    单击“家”,然后单击“管理参数”,然后,如果您尚未创建路径参数,请单击新(否则单击路径参数以选择它):

单击“ .sstatic.net/vggHt.png" rel="nofollow noreferrer">“在此处输入图像说明”

确保您的路径参数如下:

“在此处输入图像描述”

  1. 然后,在过滤行的公式框中,更改
    路径(包括引号)到路径。也就是说,更改此信息:

    = table.selectrows(源,每个([folder path] =“ https://yourdomain.sharepoint.com/sites/testteam/shared documents /shared documents/enter/general/”))

    to ...

    = table.selectrows(源,每个(([folder path] = path)))

您应该能够用任何其他有效根文件夹中的文件夹路径。

由于这些源文件位于SharePoint内,因此SharePoint的访问控件占上风。因此,要使用户能够访问实际的源文件,这是刷新需要做的,需要刷新的用户将需要适当的SharePoint访问权限的源文件以访问它们。但是,我可以肯定的是,一旦完成了刷新,任何可以访问您的文件的用户(无论是在Excel还是Power BI文件中)都应该能够看到其结果,而没有他们的结果需要对基础源文件的SharePoint访问权限。我的理解是,只有那些实际访问源文件(例如刷新)的人才需要访问这些文件的人。无论如何,这是我的经验。

Try this. After you have placed your excel workbook into SharePoint, open the workbook from within SharePoint and then select the Editing dropdown and Open in Desktop App:

enter image description here

Then, in the workbook, click File > Info > Copy Path. (This will get you the path you need for power query to directly retrieve the workbook. It is different than what you would have gotten if you tried to copy the path using SharePoint's features for copying the path.)

enter image description here

enter image description here

Paste that path somewhere safe for reuse. Then close your workbook and open Power Query.

In Power Query, create two new parameters:

Click Home > the bottom part of the Manage Parameters button > New Parameters:

enter image description here

Change the name from Parameter1 to path and change the Type to Text, then paste the path you copied for your workbook into the Current Value box. Then, from the path you just pasted, delete all the text that follows the last forward slash (/). Make sure you do include that last / in the path. You'll put the file name that follows the / in a separate parameter and won't need the ?web=1.

enter image description here

Don't click OK. Instead, click New, at the top of the Manage Parameters window. This will set you up to create the filename parameter.

Change the name from Parameter1 to filename and change the Type to Text, then paste the filename you copied for your workbook into the Current Value box. Again, you won't need the ?web=1 from the original path.

enter image description here

Click OK.

Now you can use the parameters, path and filename in your source line. And here's how to do that:

Source = Excel.Workbook(Web.Contents(path & filename), null, true)

This will give you a table like this:

enter image description here

-------------------------------------------------

For Mirza's follow-up, regarding pulling the files from the SharePoint folder, try this instead:

  1. In Get Data, select the SharePoint Folder connector and click
    Connect.
  2. For the SharePoint Folder Site URL, enter your URL for
    your root folder for your files in SharePoint and click OK.
  3. You may have to enter your credentials. If so, do that.
  4. At this point, you should see a listing of your files that you have in SharePoint. Click the Transform Data button.
  5. In the table that you see next:
    • Filter the Name column to the name of only one file that you
      want to open. (Use the dropdown arrow at the top-right.)
    • Filter the Folder Path column to only one path. (Same named
      files can be stored in multiple folders and you'll want to be sure
      you are selecting the right file by matching it with its correct
      folder, and this sets the stage for that.)
  6. Next, set up the two parameters--path and filename--similarly to what I said in my first approach. But this time:
    • For the path's current value, paste the value from the Folder Path column of the table you're currently in; and
    • For the filename's current value, paste the value from the Name column of the table you're currently in.
  7. Then, in the formula box for the Filtered Rows, change the name of the file (including the quotes) to filename and change the name of the path (including the quotes) to path.

You should wind up with code like this (yourdomain.sharepoint.com/your/root/folder/URL would be your URL to your root folder for your documents in sharepoint instead):

let
    Source = SharePoint.Files("https://yourdomain.sharepoint.com/your/root/folder/URL", [ApiVersion = 15]),
    #"Filtered Rows" = Table.SelectRows(Source, each (([Name] = filename and [Folder Path] = path)))
in
    #"Filtered Rows"

You should see something like this:

enter image description here

Click Binary, in the Content column.

-------------------------------------------------

For Mirza's second follow-up, regarding pulling multiple files from the SharePoint folder instead of just one file, try this instead:

  1. In Get Data, select the SharePoint Folder connector and click
    Connect.
  2. For the SharePoint Folder Site URL, enter your URL for your
    root folder for your files in SharePoint and click OK. You may have
    to enter your credentials. If so, do that.
  3. At this point, you should see a listing of your files that you have in SharePoint. Click the Transform Data button.

enter image description here

  1. In the table that you see next: Filter the Folder Path column to only one path--the path of the folder where the files you want are located. (Note that this is different from looking for a single file, addressed in my previous answers, because this skips filtering by the Name column.)

enter image description here

enter image description here

  1. Next, set up a parameter called path. For the path's current value, copy and paste the value from the Folder Path column of the table that you're currently in.
  • To copy the path from the Folder Path column:

enter image description here

  • To paste the path you just copied from the Folder Path column to the
    path parameter's current value:

    Click Home, then click on Manage Parameters, then if you haven't already created the path parameter, click new (otherwise click on the path parameter to select it):

enter image description here

Make sure your path parameter is set up like this:

enter image description here

  1. Then, in the formula box for the Filtered Rows, change the name of
    the path (including the quotes) to path. That is, change this:

    = Table.SelectRows(Source, each ([Folder Path] = "https://yourdomain.sharepoint.com/sites/TestTeam/Shared Documents/General/"))

    to...

    = Table.SelectRows(Source, each (([Folder Path] = path)))

You should be able to replace the current value of the path parameter with any other valid folder path within your root folder.

Since these source files reside within SharePoint, SharePoint's access controls prevail. So, for a user to be able to access the actual source files, which is what a refresh needs to do, the users who need to refresh will need appropriate SharePoint access rights to the source files in order to access them. However, I'm fairly certain that once the refresh has been done, any users who have access to your file that is built from this query's results (be it in an excel or Power BI file) should be able to see its results without them needing SharePoint access rights to the underlying source files. My understanding is that only those people who actually access to the source files, like for refresh, require the SharePoint access to those files. This has been my experience anyway.

樱花落人离去 2025-02-20 19:28:49

有一个用于电源查询本机的文件的SharePoint连接器,不会使用您的用户名。

https://learn.microsoft.com/en-en-poper-us/power-us-poper-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-us-poper-us-us-us-查询/连接器/sharepointFolder

旁注,您不能使用cell函数,

  • 因为较旧的Excel,用户可能需要按CTRL+ALT+F9使用其用户名更新计算,用户会讨厌它。
  • 在O365 Excel中,如果将文件存储在SP中,则返回文件URL。因此,您最终还将使用上面的SharePoint连接器。

There is a SharePoint connector for files native to Power Query, that will not use your username.

https://learn.microsoft.com/en-us/power-query/connectors/sharepointfolder

Side note, you cannot use CELL function because

  • in older Excel, user may need to press CTRL+ALT+F9 to update the calculation with their username, user will hate it.
  • in O365 Excel, it returns the file url if the file is stored in SP. So you will end up using the SharePoint connector above as well.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文