2016-09-11 82 views
0
PowerQuery

我想转动下表的多列多列:枢轴在Excel中使用

enter image description here

我希望得到的结果如下: enter image description here

我在Excel中使用PowerQuery,但我无法设法转换多个列(例如,我可以将列“数字”转)。任何人都有关于正确使用PowerQuery的见解吗?

+1

您想要的额外列似乎不以任何方式透视。它们看起来像是所有行上具有相同值的额外列,因此您可以在Pivot之后添加额外的列。 – Slai

+0

如果额外的列在所有行中的值不相同,该怎么办? – Egodym

+0

然后你需要给出一个更好的例子和解释,因为它不完全清楚你想要达到的目标。 – Slai

回答

1

这里是一个回答你的问题的第一个版本

let 
    src = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    lettersABC=List.Distinct(src[Attribute1]), 
    count=List.Count(lettersABC), 
    lettersNUM=List.Transform({1..count}, each "Letter"&Number.ToText(_)), 
    numbersNUM=List.Transform({1..count}, each "Number"&Number.ToText(_)), 
    group = Table.Group(src, {"ID"}, {{"attr", each Record.FromList(lettersABC&[Attribute2], lettersNUM&[Attribute1])}}), 
    exp = Table.ExpandRecordColumn(group, "attr", lettersNUM&lettersABC, lettersNUM&numbersNUM) 
in 
    exp 

enter image description here

+1

我该如何使用它?我正在使用PowerQuery功能区上提供的命令。 – Egodym

+2

@Egodym View>高级编辑器https://www.excelcampus.com/wp-content/uploads/2015/03/PowerQueryAdvancedEditorMCodeLanguage.png – Slai

+0

令人印象深刻!谢谢Sergey和谢谢Slai! – Egodym

1

例如,如果该国头是细胞A1那么这个公式中D2

= "tax rate" & CountIf($A$2:$A2, $A2) 

然后复制公式单元格D2并将其粘贴在电池下方,应该给你的东西,如:

country tax rate Income thresholds count 
UK  20%   35k     tax rate1 
UK  30%   50k     tax rate2 
..... 

现在,您可以通过数据透视表或PowerQuery的额外计数列进行调整。您可以对收入th1,收入th2等列使用相同的公式。

0

下面是一个使用PQ色带的解决方案,但需要注意的最后一步(集团通过)不动如如果你想要每个国家的4 + 4列,你将不得不改变它。

let 
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
#"Changed Type" = Table.TransformColumnTypes(Source,{{"country", type text}, {"tax rate", type number}, {"Income thresholds", type text}}), 
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1), 
#"Grouped Rows" = Table.Group(#"Added Index", {"country"}, {{"Min Index", each List.Min([Index]), type number}, {"All Rows", each _, type table}}), 
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Income thresholds", "Index", "tax rate"}, {"Income thresholds", "Index", "tax rate"}), 
#"Added Custom" = Table.AddColumn(#"Expanded All Rows", "Column Index", each [Index] - [Min Index] + 1), 
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Min Index", "Index"}), 
#"Duplicated Column" = Table.DuplicateColumn(#"Removed Columns", "Column Index", "Column Index - Copy"), 
#"Added Prefix" = Table.TransformColumns(#"Duplicated Column", {{"Column Index", each "tax rate" & Text.From(_, "en-AU"), type text}}), 
#"Pivoted Column" = Table.Pivot(#"Added Prefix", List.Distinct(#"Added Prefix"[#"Column Index"]), "Column Index", "tax rate", List.Max), 
#"Added Prefix1" = Table.TransformColumns(#"Pivoted Column", {{"Column Index - Copy", each "Income thresholds" & Text.From(_, "en-AU"), type text}}), 
#"Pivoted Column1" = Table.Pivot(#"Added Prefix1", List.Distinct(#"Added Prefix1"[#"Column Index - Copy"]), "Column Index - Copy", "Income thresholds", List.Max), 
#"Grouped Rows1" = Table.Group(#"Pivoted Column1", {"country"}, {{"tax rate1", each List.Max([tax rate1]), type number}, {"tax rate2", each List.Max([tax rate2]), type number}, {"tax rate3", each List.Max([tax rate3]), type number}, {"Income th1", each List.Max([Income thresholds1]), type text}, {"Income th2", each List.Max([Income thresholds2]), type text}, {"Income th3", each List.Max([Income thresholds3]), type text}}) 
in 
#"Grouped Rows1"