I have an excel file that contains a table formatted in this way (the example here below is just notional):
Table 1
Column A | Column B | Column C |
---|---|---|
Group A | Option 12 | Option 33 |
Group B | Option 11 | Option 15 |
Group B | Option 12 | Option 13 |
Group B | Option 1 | Option 10 |
Group A | Option 1 | Option 13 |
Group B | Option 12 | Option 15 |
Out of this table, I would like to create a Pivot table that provides the following summary (basically shows the count for each option, grouped by the Group)
Table 2
- | Option 1 | Option 10 | Option 11 | Option 12 | Option 13 | Option 15 | Option 33 |
---|---|---|---|---|---|---|---|
Group A | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
Group B | 1 | 1 | 1 | 2 | 1 | 2 | 0 |
In order to obtain this result, I found that I need to "unroll" Table 1 like this (selecting Table 1 does not produce the expected results in Table 2):
Table 3
Column A repeated twice | Column B/C merged |
---|---|
Group A | Option 12 |
Group B | Option 11 |
Group B | Option 12 |
Group B | Option 1 |
Group A | Option 1 |
Group B | Option 12 |
Group A | Option 33 |
Group B | Option 15 |
Group B | Option 13 |
Group B | Option 10 |
Group A | Option 13 |
Group B | Option 15 |
Does anyone know if is it possible to get Table 2 results using Table 1 data, instead of Table 3 ?
Thanks
The entire transformation can also be accomplished using Power Query, available in Windows Excel 2010+ and Microsoft 365 (Windows or Mac)
To use Power Query
Data => Get&Transform => from Table/Range
Home => Advanced Editor
Applied Steps
to understand the algorithmlet
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table28"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column A", type text}, {"Column B", type text}, {"Column C", type text}}),
//Unpivot columns except for the first
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column A"}, "Attribute", "Value"),
//Remove Attribute column
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
//Make a list of the Options which will be used as column headers in the Pivoted table
//This will be sorted alphabetically, but could be sorted alphanumerically if necessary
colHdrs = List.Sort(List.Distinct(#"Removed Columns"[Value])),
//Group by "Group" in Column A
//Then aggregate by Pivoting each sub group
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Column A"}, {
{"Pivot", each Table.Pivot(_, colHdrs, "Value","Value", List.Count)}}),
//Expand the aggregated group pivots
#"Expanded Pivot" = Table.ExpandTableColumn(#"Grouped Rows", "Pivot", colHdrs),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Pivot", List.Transform(colHdrs, each {_, Int64.Type}))
in
#"Changed Type1"