Created
October 9, 2024 13:12
-
-
Save bjulius/e3d2ef512b8d0eab552ce6b326592200 to your computer and use it in GitHub Desktop.
Brian Julius Solution to Excel BI Challenge 561 - Maximum Profit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], | |
ToRows = Table.FromList( Table.ToRows( Source ), Splitter.SplitByNothing(), {"Trans"}), | |
BuySell = Table.AddColumn(ToRows, "BuySell", each [ | |
a = [Trans], | |
b = List.Transform(a, each List.PositionOf(a, _)), | |
c = List.Transform(a, each 1), | |
d = Table.FromColumns( {a, b, c}, {"Buy", "BuyPos", "B1"}), | |
e = Table.FromColumns( {a, b, c}, {"Sell", "SellPos", "S1"}), | |
f = Table.SelectRows( Table.Join( d, "B1", e, "S1" ), each [BuyPos] < [SellPos]), | |
g = Table.SelectColumns( f, {"Buy", "Sell"}), | |
h = Table.AddColumn( g, "Profit", each [Sell] - [Buy]), | |
i = List.Max(h[Profit]), | |
j = Table.PrefixColumns( Table.SelectRows( h, each [Profit] = i), "x") | |
][j]), | |
SelCols = Table.SelectColumns( BuySell, "BuySell"), | |
Expand = Table.ExpandTableColumn(SelCols, "BuySell", {"x.Buy", "x.Sell", "x.Profit"}, {"x.Buy", "x.Sell", "x.Profit"}), | |
AddNegClean = Table.AddColumn(Expand, "NClean", each [ | |
a = Expand, | |
b = Table.AddColumn(a, "Buy", each if [x.Profit] < 0 then "NP" else Text.From ([x.Buy])), | |
c = Table.AddColumn(b, "Sell", each if [x.Profit] < 0 then "NP" else Text.From( [x.Sell] )), | |
d = Table.AddColumn(c, "Profit", each if [x.Profit] < 0 then "NP" else Text.From ([x.Profit])), | |
e = Table.SelectColumns(d, {"Buy", "Sell", "Profit"}) | |
][e]), | |
Ans = Table.First( AddNegClean)[NClean] | |
in | |
Ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment