简体   繁体   English

组合两个(表或列表)以创建 Power BI 中唯一组合的 output

[英]Combine two (tables or lists) to create output of unique combinations in Power BI

Trying to combine two queries in Power BI to have an output of unique combinations.尝试在 Power BI 中组合两个查询以获得唯一组合的 output。

for instance one list(or column): A, B, C例如一个列表(或列):A、B、C

and another list(or column): 1, 2, 3, 4, 5, 6, 7和另一个列表(或列):1、2、3、4、5、6、7

Output should be: A1, A2, A3, A4, A5, A6, A7, B1, B2, B3, B4, B5, B6, B7, C1, C2, C3, C4, C5, C6, C7 Output 应该是:A1,A2,A3,A4,A5,A6,A7,B1,B2,B3,B4,B5,B6,B7,C1,C2,C3,C4,C5,C6,C7

Is there a way to accomplish this?有没有办法做到这一点? (Yes my rows are not equal in count) Just don't know the best or right approach for this (tried using combine with a helper column and hit a dead end as duplicates get created, unless I did that wrong) (是的,我的行数不相等)只是不知道最好或正确的方法(尝试使用带有辅助列的组合并在创建重复项时遇到死胡同,除非我做错了)

This is essentially a Cartesian product (aka cross product) of two lists.这本质上是两个列表的笛卡尔积(又名叉积)。

If you just have two text lists, you can do a one-liner like this:如果你只有两个文本列表,你可以像这样做一个单行:

List.Combine(List.Transform(List1, (L1) => List.Transform(List2, (L2) => L1 & L2)))

This says for each item X in the first list, create a list that is the second list with X prepended to each element.这表示对于第一个列表中的每个项目 X,创建一个列表,该列表是第二个列表,每个元素都附加 X。 This gives a list of lists that is flattened out to a single list using the combine function.这给出了使用组合 function 扁平化为单个列表的列表列表。


It's not uncommon to want to do this with table though too.不过,想要用 table 来做这件事并不少见。 In this case, the analogous idea is to define a new column on one table where each row is the entire list/column of the other and then expand that new column.在这种情况下,类似的想法是在一个表上定义一个新列,其中每一行是另一个表的整个列表/列,然后扩展该新列。

Assuming we want the cross product of Table1[Column1] and Table2[Column2] :假设我们想要Table1[Column1]Table2[Column2]的叉积:

let
    Table1 = <Table1 Source>,
    AddCustom = Table.AddColumn(Table1 , "Custom", each Table2),
    Expand = Table.ExpandTableColumn(AddCustom, "Custom", {"Column2"}, {"Column2"}),
    Concatenate = Table.AddColumn(Expand, "Concatenate", each [Column1] & [Column2])
in
    Concatenate

Edit: You can do the concatenation before the expand too:编辑:您也可以在展开之前进行连接:

let
    Table1 = <Table1 Source>,
    AddCustom = Table.AddColumn(Table1 , "Custom",
                    (T1) => List.Transform(Table2[Column2], each T1[Column1] & _)),
    Expanded = Table.ExpandListColumn(AddCustom, "Custom")
in
    Expanded

References with more detail:更详细的参考资料:

Cartesian Product Joins 笛卡尔积连接

Cartesian Join of two queries... 两个查询的笛卡尔连接...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM