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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
spencerhoyle
Frequent Visitor

Power Query - creating key column based on column names and row values

I am looking to creating a "key" column with the only certain column names and values for those not null.

 

An examples is....

 

|        A        |       B        |        C        | ->>> |    key    |

|        1        |     null       |        3        | ->>> "A=1,C=3"

|      null      |       2         |       null     | ->>> "B=2"

|        2        |       4         |        5        | ->>> "A=2,B=4,C=5"

 

 

Is there a way of extracting only column names where value is not null and creating a key column like the one above?

 

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

HI @spencerhoyle ,

In my opinion ,you need to create three column ,then combine:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQKhWJ1oEG2ExAYiYzDbECoB5MUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t, c = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"a", Int64.Type}, {"b", Int64.Type}, {"c", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom1", each if [a] is null and [b] is null  then "" 
else if  [a] <>null and [b] is null  then "a="&Number.ToText([a])   else   "a="&Number.ToText([a])&","),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", each if [b] is null and [c] is null  then "" 
else if  [b] <>null and [c] is null  then "b="&Number.ToText([b])   else   "b="&Number.ToText([b])&","),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom3", each if [c] is null then "" else   "c="&Number.ToText([c])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Final", each [Custom1]&[Custom2]&[Custom3]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Custom1", "Custom2", "Custom3"})
in
    #"Removed Columns"

vluwangmsft_0-1662713802238.png

 


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,
Lucien

View solution in original post

1 REPLY 1
v-luwang-msft
Community Support
Community Support

HI @spencerhoyle ,

In my opinion ,you need to create three column ,then combine:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQKhWJ1oEG2ExAYiYzDbECoB5MUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t, c = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"a", Int64.Type}, {"b", Int64.Type}, {"c", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom1", each if [a] is null and [b] is null  then "" 
else if  [a] <>null and [b] is null  then "a="&Number.ToText([a])   else   "a="&Number.ToText([a])&","),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", each if [b] is null and [c] is null  then "" 
else if  [b] <>null and [c] is null  then "b="&Number.ToText([b])   else   "b="&Number.ToText([b])&","),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom3", each if [c] is null then "" else   "c="&Number.ToText([c])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Final", each [Custom1]&[Custom2]&[Custom3]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Custom1", "Custom2", "Custom3"})
in
    #"Removed Columns"

vluwangmsft_0-1662713802238.png

 


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,
Lucien

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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