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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
G_Whit-UK
Helper II
Helper II

Create column in table if it doesn't exist

Hi

 

I'm working within Power Query to analyse a system output file which lists reconciliation items between two systems.  When the recon doesn't have any exceptions, the output file is simply the column headers.  During the steps within Power BI whereby I merge the various system files together (to build trends, etc) I obviously have to remove the excess file headers - thus on days where there are no exceptions, there are no records on the table.

 

Later in the process, I pivot the table such that I have a column for each of the systems (2), however on days where there are no breaks, the subsequent steps fail as the pivot is missing one / both of the systems - due to them not having any records.  I therefore want to have some logic introduced after the pivot step to check if a column exists.  If it does, then thats great and the logic doesn't have to do anything.  If the column is missing, then the logic needs to add the missing column (column values can be blank or null).

 

In order to get the above to work, I was thinking of using an if statement with the Table.HasColumn function, followed by Table.AddColumn where the result was false.  When true, do nothing.  However I've not been able to get this logic to work.

 

The structure of the pivot table (which is the step just before I'd like to apply the above logic) is as follows (on days where both source files have records):

Record IDSource System 1 ValueSource System 2 Value
Record 111.5
Record 221.9
Record 31.71.6

 

On days where one of the source systems didn't report any exceptions, the pivot would look as follows:

Record IDSource System 1 Value
Record 11
Record 22
Record 32.5

 

Hence if the output was per the 2nd table above, I'l like to introduce a step whereby the logic creates the missing column.

 

Thanks in advance for any guidance which you might be able to provide.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @G_Whit-UK ,

List all the required columns in advance and refer to the steps below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkpNzi9KUTBU0lEKzi8tSk5VCK4sLknNVTBUCEvMKU0FihsqxerAFRrhUWiErNAYn4l65kqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Record ID" = _t, Attribute = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}}),
    #"Source System List" = {"Source System 1 Value","Source System 2 Value"},
    Custom1 = Table.Group(#"Changed Type",{"Record ID"}, List.Transform(#"Source System List", (y) => { y, (x) => try Table.SelectRows(x, each [Attribute] = y)[Value]{0} otherwise null }))
in
    Custom1

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @G_Whit-UK ,

List all the required columns in advance and refer to the steps below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkpNzi9KUTBU0lEKzi8tSk5VCK4sLknNVTBUCEvMKU0FihsqxerAFRrhUWiErNAYn4l65kqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Record ID" = _t, Attribute = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}}),
    #"Source System List" = {"Source System 1 Value","Source System 2 Value"},
    Custom1 = Table.Group(#"Changed Type",{"Record ID"}, List.Transform(#"Source System List", (y) => { y, (x) => try Table.SelectRows(x, each [Attribute] = y)[Value]{0} otherwise null }))
in
    Custom1

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

lbendlin
Super User
Super User

Later in the process, I pivot the table

You usually avoid doing this in Power Query. Let the Power BI visuals do that for you.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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