Extracting records for max sales per product...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi Team,

I'm looking for help with Power Query to achieve the following...

Here's my data:

ProductTypeSales
AppleBag11
AppleBox4
PearBag7
BananaBunch8
OrangeBag12
OrangeBox4

<tbody>
</tbody>

I want to extract the records for each Product where the biggest Sales have taken place. The result should be as follows:

ProductTypeSales
AppleBag11
PearBag7
BananaBunch8
OrangeBag12

<tbody>
</tbody>

Can anyone help me with this? To reiterate: I would like M code for this if possible, not DAX.

Thanks,

Matty
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], // Set the table name on this line
    #"Grouped Rows" = Table.Group(Source, {"Product"}, {{"All Rows", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Max Row", each Table.Max([All Rows], "Sales")),
    #"Expanded Max Row" = Table.ExpandRecordColumn(#"Added Custom", "Max Row", {"Type", "Sales"}, {"Type", "Sales"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Max Row",{"Product", "Type", "Sales"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Type", type text}, {"Sales", type number}, {"Product", type text}})
in
    #"Changed Type"
 
Upvote 0
I am no PQ expert, to be sure. But this seems to have worked. My Table is named Table3. I used the Group feature with two aggregations, one of which was any function (I chose Median) applied to the text field "Type", which simply reports that text as text.

let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Type", type text}, {"Sales", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Product"}, {{"Type", each List.Median([Type]), type text}, {"Sales", each List.Max([Sales]), type number}})
in
#"Grouped Rows"
 
Upvote 0
Hi,

Next issue: to show where data inconsistencies exist.

Data as follows:

ProductTypeShopSales
AppleBagA11
AppleBoxB4
PearBagA7
BananaBunchA8
OrangeBagA12
OrangeBagB4

<tbody>
</tbody>

I want to show only those Products where the Type differs across different Shops. The resulting table should look as follows:

ProductTypeShop
AppleBagA
AppleBoxB

<tbody>
</tbody>

Only Apple shows on this table because all of the other Products have consistent Types (Apple has two different Types across different Shops). Pear, on the other hand, doesn't show because there is only one record. Orange also doesn't show because Bag is consistent in both Shops.

Hope this is clear and someone can help. Again, using M code if possible.

Cheers,

Matty
 
Upvote 0
Never mind - managed to work up a solution to the above issue by grouping up twice.

Cheers,

Matty
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,104
Members
448,548
Latest member
harryls

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