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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Take the column with maximum value based on another value

I have a table like this:

firstans1secondans2thirdans3max
150yes170 yes190no170
190yes    190
110no    0
130no110yes  110
130yes190no170yes170

It's actually a Gabor-Granger analysis survey, stored like this, queue of columns, price, yes/no, second price, yes/no etc. I need to get max column, strugling couple days already, maybe someone could help? Max column take maximum price with according column ans with yes. The price range is 110-130-150-170-190

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , A solution in Power Query, New custom column

 

 

 

let 
_a = Table.Max(Table.SelectRows( Table.FromColumns({
   {[first],[second],[third]},
   {[ans1],[ans2],[ans3]}
}, {"Col1", "Col2"}), each [Col2] = "yes"), "Col1"),
_b= try Record.Field(_a, "Col1") otherwise 0
in 
_b

 

 

 

Dax New Column

 

Column = Maxx(filter(union(row("Col1",[first],"Col2", [ans1]), row("Col1",[second],"Col2", [ans2]),row("Col1",[third],"Col2", [ans3])),[Col2]= "yes"),[Col1])+0
Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

Here's another Power Query answer:

AlexisOlson_0-1665872457477.png

 

 

List.Max(
    List.Transform(
        {
            { [first],  [ans1] },
            { [second], [ans2] },
            { [third],  [ans3] }
        },
        each if List.Last(_) = "yes"
             then List.First(_) else null // or 0 if you prefer
    )
)

 

 

You can do nearly the same thing in DAX:

 

MAXX (
    {
        ( [first],  [ans1] ),
        ( [second], [ans2] ),
        ( [third],  [ans3] )
    },
    IF ( [Value2] = "yes", [Value1], 0 )
)

 

amitchandak
Super User
Super User

@Anonymous , A solution in Power Query, New custom column

 

 

 

let 
_a = Table.Max(Table.SelectRows( Table.FromColumns({
   {[first],[second],[third]},
   {[ans1],[ans2],[ans3]}
}, {"Col1", "Col2"}), each [Col2] = "yes"), "Col1"),
_b= try Record.Field(_a, "Col1") otherwise 0
in 
_b

 

 

 

Dax New Column

 

Column = Maxx(filter(union(row("Col1",[first],"Col2", [ans1]), row("Col1",[second],"Col2", [ans2]),row("Col1",[third],"Col2", [ans3])),[Col2]= "yes"),[Col1])+0
Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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