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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
fab196
Helper II
Helper II

how to find max value using m language

dataset

 

idsales amountindexcustom
120011
1300012
240021
220022
230023

 

result would be like

 

idsales amountindexcustomcum_sales
120011500
130012300
240021900
220022500
230023300

 

suppose index id 2 having custome id is 1,2,3 respec

if index number 2=max(customid ) then amount of for this row will remain same as of sales amount .then this amount will going to add in descding order for that index id 

for example index id 1 and 2 is there .for index id 2 having 3 is max cutom id 

so for the calculation for this row wil be 300 amount but for the other row of index id 2 would become 3+2+1=500 nad 500+400=900

how to  I achive this result using m languge or power query

 

please help

@amitchandak 

@ImkeF 

@themistoklis 

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @fab196 ,

 

Use below M codes:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMACShmAcqwMRM4aLGYHFjIAsE7CYEVydEVyvEYo6Y7iYsVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, #"sales amount" = _t, index = _t, custom = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"sales amount", Int64.Type}, {"index", Int64.Type}, {"custom", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"Max", each List.Max([custom]), type nullable number}, {"details", each _, type table [id=nullable number, sales amount=nullable number, index=nullable number, custom=nullable number]}}),
    #"Expanded details" = Table.ExpandTableColumn(#"Grouped Rows", "details", {"id", "sales amount", "index", "custom"}, {"details.id", "details.sales amount", "details.index", "details.custom"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded details",{"details.id"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"details.sales amount", "sales amount"}, {"details.index", "index"}, {"details.custom", "custom"}}),
    #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index.1", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom.1", each let 
value=#"Added Index"[sales amount]
in 
if [custom]=[Max] then [sales amount] else List.Sum(List.Range(value,[Index.1]-1,[Max]-[custom]+1))),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"index", "custom", "Index.1"})
in
    #"Removed Columns1"

And you will see:

vkellymsft_0-1624615544650.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

View solution in original post

2 REPLIES 2
v-kelly-msft
Community Support
Community Support

Hi @fab196 ,

 

Use below M codes:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMACShmAcqwMRM4aLGYHFjIAsE7CYEVydEVyvEYo6Y7iYsVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, #"sales amount" = _t, index = _t, custom = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"sales amount", Int64.Type}, {"index", Int64.Type}, {"custom", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"Max", each List.Max([custom]), type nullable number}, {"details", each _, type table [id=nullable number, sales amount=nullable number, index=nullable number, custom=nullable number]}}),
    #"Expanded details" = Table.ExpandTableColumn(#"Grouped Rows", "details", {"id", "sales amount", "index", "custom"}, {"details.id", "details.sales amount", "details.index", "details.custom"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded details",{"details.id"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"details.sales amount", "sales amount"}, {"details.index", "index"}, {"details.custom", "custom"}}),
    #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index.1", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom.1", each let 
value=#"Added Index"[sales amount]
in 
if [custom]=[Max] then [sales amount] else List.Sum(List.Range(value,[Index.1]-1,[Max]-[custom]+1))),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"index", "custom", "Index.1"})
in
    #"Removed Columns1"

And you will see:

vkellymsft_0-1624615544650.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

thanks Kelly ...your solution solve my probelm...thanks a lot.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors