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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
marcelmunk
Frequent Visitor

Retrieve value from same row based on column name in column

Hi,

 

Pretty basic question (I believe) as I'm a beginner in Power BI and still on my basics on DAX.

 

I have a Table which has the Product Name and many properties

marcelmunk_0-1652451188715.png

 

I want to add a new column, 'Value', which contains the value of the porperty under 'Desired Property'

Desired Result:

marcelmunk_1-1652451308872.png

 

I guess I can do that with an 'if' function (I've seen an example in the community very similiar, but which had 2 properties only, solved with an 'if' statement)

 

As I already have the column name I want to extract the value from, there must be an easier way than pile up 20 'if'.

 

Thanks

 

1 ACCEPTED SOLUTION
NickOP
Frequent Visitor

Here's an alternative solution that reads the record itself:

= Table.AddColumn(Source, "Value", each Record.FieldOrDefault(_,[Desired Property], null))

View solution in original post

11 REPLIES 11
AlexisOlson
Super User
Super User

I'd recommend a much simpler and more efficient approach.

AlexisOlson_0-1652475090780.png

 

Full sample query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXIvSk3NA9LBGYlFBUA6Aoid83Pyi5RidaKVnIA8p5zSVJCC3Pz8kgwgIxKIQ1IrSkqLUsFqnIH8oNQUkL7SkpLMvHQgKwqIfVJLSlKBxsQCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Name" = _t, Color = _t, Texture = _t, Letter = _t, #"Desired Property" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Value", each Record.Field(_, [Desired Property]))
in
    #"Added Custom"
NickOP
Frequent Visitor

Here's an alternative solution that reads the record itself:

= Table.AddColumn(Source, "Value", each Record.FieldOrDefault(_,[Desired Property], null))
Vijay_A_Verma
Super User
Super User

Use the below formula

= try Table.Column(Source,[Desired Property]){List.PositionOf(List.Select(Table.ColumnNames(Source),(x)=>Text.Start(x,4)="Prop"),[Desired Property])} otherwise null

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXIvSk3NA9LBGYlFBUDaFYiNTYBEBBAHFOUXGAFppVidaCUnIMMppzQVpDg3P78kA8jwBWIzcyARCVVtCFPtDGQEpaYASefSkpLMvHQgywOILSyBRBRUtSlYdSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Name" = _t, Prop1 = _t, Prop2 = _t, Prop3 = _t, Prop4 = _t, Prop5 = _t, #"Desired Property" = _t, Column1 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Value", each try Table.Column(Source,[Desired Property]){List.PositionOf(List.Select(Table.ColumnNames(Source),(x)=>Text.Start(x,4)="Prop"),[Desired Property])} otherwise null)
in
    #"Added Custom"

Thanks for your answer, @Vijay_A_Verma 

 

The Prop1..PropN are just an example, unfortunatey. They actually have names such as Discipline, Nominal_Diameter, etc. It was just a way to simplify the understanding.

Is it possible to write a formula that would look up all the column names and match for the results?

 

Thanks again

In this case, use below formula

= try Table.Column(Source,[Desired Property]){List.PositionOf(Table.ColumnNames(Source),[Desired Property])} otherwise null

@Vijay_A_Verma , I'm getting null for the 3rd row. Should be a 'Z'. Can't figure out why:

marcelmunk_0-1652466847361.png

 

Looking closer, the result for first row, 'Smooth' property is on the second row, not first. And the result 'Green' should be for the first row, not second.
Somehow it's messing up rows and colums.

Then use this

I misunderstood your requirement. Use this

= Table.Column(Source,[Desired Property]){List.PositionOf(Source[Desired Property],[Desired Property])}

Now it looks great on the test query:

marcelmunk_0-1652469115360.png

 

But it still givs me an error on the actual data:

marcelmunk_1-1652469190718.png

The error msg is 'A cyclic reference was found during evaluation'

Let's try to do this through an Index column.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXIvSk3NA9LBGYlFBUDaFYiNTYBEBBAHFOUXGAFppVidaCUnIMMppzQVpDg3P78kA8jwBWIzcyARCVVtDFPtDGQEpaYASefSkpLMvHQgywOILSyBRBRUtSlYdSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Name" = _t, Prop1 = _t, Prop2 = _t, Prop3 = _t, Prop4 = _t, Prop5 = _t, #"Desired Property" = _t, Column1 = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Value", each Table.Column(#"Added Index",[Desired Property]){[Index]}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"

Apparentlu it's working. Will test with more data. Thanks a lot.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors