DAX-如何根据另一个表过滤表

时间:2020-11-11 01:13:03

标签: powerbi dax

如何在PowerBI中基于另一个表过滤一个表?我尝试使用MERGE通过ManufacturuerPartKey和TxDate内部联接数据,但我一直收到错误消息“ Expression.Error:枚举中的元素太多,无法完成操作。详细信息:[List]”

还有另一种方法可以做到这一点吗?

表1:

 Manufacturer  PartNo     TxDate     TxStatus      ErrorCode     ManufacturerPartKey
  ABC             123    9/29/2020   Fail         CodeInvalid     ABC-123
  ABC             123    9/30/2020   Fail         CodeInvalid     ABC-123
  ABC             123    10/1/2020   Success      null            ABC-123
  ABC             789    10/1/2020   Fail         PartInvalid     ABC-789   
  ABC             567    10/1/2020   Success      null            ABC-567
  XYZ             567    9/29/2020   Fail         LoadFail        XYZ-567
  XYZ             567    9/30/2020   Fail         LoadFail        XYZ-567
  XYZ             789    10/1/2020   Fail         APIFault        XYZ-789
  LMO             456    9/29/2020   Fail         APIFault        LMO-456
  LMO             456    9/30/2020   Fail         APIFault        LMO-456
  EFG             123    10/1/2020   Success      null            EFG-123
  QRS             123    10/1/2020   Fail         PartInvalid     QRS-123
  QRS             123    10/2/2020   Fail         PartInvalid     QRS-123
  QRS             123    10/3/2020   Fail         PartInvalid     QRS-123
  QRS             123    10/4/2020   Fail         PartInvalid     QRS-123
  QRS             567    10/4/2020   Success      null            QRS-567

表2:

 Manufacturer  PartNo     TxDate      ManufacturerPartKey
  ABC             123    10/1/2020      ABC-123
  ABC             789    10/1/2020      ABC-789   
  ABC             567    10/1/2020      ABC-567
  XYZ             567    9/30/2020      XYZ-567
  XYZ             789    10/1/2020      XYZ-789
  LMO             456    9/30/2020      LMO-456
  EFG             123    10/1/2020      EFG-123
  QRS             123    10/4/2020      QRS-123
  QRS             567    10/4/2020      QRS-567

预期输出:

 Manufacturer  PartNo     TxDate     TxStatus      ErrorCode     ManufacturerPartKey
  ABC             123    10/1/2020   Success      null            ABC-123
  ABC             567    10/1/2020   Success      null            ABC-567
  ABC             789    10/1/2020   Fail         PartInvalid     ABC-789   
  XYZ             567    9/30/2020   Fail         LoadFail        XYZ-567
  XYZ             789    10/1/2020   Fail         APIFault        XYZ-789
  LMO             456    9/30/2020   Fail         APIFault        LMO-456
  EFG             123    10/1/2020   Success      null            EFG-123
  QRS             123    10/4/2020   Fail         PartInvalid     QRS-123
  QRS             567    10/4/2020   Success      null            QRS-567

2 个答案:

答案 0 :(得分:1)

我尝试用相同的数据创建相同的两个表,并使用 ManufacturuerPartKey 和<合并了新的查询(第二个表左外部MERGE 与第一个表) strong> TxDate ,我得到了一个新表,其中包含您想要的结果,没有任何错误

enter image description here

,然后在第一个表的第二个表中展开 TxStatus ErrorCode 。我得到以下信息:

enter image description here

此表不包含您想要的内容吗?

答案 1 :(得分:1)

尝试应用此解决方案:

https://www.biinsight.com/quick-tips-how-to-filter-a-column-by-a-column-from-a-different-query-in-power-query/

这与SQL的where语句Column1一起使用(从AnotherTable中选择column2)

#"Filtered Rows" = Table.SelectRows(#"PREVIOUS_STEP", each List.Contains(#"REFERENCED_TABLE"[REFERENCED_COLUMN], [COLUMN_TO_BE_FILTERED]))

这里是代码示例(我将表导入为两个Excel文件):

let
    Source = Excel.Workbook(File.Contents("C:\Users\MyUser\Downloads\tab1.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"TxDate", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each List.Contains(#"Tabl2"[ManufacturerPartKey], [ManufacturerPartKey]) and List.Contains(#"Tabl2"[TxDate], [TxDate]))
in
    #"Filtered Rows"

这是最有趣的部分:

#"Filtered Rows" = Table.SelectRows(#"Changed Type", each List.Contains(#"Tabl2"[ManufacturerPartKey], [ManufacturerPartKey]) and List.Contains(#"Tabl2"[TxDate], [TxDate]))