Forum Discussion
Running Maximum
- 9 months ago
Transho
Apart from, Aliensx solution,
You can also achieve your result by using List.Accumulate function.. Below is the codelet Source = Table, ConvertingGroupingList = Table.Group( Source,{"Category"},{"AllRows",each _} )[AllRows], Result = Table.Combine( List.Transform( ConvertingGroupingList,(x) => Table.FromColumns( Table.ToColumns(x) & {List.Select( List.Accumulate( x[Value], {0}, (s,c) => s & {if List.Last(s) < c then c else List.Last(s) } ),each _ <>0 )}, Table.ColumnNames(Source) & {"Running Max"} ) ) ) in ResultAlso, you can use List.FirstN function for achieveing the result.. Please note List.FirstN is very slow function and you may face performance issue if you are delaing with large dataset.You can use List.Buffer to speed up the performance but, List.Generate and List.Accumulate always preferable. Below is the List.FirstN code
let Source = Table, Result = Table.Combine( Table.Group( Table,{"Category"},{"AllRows", each let IndexAdded = Table.AddIndexColumn(_,"Index",1,1), MaxColumnAdded = Table.AddColumn(IndexAdded,"Running Max", each List.Max( List.FirstN( List.Buffer( IndexAdded[Value] ),[Index])) ) in MaxColumnAdded } )[AllRows] )[[Date],[Category],[Value],[Running Max]] in ResultAttaching pbix file for your reference where you will find both solution.
Hope this will help you.
Regards,
sanalytics
Transho
Apart from, Aliensx solution,
You can also achieve your result by using List.Accumulate function.. Below is the code
let
Source = Table,
ConvertingGroupingList = Table.Group(
Source,{"Category"},{"AllRows",each _}
)[AllRows],
Result = Table.Combine(
List.Transform(
ConvertingGroupingList,(x) =>
Table.FromColumns(
Table.ToColumns(x) &
{List.Select(
List.Accumulate(
x[Value],
{0},
(s,c) => s & {if List.Last(s) < c then c else List.Last(s) }
),each _ <>0 )}, Table.ColumnNames(Source) & {"Running Max"}
) ) )
in
Result
Also, you can use List.FirstN function for achieveing the result.. Please note List.FirstN is very slow function and you may face performance issue if you are delaing with large dataset.You can use List.Buffer to speed up the performance but, List.Generate and List.Accumulate always preferable. Below is the List.FirstN code
let
Source = Table,
Result = Table.Combine(
Table.Group(
Table,{"Category"},{"AllRows", each
let
IndexAdded = Table.AddIndexColumn(_,"Index",1,1),
MaxColumnAdded = Table.AddColumn(IndexAdded,"Running Max", each List.Max( List.FirstN(
List.Buffer(
IndexAdded[Value] ),[Index])) )
in
MaxColumnAdded
}
)[AllRows]
)[[Date],[Category],[Value],[Running Max]]
in
Result
Attaching pbix file for your reference where you will find both solution.
Hope this will help you.
Regards,
sanalytics
Hi,
I used the List.Accumulate statement out of your code and it worked like a charm, but i added an additional step to read only one list of the generated lists at every record.
Thanks.