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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Medmbchr
Helper IV
Helper IV

Return column name with max value (for each row)

Hi,

 

I have a set of 4 columns with different values, and I need to put a 5th column in which I get the name of the column with the maximum value of the 4 columns (for each row).

 

The thing is that I need to do it in DAX, not PowerQuery (hence no transpose or as such).

 

Is there any way to do it?

 

Thanks

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You could try

Col with max value =
VAR SummaryTable =
    UNION (
        ROW ( "Column name", "Column1", "Column Value", 'Table'[Column1] ),
        ROW ( "Column name", "Column2", "Column Value", 'Table'[Column2] ),
        ROW ( "Column name", "Column3", "Column Value", 'Table'[Column3] ),
        ROW ( "Column name", "Column4", "Column Value", 'Table'[Column4] )
    )
VAR Result =
    CONCATENATEX ( TOPN ( 1, SummaryTable, [Column Value] ), [Column name], ", " )
RETURN
    Result

In the event of a tie it will return a comma separated list of the column names with the max value

View solution in original post

4 REPLIES 4
johnt75
Super User
Super User

You could try

Col with max value =
VAR SummaryTable =
    UNION (
        ROW ( "Column name", "Column1", "Column Value", 'Table'[Column1] ),
        ROW ( "Column name", "Column2", "Column Value", 'Table'[Column2] ),
        ROW ( "Column name", "Column3", "Column Value", 'Table'[Column3] ),
        ROW ( "Column name", "Column4", "Column Value", 'Table'[Column4] )
    )
VAR Result =
    CONCATENATEX ( TOPN ( 1, SummaryTable, [Column Value] ), [Column name], ", " )
RETURN
    Result

In the event of a tie it will return a comma separated list of the column names with the max value

seriously this is great, life saviour 👍

Brilliant, thanks! In the event of 2 or more max values, I would like that the last column to be taken into consideration, any way to do that?

you could add another column to act as the tie breaker

Col with max value =
VAR SummaryTable =
    UNION (
        ROW (
            "Column name", "Column1",
            "Column Value", 'Table'[Column1],
            "Tie breaker", 1
        ),
        ROW (
            "Column name", "Column2",
            "Column Value", 'Table'[Column2],
            "Tie breaker", 2
        ),
        ROW (
            "Column name", "Column3",
            "Column Value", 'Table'[Column3],
            "Tie breaker", 3
        ),
        ROW (
            "Column name", "Column4",
            "Column Value", 'Table'[Column4],
            "Tie breaker", 4
        )
    )
VAR Result =
    CONCATENATEX (
        TOPN ( 1, SummaryTable, [Column Value], DESC, [Tie breaker], DESC ),
        [Column name],
        ", "
    )
RETURN
    Result

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.