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

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.