Forum Discussion
Add multiple columns in a single step (Power Query)
- 8 years ago
Hi Anonymous,
To what I could understood you wnat to append columns if you append two columns that have different columns the ones that don't exist in the other table will automaticly be filled with nulls so you don't need to create those addtional columns:
Regards,
MFelix
- 8 years ago
This post was concurrent with MFelix' post.
Actually I don't understand the requirement, as you can just append tables with different columns.
Null values will be added automatically.
Anyhow if you still want to add multiple blank columns (I hope null is also fine) then you can use Table.SelectColumns with 3rd argument MissingField.UseNull (see bottom query below).
All 4 queries below consist of 1 line each.
Query Table5Columns:
= #table(5,{{1..5}})Query Table1Column:
= #table(1,List.Zip({{1..3}}))Query Appended:
= Table5Columns&Table1Column
Query Table1AddedColumns:
= Table.SelectColumns(Table1Column,Table.ColumnNames(Table5Columns),MissingField.UseNull)
If you want to add 5 null columns this syntax will work:
Add_Multiple_Columns=List.Accumulate({1..5}, Source, (state, current) => Table.AddColumn(state, "UserDefined" & Number.ToText(current), each null)),
Help from: https://stackoverflow.com/questions/38251421/powerquery-adding-multiple-columns
Nice one Bsacheri !
I too stumbled across your answer when looking for something else and have made use of the code. Just adding that it doesn't have to be a number sequence in the List.Accumulate, I have used
List.Accumulate({"A","B","X","Y"}...And a conditional statement within the Table.AddColumn - all works really well!
- RollKoll4 years agoFrequent Visitor
I tried this oneliner, and sadly failed...
= Table.AddColumn(#"Grouped Rows", List.Accumulate({"B","X","Y"}), each "AZ12345")caused an error,
Expression.Error: 1 arguments were passed to a function which expects 3. Details: Pattern= Arguments=[List]I am quite new in this field... Can you please help with it?
I simply want to add 4 or more columns in one line.- MFelix4 years agoSuper User
Hi RollKoll ,
What do you want to do? Do you want to add an additional line to your code with specif values?
- RollKoll4 years agoFrequent Visitor
I simply need to add 1 Column with values "CD" in each row and 3 more empty columns (spaceholders).
In ideal, name or rename them in one go also.
I want to minimise the amount of steps, - as later it will be repeated in around 10 different tables.