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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
Syndicate_Admin
Administrator
Administrator

Column with the maximum value of another according to condition

Hello!!

I need help with a calculation, I have a table with two fields, the first is an id that can be repeated in other rows, the second has the total amount that can be different in each row. What I need is to create a new field that rescues the maximum value for each ID and leaves me at zero those with the minimum value.

Example:

IDAmountTotal
A1000
A1500
A170170
B500
B8080
C2000
C250250
D1010

I hope you understand, thank you very much.

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Syndicate_Admin 

 

If you need a column in M

 

Vera_33_0-1617246956289.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MFCK1YGyTZHY5hC2E5BtimBaQJjOQKaRARIbqsQFbKJSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Amount", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Total_m", each List.Max([Amount]), type nullable number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, #"Grouped Rows", {"ID"}, "Grouped Rows", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries", "Total_m", each if [Amount]=[Grouped Rows][Total_m]{0} then [Grouped Rows][Total_m]{0} else 0)
in
    #"Added Custom"

 

Or DAX column

Vera_33_1-1617247029862.png

Total = 
VAR CurID = 'Table'[ID]
VAR MaxAmount = MAXX(FILTER('Table','Table'[ID]=CurID),'Table'[Amount])
RETURN
IF('Table'[Amount]=MaxAmount,'Table'[Amount],0)

 

View solution in original post

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

Nice day

Please your help, what would happen if the total amount is repeated in the same id the idea is to leave a single result

Edwincv_0-1701868455191.png

Thank you for your help

v-angzheng-msft
Community Support
Community Support

Hi @Syndicate_Admin ,

Try to create a measure or calculate column

Measure:

Measure =
VAR maxAmoout = CALCULATE(
    MAX('Table'[Amount]),FILTER(ALL('Table'),'Table'[ID]= MAX('Table'[ID]))
)
RETURN
IF(maxAmoout = SUM('Table'[Amount]),maxAmoout,0)

Calculate column:

Column =
VAR MaxAmount =
    CALCULATE (
        MAX ( 'Table'[Amount] ),
        FILTER ( ALL ( 'Table' ), 'Table'[ID] = EARLIER ( 'Table'[ID] ) )
    )
RETURN
    IF ( 'Table'[Amount] = MaxAmount, 'Table'[Amount], 0 )

Sample data:

v-angzheng-msft_0-1617355720632.png

Result:

v-angzheng-msft_1-1617355720775.jpeg

 

Is this the result you want? Hope this is useful to you

Please feel free to let me know If you have further questions

 

Best Regards,
Community Support Team _ Zeon Zheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Jihwan_Kim
Super User
Super User

Hi, 

Please try the below measure.

 

Maximum for each ID =
VAR currentID =
SELECTEDVALUE ( 'Table'[ID] )
VAR groupbytable =
FILTER (
GROUPBY (
ALL ( 'Table' ),
'Table'[ID],
"@maxamount", MAXX ( CURRENTGROUP (), 'Table'[Amount] )
),
'Table'[ID] = currentID
)
RETURN
IF (
SELECTEDVALUE ( 'Table'[Amount] ) = SUMX ( groupbytable, [@maxamount] ),
SUMX ( groupbytable, [@maxamount] ),
0
)

 

Picture3.png

Hi, My name is Jihwan Kim.

If this post helps, then please consider accept it as the solution to help other members find it faster.

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Vera_33
Resident Rockstar
Resident Rockstar

Hi @Syndicate_Admin 

 

If you need a column in M

 

Vera_33_0-1617246956289.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MFCK1YGyTZHY5hC2E5BtimBaQJjOQKaRARIbqsQFbKJSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Amount", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Total_m", each List.Max([Amount]), type nullable number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, #"Grouped Rows", {"ID"}, "Grouped Rows", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries", "Total_m", each if [Amount]=[Grouped Rows][Total_m]{0} then [Grouped Rows][Total_m]{0} else 0)
in
    #"Added Custom"

 

Or DAX column

Vera_33_1-1617247029862.png

Total = 
VAR CurID = 'Table'[ID]
VAR MaxAmount = MAXX(FILTER('Table','Table'[ID]=CurID),'Table'[Amount])
RETURN
IF('Table'[Amount]=MaxAmount,'Table'[Amount],0)

 

Thanks a lot!!! I used the DAX column and it worked perfectly.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.