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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
PC2790
Community Champion
Community Champion

Insert new columns with value as 0 in case they don't exist already

Hello,

 

I am dealing with a situation of loading the data from multiple columns which may or may not be available everytime.

This is the reason the further transformation steps are failing in case if there is even a single column unavailable.

 

So as a workaround, I am thinking of adding the column as a dummy column with values as '0'

For example I have columns names as 6 hope- H1,H2,H3,H4,H5 and H6. 

Can anyone please help me with the power query with the logic to check if any of the columns with the above mentioned column names is missing, add a new column with value as 0.

 

Thanks in advance for your help

2 ACCEPTED SOLUTIONS
Jakinta
Solution Sage
Solution Sage

This might help...

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HYvJDQAgEAJ74e1DvK1ls/23IfgYQiYQAaKgiS4GsgSm2hJbnG+uGqvDa7Yv6QeHww8uZD4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [H1 = _t, H2 = _t, H3 = _t, H6 = _t]),
    Cols = {"H1","H2","H3","H4","H5","H6"},
    Missing = List.Difference(Cols, Table.ColumnNames(Source)),
    AllRows = List.Transform( Table.ToRows(Source), each _ & List.Repeat({0}, List.Count(Missing))),
    Table = Table.FromRows( AllRows, Table.ColumnNames(Source) & Missing),
    Sorted = Table.ReorderColumns( Table, List.Sort( Table.ColumnNames( Table ), Order.Ascending ))
in
    Sorted

View solution in original post

Vera_33
Resident Rockstar
Resident Rockstar

Hi @PC2790 

 

Another way, based on @Jakinta solution, if you can use null, stop at Custom,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HYvJDQAgEAJ74e1DvK1ls/23IfgYQiYQAaKgiS4GsgSm2hJbnG+uGqvDa7Yv6QeHww8uZD4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [H1 = _t, H2 = _t, H3 = _t, H6 = _t]),
    Cols = {"H1","H2","H3","H4","H5","H6"},
    Missing = List.Difference(Cols, Table.ColumnNames(Source)),
    Custom = Table.Combine({ #table(Cols,{}),Source}),
    #"Replaced Value" = Table.ReplaceValue(Custom,null,0,Replacer.ReplaceValue,Missing)
in
    #"Replaced Value"

View solution in original post

5 REPLIES 5
Vera_33
Resident Rockstar
Resident Rockstar

Hi @PC2790 

 

Another way, based on @Jakinta solution, if you can use null, stop at Custom,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HYvJDQAgEAJ74e1DvK1ls/23IfgYQiYQAaKgiS4GsgSm2hJbnG+uGqvDa7Yv6QeHww8uZD4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [H1 = _t, H2 = _t, H3 = _t, H6 = _t]),
    Cols = {"H1","H2","H3","H4","H5","H6"},
    Missing = List.Difference(Cols, Table.ColumnNames(Source)),
    Custom = Table.Combine({ #table(Cols,{}),Source}),
    #"Replaced Value" = Table.ReplaceValue(Custom,null,0,Replacer.ReplaceValue,Missing)
in
    #"Replaced Value"
PC2790
Community Champion
Community Champion

Thanks @Vera_33 

I tried this. The only problem I am facing here is that it is not replacing all null in all the missing columns.

It is just doing only for the first column in the missing list.

I am not sure what I am doing wrong here.

Vera_33
Resident Rockstar
Resident Rockstar

Hi @PC2790 

 

I was using the sample from @Jakinta , H4 and H5 are missing.

Cols contains all the columns you want, Missing to find the missing columns. Mine was creating a table with full Cols then append with your current table which contains missing columns

Vera_33_0-1628575482139.png

 

Jakinta
Solution Sage
Solution Sage

This might help...

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HYvJDQAgEAJ74e1DvK1ls/23IfgYQiYQAaKgiS4GsgSm2hJbnG+uGqvDa7Yv6QeHww8uZD4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [H1 = _t, H2 = _t, H3 = _t, H6 = _t]),
    Cols = {"H1","H2","H3","H4","H5","H6"},
    Missing = List.Difference(Cols, Table.ColumnNames(Source)),
    AllRows = List.Transform( Table.ToRows(Source), each _ & List.Repeat({0}, List.Count(Missing))),
    Table = Table.FromRows( AllRows, Table.ColumnNames(Source) & Missing),
    Sorted = Table.ReorderColumns( Table, List.Sort( Table.ColumnNames( Table ), Order.Ascending ))
in
    Sorted
PC2790
Community Champion
Community Champion

Thankyou so much.

It works like magic.

Just one thing more, Is there any way I can maintain the list as a separate query(as in Blank Query) and reference in my table for as you are doing it in the below step?

Missing = List.Difference(Cols, Table.ColumnNames(Source)),

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.

Top Kudoed Authors