Power Query Pivot丢失行

时间:2018-04-04 15:05:52

标签: excel powerquery

我试图在Get & Transform中复制一些在Excel数据透视表中很容易完成的事情。但是当我尝试透视类别列时,我只得到一行。

原始数据

| Course         | Category |
|----------------|----------|
| English        | NL       |
| Mathematics    | CCA      |
| Social Studies | STS      |
| Social Studies | SS       |
| History        | STS      |
| History        | CCA      |
| Physics        | STS      |
| Geology        | SS       |
| Geology        | CCA      |

在Excel中,我可以通过拖动轻松生成所需的数据透视表:

  • 到行区域的课程
  • 列区域的类别
  • 值区域的类别

enter image description here

但是如果我尝试Get and Transform中的Pivot Category列,则会创建新列,但会汇总所有计数;不是每门课程的计数。 (如果我选择Don't Aggregate,我会收到错误。

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

M代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{" Course         ", type text}, {" Category ", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {" Course         "}, {{"Count", each _, type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {" Course         ", " Category "}, {"Count. Course         ", "Count. Category "}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Count", List.Distinct(#"Expanded Count"[#"Count. Category "]), "Count. Category ", "Count. Course         ", List.Count)
in
    #"Pivoted Column"

<强>结果:

Image of query

第3步:

Step2

第5步:

Step 5