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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors