2016-09-11 84 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"