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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
AndreasFrank
New Member

Find a column name

Dear PowerBI Community!

 

I have a table like this:

 

LineitemOption 1Option 2Option 3Selected ValueFrom Option
a1411911Option 2
b31477Option 3
c75633Option 3
d3412734Option 1


For each lineitem I select a value from one of the 3 available options. So for lineitem "a" I selected value 11 which belongs to Option 2.

 

Now I'd like to add a column "From Option" that shows the name of the column from which the value was selected. Is there any formular that can search through the columns of a row to find a value and then return the name of the respective column?

 

Thank you!
Andreas

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @AndreasFrank 

 

Not sure if I made it too complicated

Vera_33_0-1637281045542.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTI0ARGGQMISwojViVZKAjKNYZLmYAwSTobyTM2g8sZg4RQQC2yMEVQBkBcbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lineitem = _t, #"Option 1" = _t, #"Option 2" = _t, #"Option 3" = _t, #"Selected Value" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "OptionFrom",  (x)=>
[a= Table.SelectRows(Source,each [Lineitem]=x[Lineitem]),
colName = List.Select( Table.ColumnNames(a), each Text.Contains(_,"Option")),
b=Table.Transpose( Table.DemoteHeaders( Table.SelectColumns(a, colName))),
c=Table.SelectRows(b,each [Column2]=x[Selected Value])[Column1]{0}?][c])
in
    #"Added Custom"

View solution in original post

3 REPLIES 3
Vera_33
Resident Rockstar
Resident Rockstar

Hi @AndreasFrank 

 

Not sure if I made it too complicated

Vera_33_0-1637281045542.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTI0ARGGQMISwojViVZKAjKNYZLmYAwSTobyTM2g8sZg4RQQC2yMEVQBkBcbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lineitem = _t, #"Option 1" = _t, #"Option 2" = _t, #"Option 3" = _t, #"Selected Value" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "OptionFrom",  (x)=>
[a= Table.SelectRows(Source,each [Lineitem]=x[Lineitem]),
colName = List.Select( Table.ColumnNames(a), each Text.Contains(_,"Option")),
b=Table.Transpose( Table.DemoteHeaders( Table.SelectColumns(a, colName))),
c=Table.SelectRows(b,each [Column2]=x[Selected Value])[Column1]{0}?][c])
in
    #"Added Custom"

Hi Vera!

 

It works, but I have no clue how 🙂

 

Thank you for the quick support!!

 

Best regards!
Andreas

Hi @AndreasFrank 

 

Here is another way, use a custom function called fnSwitch

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTI0ARGGQMISwojViVZKAjKNYZLmYAwSTobyTM2g8sZg4RQQC2yMEVQBkBcbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lineitem = _t, #"Option 1" = _t, #"Option 2" = _t, #"Option 3" = _t, #"Selected Value" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "OptionFrom",  (x)=>
[a= Table.SelectRows(Source,each [Lineitem]=x[Lineitem]),
b=List.Transform( Table.ToRecords(a),Record.ToTable){0},
fnSwitch = (input as text) => [
        values =List.Zip ({
        b[Name],b[Value]
        }),
        Result = List.First(List.Select(values, each _{1}=input)){0}][Result],
c= fnSwitch(x[Selected Value])][c])
in
    #"Added Custom"

 

Read his blog to understand M better

Power Query M Primer (part 1): Introduction, Simple Expressions & let | Ben Gribaudo

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.