For the below table, I am first looking to sum values by name and date. Then using that result data looking to take the average of value, grouped by name, for each month.
It seems like I want to take a take a pivot table of a pivot table. I am trying to use powerpivot although I am open to ideas.
Does anyone know how to do this?
date | value | name |
---|---|---|
12/29/23 | 50 | stephen |
12/29/23 | 20 | bob |
12/29/23 | 30 | tom |
12/28/23 | 5 | stephen |
12/28/23 | 7 | bob |
12/28/23 | 3 | tom |
Assuming there is no Excel Constraints
as per the tags posted, then this should work as expected :
• Using GROUPBY()
• Formula used in cell F3
=LET(
_GroupBySum, GROUPBY(HSTACK(B3:B8,D3:D8),C3:C8,SUM,0,0),
_CombineMonthName, HSTACK(TEXT(TAKE(_GroupBySum,,1),"mmm"),INDEX(_GroupBySum,,2)),
_Output, GROUPBY(_CombineMonthName,TAKE(_GroupBySum,,-1),AVERAGE,0,0),
VSTACK({"Month","Name","Values"},_Output))
Or,
• Using PIVOTBY()
• Formula used in cell F3
=LET(
_PivotBySum, PIVOTBY(HSTACK(B3:B8,D3:D8),,C3:C8,SUM,0,0),
_CombineMonthName, HSTACK(TEXT(TAKE(_PivotBySum,,1),"mmm"),INDEX(_PivotBySum,,2)),
_Output, PIVOTBY(_CombineMonthName,,TAKE(_PivotBySum,,-1),AVERAGE,0,0),
VSTACK({"Month","Name","Values"},_Output))
Note: Both the functions GROUPBY()
& PIVOTBY()
are available in MS365
for Beta Users.
Also I had prefer using POWER QUERY. To achieve this using the said procedure, follow this simple steps using POWER QUERY window UI
:
Table_1
let
Source = Excel.CurrentWorkbook(){[Name="Table_1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"value", Int64.Type}, {"name", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"date", "name"}, {{"Value", each List.Sum([value]), type nullable number}}),
#"Extracted Month Name" = Table.TransformColumns(#"Grouped Rows", {{"date", each Date.MonthName(_), type text}}),
#"Grouped Rows1" = Table.Group(#"Extracted Month Name", {"date", "name"}, {{"Values", each List.Average([Value]), type nullable number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows1",{{"name", Order.Ascending}})
in
#"Sorted Rows"
Updated with Months as headers.
let
Source = Excel.CurrentWorkbook(){[Name="Table_1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"value", Int64.Type}, {"name", type text}}),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Changed Type", {{"value", null}}),
#"Filtered Rows" = Table.SelectRows(#"Replaced Errors", each [value] <> null and [value] <> ""),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"date", "name"}, {{"Value", each List.Sum([value]), type nullable number}}),
#"Extracted Month Name" = Table.TransformColumns(#"Grouped Rows", {{"date", each Date.MonthName(_), type text}}),
#"Pivoted Column" = Table.Pivot(#"Extracted Month Name", List.Distinct(#"Extracted Month Name"[date]), "date", "Value", List.Average)
in
#"Pivoted Column"