merge rows based on cell value using aggregates

samh92

New Member
Joined
Jul 25, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm relatively new to VBA and I've been searching the forum for an answer but I'm struggling to implement any of the solutions I've come across.

I have a file that's produced daily and shows data for the previous days performance. I need to merge all of the data in the file to produce a daily total.

I've provided a snip of the data below:
1658758501493.png


What I'm trying to achieve is:
  • Merge all rows based on 'Product_Type'.
  • Columns in blue need to be summed
  • Columns in Green need to be an average
  • Time column is irrelevant so can be removed
Given the above example the final result should look like this:

1658758512204.png


Ideally id like to be able to run it as a macro as this task will need to be done daily and the files have 900+ rows.

Other examples that I've come across sort the data by a column and then merge cells but this doesn't total the other columns or produce an average which is what I need.

any help would be appreciated.

thank you.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Cannot manipulate data in a picture. Please reload your samples using XL2BB.
 
Upvote 0
2022-07-22 - SanOps_Q_Geoban(490).csv
ABCDEFGHIJKLMNOP
1DateTimeProductChannelProduct_TypeResourcePlanningOffered_Net_Abn5SecOffered_Inc_Abn5SecHandledAbandoned_Net_Abn5SecAbandoned_Inc_Abn5SecAbn5SecSL_NumberSL_Number_CRTime_to_AnswerTime_to_Abandon
222/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanANMF11111100019.0925210
322/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanANMF AMS22111000399121
422/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanANMF Flexi222000006450
522/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanANMF Inter11100000690
622/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanANMF RedDed6651100011028
722/07/2022 00:001899-12-30 08:45:00AccountsANMFGeobanw333000006710
822/07/2022 00:001899-12-30 08:45:00BereavementGeobanBnkSav777000571.432550
922/07/2022 00:001899-12-30 08:45:00BereavementGeobanBRVMNT Digital2220002100160
1022/07/2022 00:001899-12-30 08:45:00BereavementGeobanPOA333000266.671150
1122/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanANMF4343271616049.32577810409
1222/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanANMF AMS110110000531
1322/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanANMF Flexi1101100001357
1422/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanANMF Inter151515000853.333670
1522/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanANMF RedDed151578800078443423
1622/07/2022 00:001899-12-30 09:30:00AccountsANMFGeobanw3330000033910
2022-07-22 - SanOps_Q_Geoban(49
 
Upvote 0
Power Query Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Time", "ResourcePlanning"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Date", "Product", "Channel", "Product_Type"}, {{"Total Offered Net", each List.Sum([Offered_Net_Abn5Sec]), type number}, {"Total Offered Inc", each List.Sum([Offered_Inc_Abn5Sec]), type number}, {"Total Handled", each List.Sum([Handled]), type number}, {"Total Abandoned Net", each List.Sum([Abandoned_Net_Abn5Sec]), type number}, {"Total Abandoned Inc", each List.Sum([Abandoned_Inc_Abn5Sec]), type number}, {"Total Abn", each List.Sum([Abn5Sec]), type number}, {"Total SL", each List.Sum([SL_Number]), type number}, {"Avg SL Number CR", each List.Average([SL_Number_CR]), type number}, {"Avg Time to Ans", each List.Average([Time_to_Answer]), type number}, {"Avg Time to Abandon", each List.Average([Time_to_Abandon]), type number}})
in
    #"Grouped Rows"

DateProductChannelProduct_TypeTotal Offered NetTotal Offered IncTotal HandledTotal Abandoned NetTotal Abandoned IncTotal AbnTotal SLAvg SL Number CRAvg Time to AnsAvg Time to Abandon
7/22/2022AccountsANMFGeobanANMF5454381616059.19514149.55204.5
7/22/2022AccountsANMFGeobanANMF AMS33122000199.5326
7/22/2022AccountsANMFGeobanANMF Flexi33211000322.5678.5
7/22/2022AccountsANMFGeobanANMF Inter161616000826.6652180
7/22/2022AccountsANMFGeobanANMF RedDed2121129900044731715.5
7/22/2022AccountsANMFGeobanw6660000020310
7/22/2022BereavementGeobanBnkSav777000571.432550
7/22/2022BereavementGeobanBRVMNT Digital2220002100160
7/22/2022BereavementGeobanPOA333000266.671150
 
Upvote 0
Solution
That's perfect - thank you !

one question as I've never used power query before, why is there a call to 'removed columns' in the 'grouped rows' section? is it to compile the table without the columns ? I would have assumed that Table.removeColumns would handle that?
 
Upvote 0
That is just a reference to the prior step. In power query, you can reference prior steps which are out of sequence. This keeps the steps in order. In this case all the steps are in sequence. Click on the link in my signature to learn more about PQ
 
Upvote 0
Thank you for the explanation. I really appreciate your help ! I'll have a look at the links now.
 
Upvote 0
I've read the links in your signature and made some changes. The List.Average functions have been changed to List.Sum which is then divided by the sum of handled to give an average that better suits my needs.

I also want to merge all rows that have the value 'AccountsANMF' in the 'Product' column however I can only get it to merge them all using "Product_Type", each [Product_Type]{0}.

Should I be using the <> operator and specifying which Products not to merge?

Power Query:
= let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Time", "ResourcePlanning"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Date", "Product", "Channel"}, {{"Product_Type", each [Product_Type]{0},"Total Offered Net", each List.Sum([Offered_Net_Abn5Sec]), type number}, {"Total Offered Inc", each List.Sum([Offered_Inc_Abn5Sec]), type number}, {"Total Handled", each List.Sum([Handled]), type number}, {"Total Abandoned Net", each List.Sum([Abandoned_Net_Abn5Sec]), type number}, {"Total Abandoned Inc", each List.Sum([Abandoned_Inc_Abn5Sec]), type number}, {"Total Abn", each List.Sum([Abn5Sec]), type number}, {"Total SL", each List.Sum([SL_Number]), type number}, {"Avg SL Number CR", each List.Sum([SL_Number_CR]), type number}, {"Avg Time to Ans", each List.Sum([Time_to_Answer])/List.Sum([Handled]), type number}, {"Avg Time to Abandon", each List.Sum([Time_to_Abandon])/List.Sum([Handled]), type number}})
in
    #"Grouped Rows"
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top