Aggregating certain rows with power query?

jplank

Board Regular
Joined
Sep 19, 2012
Messages
53
I'm new to Power Query and have what might be a basic question. I have my data in the following format:

Company Sales
Company A100
Company B (division 1)200
Company B (division 2)300
Company B (division 3)400
Company C500
Company D600
Company E (division 1)100
Company E (division 2)200

Is there a way to transform my data so that Company E divisions 1 and 2 are aggregated into a Company E row and similarly with Company B divisions 1, 2 and 3?

I've cross-posted this question here:

https://www.reddit.com/r/excel/comments/emsd8e
 

jplank

Board Regular
Joined
Sep 19, 2012
Messages
53
#1 is what I'm looking for. The actual information about divisions is not relevant for my analysis.
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
3,829
#1 is what I'm looking for
sure
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Company", type text}, {"Sales", Int64.Type}}),
    ETBD = Table.TransformColumns(Type, {{"Company", each Text.BeforeDelimiter(_, " ("), type text}}),
    Group = Table.Group(ETBD, {"Company"}, {{"Sales", each List.Sum([Sales]), type number}})
in
    Group
 

jplank

Board Regular
Joined
Sep 19, 2012
Messages
53
Probably a dumb question, but where do I put that code? And how to I execute it?
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
3,829
Select you range and use Ctrl+T
Be sure the name of the new table is the same as in the M-code (here: Table1)
select any cell in the new Table and:
- Data - From Table
- Advanced Editor
- replace whole code there with copied from the post
- Done
- Close&Load
 

Forum statistics

Threads
1,082,354
Messages
5,364,887
Members
400,814
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top