如何根据表中的值范围过滤Power Query?

时间:2019-02-11 18:26:23

标签: excel powerquery

好吧,所以我有一系列每月都会更改的值,我希望能够从这些值中过滤出Power Query,但不能完全正确地编写代码。下面是将表拉入Power Query的代码,可以从代码下面的图片中看到它的工作原理。从该代码中可以看到,我在Excel中的表名是Job,而我要过滤掉的列是Order。第二组代码是到目前为止,我不得不尝试从该表中过滤掉我的查询的代码。我为查询粘贴了整个代码,但实际上这是问题所在所在的#“按订单过滤”行。对于使该代码正常工作的任何帮助,将不胜感激。

let
Source = Excel.CurrentWorkbook(){[Name="Job"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Order", type text}})
in
#"Changed Type"

enter image description here

let
Source = Sql.Database("jansql01", "mas500_app"),
dbo_vdvInvoiceLine = Source{[Schema="dbo",Item="vdvInvoiceLine"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(dbo_vdvInvoiceLine,{"Description", "ItemID", "STaxClassID", "ExtAmt", "FreightAmt", "TranID", "TradeDiscAmt", "FormattedGLAcctNo", "Segment1", "Segment2", "Segment3", "SalesOrder", "CustID", "CustName", "TranDate", "PostDate", "City", "StateID", "ItemClassID", "ReleaseSO", "Job Number"}),
#"Filtered by Order" = Table.SelectRows(#"Removed Other Columns", each Table.Contains(Order,[SalesOrder = [Order]])),
#"Added Material Column" = Table.AddColumn(#"Filtered by Order", "Material $", each if [ItemClassID] <> "INSTALLATION" then [ExtAmt] else 0),
#"Added Installation Column" = Table.AddColumn(#"Added Material Column", "Installation $", each if [ItemClassID] = "INSTALLATION" then [ExtAmt] else 0),
#"Merged Queries" = Table.NestedJoin(#"Added Installation Column",{"TranID"},vdvInvoice,{"TranID"},"vdvInvoice",JoinKind.LeftOuter),
#"Expanded vdvInvoice" = Table.ExpandTableColumn(#"Merged Queries", "vdvInvoice", {"STaxAmt"}, {"vdvInvoice.STaxAmt"}),
#"Extracted Date" = Table.TransformColumns(#"Expanded vdvInvoice",{{"TranDate", DateTime.Date, type date}, {"PostDate", DateTime.Date, type date}}),
#"Added Invoice+Tax" = Table.AddColumn(#"Extracted Date", "Invoice+Tax", each [TranID]&Number.ToText([vdvInvoice.STaxAmt])),
#"Sorted Invoice+Tax" = Table.Sort(#"Added Invoice+Tax",{{"Invoice+Tax", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Invoice+Tax", "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Invoice+Tax2", each if [Index]=0 then [#"Invoice+Tax"] else if #"Added Index"{[Index]-1}[#"Invoice+Tax"]=[#"Invoice+Tax"] then null else [#"Invoice+Tax"]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Added Tax Column" = Table.AddColumn(#"Removed Columns", "Tax $", each if [#"Invoice+Tax2"] = null then 0 else [vdvInvoice.STaxAmt]),
#"Changed Tax Type" = Table.TransformColumnTypes(#"Added Tax Column",{{"Tax $", type number}}),
#"Added Total Contract" = Table.AddColumn(#"Changed Tax Type", "Total Contract $", each [#"Material $"]+[FreightAmt]+[#"Installation $"]+[#"Tax $"])
in
#"Added Total Contract"

2 个答案:

答案 0 :(得分:1)

合并两个查询。您可以单击“合并”命令中的某些设置以仅保留匹配的行。

答案 1 :(得分:0)

teylyn的答案是一个很好且可能的方法。

另一种解决方法,可能是将Job表的Order number作为列表传递到第二个查询的filter参数中:

Orderlist = Job[Order]
.
.
.
#"Filtered by Order" = Table.SelectRows(#"Removed Other Columns", each List.Contains(Orderlist,[SalesOrder])),