Power Query或Power BI代码用于按多列中的行计数重复项

时间:2019-05-30 04:03:28

标签: powerbi powerquery

我需要计算在不同试验中使用相同代码集的次数。例如,从6个试验中,算出全部6个已被使用的数量,或者只有5、4、3、2,甚至只有1个。

我尝试创建所有组合(包括5、4等),但是主要问题是,如果使用计算列,它将导致内存错误。 一次我使用具有不可透视数据的幂查询但存在相同的问题,因为它使表中的项目数增加了一倍(或更多),从而导致内存问题。

所以我在考虑是否有可能在不重新创建组合的情况下使用过程中的实际数据。我阅读了一些有关记录或列表的内容,但未能创建必要的公式。 下面是我尝试创建5个条目的组合作为列表的内容,将2并并计算与第一行相同的每一行,但给我一个错误

样本数据:

Column1 Column2 Column3 Column4 Column5 Column6
A          B        C       D       E       F
A          B        C       D       E       G
A          K        L       M       N       O


result: line 1: equals 2 on 5 duplicates (line 1 and line 2); 0 on other duplicates
line 2: same as line 1
line 3: 3 on 1 duplicate (counting line 1, 2 and 3); 0 on other duplicates

@aldert 我能够创建类似这样的东西,但是正如您所看到的那样,最后一部分太长了,根据@alexis olson的帖子,该部分将仅覆盖前2个重复项(1和2)

    let
Source = Csv.Document(File.Contents("XXX\Sample_small data.csv"),[Delimiter=",", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "ID", each [Column1]&"-"&[Column2]&"-"&[Column3]&"-"&[Column4]&"-"&[Column5]&"-"&[Column6]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "First Duplicate 2", each List.Count(
Table.SelectRows(
    #"Added Custom", (C) => (C[Column1] = [Column1] or C[Column1] = [Column2] or C[Column1] = [Column3] 

    or C[Column1] = [Column4] 
    or C[Column1] = [Column5] 
    or C[Column1] = [Column6]) 
    and (C[Column2] = [Column1] 
    or C[Column2] = [Column2] 
    or C[Column2] = [Column3] 
    or C[Column2] = [Column4] 
    or C[Column2] = [Column5] 
    or C[Column2] = [Column6]) 
    and (C[Column3] <> [Column1]
    and C[Column3] <> [Column2]
    and C[Column3] <> [Column3]
    and C[Column3] <> [Column4]
    and C[Column3] <> [Column5]
    and C[Column3] <> [Column6])
    and (C[Column4] <> [Column1]
    and C[Column4] <> [Column2]
    and C[Column4] <> [Column3]
    and C[Column4] <> [Column4]
    and C[Column4] <> [Column5]
    and C[Column4] <> [Column6])
    and (C[Column5] <> [Column1]
    and C[Column5] <> [Column2]
    and C[Column5] <> [Column3]
    and C[Column5] <> [Column4]
    and C[Column5] <> [Column5]
    and C[Column5] <> [Column6])
    and (C[Column6] <> [Column1]
    and C[Column6] <> [Column2]
    and C[Column6] <> [Column3]
    and C[Column6] <> [Column4]
    and C[Column6] <> [Column5]
    and C[Column6] <> [Column6])
    )[ID]
    ))
    in
    #"Added Custom1"

0 个答案:

没有答案