【问题标题】:Automating An .XLS Report to Power BI将 .XLS 报告自动化到 Power BI
【发布时间】:2020-03-14 10:09:06
【问题描述】:

我正在尝试使用带有 Microsoft Flow 的 Power BI 数据集进行每日更新。这是我目前的情况:

  1. 每天早上都会以 .xls 格式将报告导出到 SharePoint
  2. 我打开 Excel 以刷新 Power Query,该 Power Query 将报表中的数据提取为仪表板和 MS Flow 的格式 (我尝试直接安排对 Power BI 的刷新,但似乎无法安排对 .xls 的刷新)
  3. MS Flow 和 Power BI 刷新包含查询数据的 .xlsx 文件

有没有办法避免手动打开 Excel 并在 Power Query 上点击刷新的步骤?

let
    Source = SharePoint.Files("https://xxxxxxxxxx", [ApiVersion = 15]),
    FileContent = Source{[Name = "Individual Status.xls"]}[Content],
    ParsedTable = ParseXls(FileContent, "Individual Status")
in
    Source

Expression.Error: We cannot convert the value "Individual Status" to type Binary.
Details:
Value=Individual Status.xls
Type=[Type]

【问题讨论】:

    标签: powerbi powerquery


    【解决方案1】:

    一种可能的解决方法是直接在 Power BI 中使用 Python 或 R 脚本解析 .xls 文件。

    这是一个使用 Python 解析 .xls 的示例函数。将以下代码复制并粘贴到空白查询中,并将其重命名为“ParseXls”或一些有意义的名称。

    (workbook as binary, sheet as text, optional header as logical) as table =>
        let dataset = Table.FromRecords({[
            content = Binary.ToText(workbook, BinaryEncoding.Base64),
            sheet = sheet,
            header = if header = null then true else header
        ]}),
        result = Python.Execute("import pandas as pd#(lf)from base64 import b64decode#(lf)from io import BytesIO#(lf)params = dataset.loc[0]#(lf)content = BytesIO(b64decode(params['content']))#(lf)sheet = params['sheet']#(lf)header = 0 if params['header'] else None#(lf)result = pd.read_excel(content, sheet, header=header)", [dataset = dataset]){[Name = "result"]}[Value]
        in result
    

    然后,您可以将此函数用于从 SharePoint 获得的二进制文件内容。整个查询将如下所示。

    let
        Source = SharePoint.Files("https://yourcompany.sharepoint.com/...", [ApiVersion = 15]),
        FileContent = Source{[Name = "Book1.xls"]}[Content],
        ParsedTable = ParseXls(FileContent, "Sheet1"),
        ...
    

    【讨论】:

    • 感谢您的信息...我有点关注,但遇到了一些问题。看起来您正在使用 pandas 从 SPT 导入数据,并使用 read_excel 实际将其拉入。几个问题...... 1)b64decode 部分有什么作用? 2)这是我第一次在 Power BI 中使用 python,所以也许我做错了什么,但是在尝试转换为二进制时出现上述错误。
    • 1) Base64 编码只是将二进制文件发送到 Python 的一种解决方法。看起来 PQ 二进制文件映射到 Python3 str 类型,所以我选择在将二进制文件传递给 Python 之前将其编码为 base64 文本,然后在 Python 中将其解码回字节。 2)它是说你正在将一个文本值传递给一个需要二进制类型的函数,但是我在你的代码中找不到你正在做的部分......你能检查一下 FileContent 的实际类型是什么?
    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 2021-06-23
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2019-05-25
    相关资源
    最近更新 更多