对不同列中相同类型的值进行透视 table

Pivot table for same types of values in different columns

我有这个数据集,其中同一个人可能在两个不同的列中,因此 his/her 销售记录。有什么方法可以使用 pivot table?

按个人和国家/地区查看他们的汇总销售记录

MS Excel 2016

Dataset sample and the way Pivot table should be

我认为你最好使用 Data>Get&Transform 或 Data>From Table/Range(我不确定 excel 2016 中的名称,我使用的是 365)。这是为了将您的 table 带到 Power Query 并对其进行转换。

这里的关键是 Transform>Unpivot Other Columns (out of Country and City columns), values: Person1, Person2, Person1_sales, Person2_sales 到 1 列。接下来删除“1”和“2”,使该列仅包含 2 个值:Person 和 Person_sales。然后 Transform>Pivot 该列变为 2 列:Person 和 Person_sales:

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Country", "City"}, "Attribute", "Value"),
    #"Added addedName_temp" = Table.AddColumn(#"Unpivoted Other Columns", "addedName_temp", each [Country]&Text.Middle([Attribute],6,1)),
    #"Replace '1'" = Table.ReplaceValue(#"Added addedName_temp","1","",Replacer.ReplaceText,{"Attribute"}),
    #"Replace '2'" = Table.ReplaceValue(#"Replace '1'","2","",Replacer.ReplaceText,{"Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Replace '2'", List.Distinct(#"Replace '2'"[Attribute]), "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"addedName_temp"})
in
    #"Removed Columns"

Table 转换后的样子: enter image description here

这是您在 excel 数据透视表中的结果: enter image description here

如果您有很多对 Person 和 Person_sales,您仍然可以使用此查询。玩得开心。