cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
forever
Frequent Visitor

Create Columns with given name if it doesn't exist.

Hi! I have some tables with different number of column with prefix "Level"

Example: 

Table1 - Level1, Level2, Level3

Table2 - Level1, Level2, Level3, Level4

 

I want make sure all tables have Level1 ~ Level10.

How can I do that with powerquery? Thanks!

1 ACCEPTED SOLUTION
Vera_33
Solution Sage
Solution Sage

Hi @forever 

 

What are the values in those newly created columns? Here is one sample to have the same value with the column name

Vera_33_0-1626701845769.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlHSwUbFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [level1 = _t, level2 = _t, level3 = _t, level4 = _t]),
    columnList = {"level1","level2","level3","level4","level5","level6","level7","level8","level9","level10"},
    fxAddColumn = (T as table, N as number) =>
         [
             newColumns=List.Difference(columnList, Table.ColumnNames(Source)),
             counter = List.Count(newColumns),
             columnName = newColumns{N},
             tempTable = Table.AddColumn(T, columnName, each columnName),
             result = if N>= counter-1 then tempTable else @fxAddColumn(tempTable,N+1) 
         ][result], 

    Custom = fxAddColumn(Source,0)

in
    Custom

 

View solution in original post

4 REPLIES 4
CNENFRNL
Super User III
Super User III

let
    Source = #table(List.Transform({1..10}, each "Level" & Text.From(_)), {}),
    Other = #table({"Level1","Level3","Level5"},{{1,3,5}}),
    Appended = Source & Other
in
    Appended

Screenshot 2021-07-20 064806.png

Vera_33
Solution Sage
Solution Sage

Hi @forever 

 

What are the values in those newly created columns? Here is one sample to have the same value with the column name

Vera_33_0-1626701845769.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlHSwUbFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [level1 = _t, level2 = _t, level3 = _t, level4 = _t]),
    columnList = {"level1","level2","level3","level4","level5","level6","level7","level8","level9","level10"},
    fxAddColumn = (T as table, N as number) =>
         [
             newColumns=List.Difference(columnList, Table.ColumnNames(Source)),
             counter = List.Count(newColumns),
             columnName = newColumns{N},
             tempTable = Table.AddColumn(T, columnName, each columnName),
             result = if N>= counter-1 then tempTable else @fxAddColumn(tempTable,N+1) 
         ][result], 

    Custom = fxAddColumn(Source,0)

in
    Custom

 

View solution in original post

forever
Frequent Visitor

I prefer all values of new created columns are setted as null. Thanks!

Hi @forever 

 

modify this line a little bit

  tempTable = Table.AddColumn(T, columnName, each null),

Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Get Ready for Power BI Dev Camp

Microsoft named a Leader in The Forrester Wave

Microsoft received the highest score of any vendor in both the strategy and current offering categories.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Top Kudoed Authors