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