【问题标题】:Power BI Power Query filter main table with two tables based on columns from bothPower BI Power Query 根据两个表中的列筛选主表
【发布时间】:2021-07-09 11:18:31
【问题描述】:

我有一张发票表,必须针对某些供应商和某些帐号进行过滤。必须过滤的供应商和帐号都在两个小的 Excel 文件中。如何将它们与主表连接,以便主表被过滤为帐号或供应商编号?

【问题讨论】:

    标签: filter merge powerbi powerquery


    【解决方案1】:

    有几种方法可以做到这一点。这是一个解决方案,它通过分别过滤供应商和帐号,然后将它们组合成一个用于过滤主表的列表来创建两个发票编号列表。

    这里是使用的示例表(全部包含在下面的 M 代码中):

    主表


    供应商过滤器


    帐户过滤器


    这是您可以粘贴到空白查询中的 M 代码:

    let
        // Import sample tables and edit data types if needed
        SourceMainTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY+7DcAgDAV3oabwB4dQZok0EfuvkWcLJNMg3p3Qie8rRFxqYSOcr18fgFldCNYILgdXLOEQeoiG1SlECyFLGJZSSmxxYbWU2Lx7wlJCl7j9QU+JLYb/glNiiUCWEuDzBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Invoice = _t, Value = _t, Vendor = _t, Account = _t]),
        MainTable = Table.TransformColumnTypes(SourceMainTable,{{"Invoice", type text}, {"Value", Int64.Type}, {"Vendor", type text}, {"Account", type text}}),
        VendorFilter = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjNWitUBUiZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Vendor = _t]),
        AccountFilter = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t]),
        
        // Extract lists of invoice numbers filtered for vendors and for account numbers separately
        ListInvoicesFilteredVendors = Table.SelectRows(MainTable, each List.Contains(VendorFilter[Vendor], [Vendor]))[Invoice],
        ListInvoicesFilteredAccounts = Table.SelectRows(MainTable, each List.Contains(AccountFilter[Account], [Account]))[Invoice],
        
        // Combine both lists of invoice numbers into one list and use it to filter main table
        ListInvoicesCombined = List.Distinct(List.Combine({ListInvoicesFilteredVendors, ListInvoicesFilteredAccounts})),
        FilteredTable = Table.SelectRows(MainTable, each List.Contains(ListInvoicesCombined, [Invoice]))
    in
        FilteredTable
    

    这是结果,您可以看到发票 001、002、005 和 006 不包括在内,因为它们不包含 V3、V4 或 A03:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多