2017-03-17 162 views
0

我试图获取一起出现的项目Id的费率表。我在Excel中有一个表格,它是行和列中唯一的ID列表。然后,我计算并划分它们匹配且不匹配的销售订单号码。我试图在bi bi中复制这个,但是我还是有点新到了。我尝试了几种方式进行旋转,但我不确定我是否在正确的道路上。Power Query/PowerBi:2个或多个表中的项目在一个交易中出现在一起的项目

产品基本上会|销售订单

A | 1

B | 1

A | 2

B | 3

C | 4

成类似

| A | B | C | 

A | 1.0 | 0.33 | 0.0

B | 0.33 | 1.0 | 0.0

C | 0.0 | 0.0 | 1.0 |

任何帮助非常感谢

This is an example of the table I currently use to do this. I'm just counting sales orders that match up with the products

回答

0

我的建议有3个查询:

  1. 表1(输入)。

  2. OrdersPerProduct:具有独特产品和独特订单的嵌套列表的表。

  3. 结果:产生所需的输出。

查询OrdersPerProduct:

let 
    Source = Table1, 
    #"Grouped Rows" = Table.Group(Source, {"Product"}, {{"Orders", each List.Distinct(_[Order]), type list}}) 
in 
    #"Grouped Rows" 

查询结果:

注所有产品列的动态 “更改类型” 的最后一步

let 
    Source = OrdersPerProduct, 
    #"Removed Columns" = Table.RemoveColumns(Source,{"Orders"}), 
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "OtherProduct", each #"Removed Columns"[Product], type {text}), 
    #"Expanded OtherProduct" = Table.ExpandListColumn(#"Added Custom", "OtherProduct"), 
    #"Merged Queries" = Table.NestedJoin(#"Expanded OtherProduct",{"Product"},OrdersPerProduct,{"Product"},"ProductOrders",JoinKind.LeftOuter), 
    #"Expanded ProductOrders" = Table.ExpandTableColumn(#"Merged Queries", "ProductOrders", {"Orders"}, {"ProductOrders"}), 
    #"Merged Queries1" = Table.NestedJoin(#"Expanded ProductOrders",{"OtherProduct"},OrdersPerProduct,{"Product"},"NewColumn",JoinKind.LeftOuter), 
    #"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries1", "NewColumn", {"Orders"}, {"OtherProductOrders"}), 
    #"Added Custom1" = Table.AddColumn(#"Expanded NewColumn", "Rate", each List.Count(List.Intersect({[ProductOrders],[OtherProductOrders]}))/List.Count(List.Distinct(List.Combine({[ProductOrders],[OtherProductOrders]})))), 
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"ProductOrders", "OtherProductOrders"}), 
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[OtherProduct]), "OtherProduct", "Rate"), 
    #"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",List.Transform(#"Pivoted Column"[Product], each {_, type number})) 
in 
    #"Changed Type" 
+0

谢谢马塞尔,我正在尝试你提出的解决方案。我只是想跟随它背后的逻辑。 – Tom

+0

嗯,我面临同样的挑战。您可以轻松地复制代码并尝试使用您的数据,并逐步查看正在发生的事情。如果您需要进一步澄清,请让我知道。 – MarcelBeug

+0

嘿@marcel,我运行它没有一个表达式。错误不能将值转换为添加的Custom1的类型列表。我试图进入数据来改变类型,就像我发现[这里](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/c6a96479-2376-49a1-a6e6-2bf275eaaa04/power-query -error-expressionerror-we-can-convert-the-value-to-type-text?forum = sqlkjpowerpivotforexcel)但我一直无法使它工作。你有没有遇到过这个错误? – Tom

相关问题