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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Nasif_Azam
Super User
Super User

Replace null values of one column to another column.

Dear Contributors, 
I hope you guys are well. I need a help in power query to Replace null values of one column to another column.

 

I have 2 column namely OldKey and NewKey. 

OldKey has not any nulls but on the other hand NewKey has some nulls. So I would like to replace the NewKey null values by using OldKey values. 

 

Sample Data:

OldKey NewKey
A A
B

B

C null
D null
E

null

 

 

Expected Output:

OldKey NewKey FinalKey
A A A
B B B
C null C
D null D
E

null

E

 

 

The idea is,

if NewKey = null then replace the null using OldKey

if NewKey != null then keep the NewKey values as it is.

 

Please suggest some power query or m code or custom column or replace value.

 

Thanks in advanced.

 

Best Regards,

Nasif Azam

 



Did I answer your question?
If so, mark my post as a solution!
Also consider helping someone else in the forums!

Proud to be a Super User!


LinkedIn
2 ACCEPTED SOLUTIONS
ronrsnfld
Super User
Super User

@Nasif_Azam You write you want to "Replace" but your example shows you creating an additional column.

 

If you truly want to "Replace" the NewKey null then try the code below using Table.ReplaceValue function:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSAeJYnWglJyDLCcxyBrLySnNywBwXZI4rnBMLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OldKey = _t, NewKey = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"OldKey", type text}, {"NewKey", type text}}),
    #"Replace Null" = Table.ReplaceValue(
        #"Changed Type",
        each [NewKey],
        each [OldKey],

//if null is a text string, then use as below
//if null is a true null, then remove the quote marks below
        (x,y,z) as text=>if y= "null" then z else x,
        {"NewKey"}
    )
in
    #"Replace Null"

 

Note that your example shows null as a text string. The code takes that into account. However, if the null is really a true null, then remove the quote marks around null in the Replace.Value function.

 

Before:

ronrsnfld_2-1759598939866.png

After

ronrsnfld_3-1759598970524.png

 

 

 

View solution in original post

Royel
Solution Sage
Solution Sage

Hi @Nasif_Azam 

check this out 

let
    Source = Table.FromRows({
        {"A", "A"},
        {"B", "B"},
        {"C", null},
        {"D", null},
        {"E", null}
    }, {"OldKey", "NewKey"}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"OldKey", type text}, {"NewKey", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "FinalKey", each [NewKey] ?? [OldKey])
in
    #"Added Custom"

 

Results: 

Royel_0-1759604467976.png

 

Find this helpful? ✔ Give a Kudo • Mark as Solution – help others too!

 

View solution in original post

5 REPLIES 5
raisurrahman
Advocate I
Advocate I

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"OldKey", type text}, {"NewKey", type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "FinalKey", each if [NewKey] = null then [NewKey] else [OldKey])
in
#"Added Conditional Column"

Royel
Solution Sage
Solution Sage

Hi @Nasif_Azam 

check this out 

let
    Source = Table.FromRows({
        {"A", "A"},
        {"B", "B"},
        {"C", null},
        {"D", null},
        {"E", null}
    }, {"OldKey", "NewKey"}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"OldKey", type text}, {"NewKey", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "FinalKey", each [NewKey] ?? [OldKey])
in
    #"Added Custom"

 

Results: 

Royel_0-1759604467976.png

 

Find this helpful? ✔ Give a Kudo • Mark as Solution – help others too!

 

ronrsnfld
Super User
Super User

@Nasif_Azam You write you want to "Replace" but your example shows you creating an additional column.

 

If you truly want to "Replace" the NewKey null then try the code below using Table.ReplaceValue function:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSAeJYnWglJyDLCcxyBrLySnNywBwXZI4rnBMLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OldKey = _t, NewKey = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"OldKey", type text}, {"NewKey", type text}}),
    #"Replace Null" = Table.ReplaceValue(
        #"Changed Type",
        each [NewKey],
        each [OldKey],

//if null is a text string, then use as below
//if null is a true null, then remove the quote marks below
        (x,y,z) as text=>if y= "null" then z else x,
        {"NewKey"}
    )
in
    #"Replace Null"

 

Note that your example shows null as a text string. The code takes that into account. However, if the null is really a true null, then remove the quote marks around null in the Replace.Value function.

 

Before:

ronrsnfld_2-1759598939866.png

After

ronrsnfld_3-1759598970524.png

 

 

 

SundarRaj
Super User
Super User

Hi @Nasif_Azam,
What you can do is use Table.TransformRows instead of creating a custom column. Sure, that works but it adds an additional step. Using Table.TransformRows gives you the desired solution. I'll attach the image of the code and file as well for your reference.

SundarRaj_0-1759585317557.png

Thanks,

Sundar Rajagopalan
GeraldGEmerick
Impactful Individual
Impactful Individual

@Nasif_Azam You can create a custom column like the following:

= if [NewKey] = null then [OldKey] else [NewKey]

GeraldGEmerick_0-1759584178307.png

 

Or you could just use the OldKey column?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.