【问题标题】:Add sections of a table column as new Columns - Power Query将表列的部分添加为新列 - Power Query
【发布时间】:2020-10-15 07:30:37
【问题描述】:

我有以下 Unpivoted table,其中包含 Stat-tested % values 以及它们的 Stat-lettersStat-Letters position indicators 在不同的行上。

----------------------------------------
CODE  |  ATTR      |  TEXT     |  VALUE
----------------------------------------
1       mean        I love it     0.45
2       mean        I love it     0.67
3       mean        I love it     0.49
4       mean        I love it     0.21
5       mean        I love it     0.66
1       mean        I love it     abd
2       mean        I love it     e
3       mean        I love it     cd
4       mean        I love it     a
5       mean        I love it     ab
1       mean        I love it     1
2       mean        I love it     1
3       mean        I love it     1
4       mean        I love it     1
5       mean        I love it     1
1       wt-mean     I hate it     0.22
2       wt-mean     I hate it     0.56
3       wt-mean     I hate it     0.13
4       wt-mean     I hate it     0.89
5       wt-mean     I hate it     0.50
1       wt-mean     I hate it     ab
2       wt-mean     I hate it     ae
3       wt-mean     I hate it     c
4       wt-mean     I hate it     b
5       wt-mean     I hate it     de
1       wt-mean     I hate it     1
2       wt-mean     I hate it     1
3       wt-mean     I hate it     1
4       wt-mean     I hate it     1
5       wt-mean     I hate it     1

我想在 CODE 列上进行分组,并将 Stat-tested Lettersposition indicators 添加为单独的列,如下所示:

----------------------------------------------------------------
CODE  |  ATTR      |  TEXT     |  VALUE     LETTERS     POSITION
----------------------------------------------------------------
1       mean        I love it     0.45      abd         1
2       mean        I love it     0.67      e           1
3       mean        I love it     0.49      cd          1
4       mean        I love it     0.21      a           1
5       mean        I love it     0.66      ab          1
1       wt-mean     I hate it     0.22      ab          1
2       wt-mean     I hate it     0.56      ae          1
3       wt-mean     I hate it     0.13      c           1
4       wt-mean     I hate it     0.89      b           1
5       wt-mean     I hate it     0.50      de          1

我在对 Value 列上的数据进行分组时遇到的问题是该列具有混合数据类型(文本、数字)。如何将这些拆分成单独的列,如下所示?

【问题讨论】:

    标签: excel group-by split pivot powerquery


    【解决方案1】:

    您可以为此插入新的自定义列,如果值是数字,请检查 try

    我的来源是Tabelle1

    let
        Quelle = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
        Change_Type = Table.TransformColumnTypes(Quelle,{{"CODE", Int64.Type}, {"ATTR", type text}, {"TEXT", type text}, {"VALUE", type any}}),
        Custom_Number = Table.AddColumn(Change_Type, "Number", each if try Number.From([VALUE]) < 1 otherwise null = true then [VALUE] else null),
        Custom_Letters = Table.AddColumn(Custom_Number, "Letters", each if (try Number.From([VALUE]) >= 1 otherwise null) = null then [VALUE] else null),
        #"Hinzugefügte benutzerdefinierte Spalte" = Table.AddColumn(Custom_Letters, "POSITION", each if [Number] = null and [Letters]= null then [VALUE] else null),
        Grouped_Rows = Table.Group(#"Hinzugefügte benutzerdefinierte Spalte", {"CODE", "ATTR", "TEXT"}, {{"VALUE", each List.Max([Number]), type nullable number}, {"LETTERS", each List.Max([Letters]), type nullable text}, {"POSITION", each List.Max([POSITION]), type nullable number}})
    in
        Grouped_Rows
    

    【讨论】:

    • 您可以缩短 if 条件,例如 if try Number.From([VALUE]) &lt; 1 otherwise false then [VALUE] else nullif try Number.From([VALUE]) &gt; 1 otherwise true then [VALUE] else null,对于第三列,可以使用 if try Number.From([VALUE]) &gt;= 1 otherwise false then [VALUE] else null。但是很好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多