Identify column using Max result

Comfy

Well-known Member
Joined
Dec 21, 2009
Messages
3,386
Hi Boardies,

Here's a sample of my source data.

Power Query:
let
    Source = #table({"Invoice Ref", "Organistion Code", "Account", "High Level Cat", "Amount", "Abs"},{{"A12345","Sub1",88888,"I&E",120,120},{"A12345","Sub2",33333,"I&E",-100,100},{"A12346","Sub2",77777,"I&E",240,240},{"A12346","Sub3",33333,"I&E",-200,200}}),
    #"Grouped Rows" = Table.Group(Source, {"Invoice Ref", "Organistion Code", "High Level Cat"}, {{"Data", each _, type table}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Organistion Code"]), "Organistion Code", "Data")
in
    #"Pivoted Column"

I wish to add two additional columns (Account and Amount)

Account - will return the Account from the Sub1, Sub2, Sub3 columns where the Absolute amount in those tables is the maximum. So row 1 will return 88888 from [Sub1] and row 2 will return 77777 from [Sub2].
Amount - will return the sum of [Amount] in each of the tables. So Row 1 will return 20 and row 2 will return 40.

I cannot hardcode the column names in the solution as there are more and I want to make it flexible for when others are added or removed.
I can achieve this by using List.RemoveItems and Table.ColumnNames but am struggling with how to use that for the two columns above.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Well I managed to rehash some code from elsewhere in the project.

It's not pretty but I get the results I need:

Power Query:
let
    Source = #table({"Invoice Ref", "Organistion Code", "Account", "High Level Cat", "Amount", "Abs"},{{"A12345","Sub1",88888,"I&E",120,120},{"A12345","Sub2",33333,"I&E",-100,100},{"A12346","Sub1",77777,"I&E",240,240},{"A12346","Sub3",33333,"I&E",-200,200}}),
    #"Grouped Rows" = Table.Group(Source, {"Invoice Ref", "Organistion Code", "High Level Cat"}, {{"Data", each _, type table}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Organistion Code"]), "Organistion Code", "Data"),
    AddDiff = Table.AddColumn(#"Pivoted Column", "Correction", each List.Sum(Table.ExpandTableColumn(Table.SelectRows(Record.ToTable(Record.RemoveFields(_, {"Invoice Ref", "High Level Cat"})), each [Value] <> null), "Value", {"Amount"}, {"Amount"})[Amount]) *- 1, Currency.Type),
    IDSub = Table.AddColumn(AddDiff, "GroupedSubs", each Table.ExpandTableColumn(Table.SelectRows(Record.ToTable(Record.RemoveFields(_, {"Invoice Ref", "High Level Cat", "Correction"})), each [Value] <> null), "Value", {"Account", "Abs"}, {"Account", "Abs"}), type table),
    AddMax = Table.AddColumn(IDSub, "Max", each List.Max(_[GroupedSubs][Abs])),
    AddAccount = Table.AddColumn(AddMax, "Account", each List.First(Table.SelectRows([GroupedSubs], (Magic) =>[Max]=Magic[Abs])[Account])),
    AddSub = Table.AddColumn(AddAccount, "Name", each List.First(Table.SelectRows([GroupedSubs], (Magic) =>[Max]=Magic[Abs])[Name])),
    #"Removed Other Columns" = Table.SelectColumns(AddSub,{"Name", "Account", "Correction"})
in
    #"Removed Other Columns"
 
Upvote 0
Solution

Forum statistics

Threads
1,226,729
Messages
6,192,696
Members
453,747
Latest member
tylerhyatt04

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