Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Transho
Frequent Visitor

Running Maximum

I have a table with sorted records by Category and Date.

Transho_0-1760858504410.png

 

I know how to group and index by Category but simply need a power query code that inserts a column that gets the running maximum per group so the results will be as such.

Transho_1-1760858526547.png

 

Thanks for your help.

1 ACCEPTED SOLUTION
sanalytics
Super User
Super User

@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

View solution in original post

4 REPLIES 4
sanalytics
Super User
Super User

@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.

 

AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    rows = List.Buffer(Table.ToList(Source, each _)),
    iterate = List.Generate(
        () => [i = 0, r = rows{0}, max = r{2}],
        (x) => x[i] < List.Count(rows),
        (x) => [
            i = x[i] + 1, 
            r = rows{i}, 
            max = if r{1} <> x[r]{1} or r{2} > x[max] then r{2} else x[max]
        ],
        (x) => x[r] & {x[max]}
    ),
    tbl = Table.FromList(iterate, each _, Table.ColumnNames(Source) & {"Running Max"})
in
    tbl

Hi,

Thanks for your answer and the solution you have provided.

I used to write such codes in SAS but since i am new to power query, this code is a bit complicatd to understand.

During my investigation, i realized that if i grouped then indexed each group, i will be able to use the 

List.Accumulate function with an iterator statement.

But i got stuck in outputing all the records per table.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.