在 Excel 中使用 Power Query 透视多列
Pivoting multiple columns using PowerQuery in Excel
我正在尝试旋转以下的多列 table:
我想得到的结果如下:
我在 Excel 中使用 PowerQuery,但我无法旋转多个列(例如,我可以旋转列 "Number")。有人对正确使用 PowerQuery 有任何见解吗?
例如,如果国家 header 在单元格 A1
中,那么 D2
中的这个公式:
= "tax rate" & CountIf( $A:$A2, $A2 )
然后复制公式单元格 D2
并将其粘贴到它下面的单元格中,应该会得到如下内容:
country tax rate Income thresholds count
UK 20% 35k tax rate1
UK 30% 50k tax rate2
.....
现在您可以使用数据透视表或 PowerQuery 按额外计数列进行数据透视。您可以对收入 th1、收入 th2 等列使用相同的公式。
这是对您问题的第一个版本的回答
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
这是一个使用 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"
我正在尝试旋转以下的多列 table:
我想得到的结果如下:
我在 Excel 中使用 PowerQuery,但我无法旋转多个列(例如,我可以旋转列 "Number")。有人对正确使用 PowerQuery 有任何见解吗?
例如,如果国家 header 在单元格 A1
中,那么 D2
中的这个公式:
= "tax rate" & CountIf( $A:$A2, $A2 )
然后复制公式单元格 D2
并将其粘贴到它下面的单元格中,应该会得到如下内容:
country tax rate Income thresholds count
UK 20% 35k tax rate1
UK 30% 50k tax rate2
.....
现在您可以使用数据透视表或 PowerQuery 按额外计数列进行数据透视。您可以对收入 th1、收入 th2 等列使用相同的公式。
这是对您问题的第一个版本的回答
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
这是一个使用 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"