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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

How to Look up for values in another column based on current column value

Hi all,

 

I'm learning my way around Power Query. Hoping someone can help me with this.

 

I have a table that is similar to below.

Col1Col2_linkCol3_linkCol4_linkCol5_link
 URL1URL2URL3URL4
Col2URL5URL6URL7URL8
Col4URL9URL10URL11URL12
 URL13URL14URL15URL16

 

In Col1, the values are either prefix of other column headers or blank. I want to know how to look up for the actual URL based on the column header prefix and leave the blanks as blank so it becomes

Col1Col2_linkCol3_linkCol4_linkCol5_link
 URL1URL2URL3URL4
URL5URL5URL6URL7URL8
URL11URL9URL10URL111URL12
 URL13URL14URL15URL16

 

I tried the following but it's not working

= Table.ReplaceValue(#"PreviousStep", each if [Col1] <> "" then [[Col1]&"_link"] else [Col1])

 

1 ACCEPTED SOLUTION
v-cazheng-msft
Community Support
Community Support

Hi, @Anonymous 

 

You can paste the following M code to Advanced Editor to get the table you want.

 

let

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRCg3yMYRQRhDKGEKZKMXqRCs55+dAhU0hlBmEModQFjBFJhC+JdRAAygNNdnQCKwOyoFaYAjVYwg12dBMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2_link = _t, Col3_link = _t, Col4_link = _t, Col5_link = _t]),

    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2_link", type text}, {"Col3_link", type text}, {"Col4_link", type text}, {"Col5_link", type text}}),

    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"Col2_link", "Col3_link", "Col4_link", "Col5_link"}, "Attribute", "Value"),

    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom", each if Text.Contains([Attribute],[Col1]) and [Col1]<>"" then [Value] else ""),

    Custom1 = Table.TransformRows(#"Added Custom",each

   let

   t=Text.Combine( Table.SelectRows(#"Added Custom",(x)=>x[Col1]=_[Col1])[Custom])

   in

   if _[Col1]<>"" and _[Custom]=""

   then

   [Col1=_[Col1],Attribute=_[Attribute],Value=_[Value],Custom=t]

   else

   _

),

    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Col1", "Attribute", "Value", "Custom"}, {"Col1", "Attribute", "Value", "Custom"}),

    #"Removed Columns" = Table.RemoveColumns(#"Expanded Column1",{"Col1"}),

    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),

    #"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 4), Int64.Type),

    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),

    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Attribute]), "Attribute", "Value"),

    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Integer-Division"}),

    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns2",{{"Custom", "Col1"}})

in

    #"Renamed Columns"

 

The result looks like this:

v-cazheng-msft_0-1611195224656.png

Here is the sample.

 

Best Regards,

Caiyun Zheng

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-cazheng-msft
Community Support
Community Support

Hi, @Anonymous 

 

You can paste the following M code to Advanced Editor to get the table you want.

 

let

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRCg3yMYRQRhDKGEKZKMXqRCs55+dAhU0hlBmEModQFjBFJhC+JdRAAygNNdnQCKwOyoFaYAjVYwg12dBMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2_link = _t, Col3_link = _t, Col4_link = _t, Col5_link = _t]),

    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2_link", type text}, {"Col3_link", type text}, {"Col4_link", type text}, {"Col5_link", type text}}),

    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"Col2_link", "Col3_link", "Col4_link", "Col5_link"}, "Attribute", "Value"),

    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom", each if Text.Contains([Attribute],[Col1]) and [Col1]<>"" then [Value] else ""),

    Custom1 = Table.TransformRows(#"Added Custom",each

   let

   t=Text.Combine( Table.SelectRows(#"Added Custom",(x)=>x[Col1]=_[Col1])[Custom])

   in

   if _[Col1]<>"" and _[Custom]=""

   then

   [Col1=_[Col1],Attribute=_[Attribute],Value=_[Value],Custom=t]

   else

   _

),

    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Col1", "Attribute", "Value", "Custom"}, {"Col1", "Attribute", "Value", "Custom"}),

    #"Removed Columns" = Table.RemoveColumns(#"Expanded Column1",{"Col1"}),

    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),

    #"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 4), Int64.Type),

    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),

    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Attribute]), "Attribute", "Value"),

    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Integer-Division"}),

    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns2",{{"Custom", "Col1"}})

in

    #"Renamed Columns"

 

The result looks like this:

v-cazheng-msft_0-1611195224656.png

Here is the sample.

 

Best Regards,

Caiyun Zheng

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

FarhanAhmed
Community Champion
Community Champion

One way to do this is to create a calculated column in your Power Query 

 

 

ConditionalColumn.png







Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!

Proud to be a Super User!




Anonymous
Not applicable

Thanks @FarhanAhmed, the actual table I have do have more than 30 columns. I wonder if there is an easier way to do this. 

 

In Excel, it would just be index(A:Z,row(A2),match(A2&"_link", 1:1,0))

Helpful resources

Announcements
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!

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.