how do I add multiple new columns in m for power query or power bi?
- User
how do I add multiple new columns in m for power query or power bi?
- Assistant
In Power Query or Power BI, you can add multiple new columns by using the "Add Column" menu in the Home tab. Once you click on the "Add Column" menu, you will see options to add a custom column, index column, and more.
To add multiple new columns, you can use the "Custom Column" option. You can also use "Duplicate Column" option to duplicate an existing column, and then rename the new column as desired.
Another way to add multiple new columns is to use the "Add Columns" button to add columns from a table, then rename the new columns as desired.
You can also add multiple columns by using the "Add Column" button to add columns from a table, then rename the new columns as desired.
In addition, you can also use formula or other transformation in Power Query Editor to generate new columns based on existing columns or your own logic.
- User
can you show me the m code?
- Assistant
Sure, here is an example of how you can use M code to add multiple new columns in Power Query:
codelet Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUzI1V...", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]), #"Added Custom Columns" = Table.AddColumn(Source, "Custom Column 1", each "Value 1"), #"Added Custom Columns1" = Table.AddColumn(#"Added Custom Columns", "Custom Column 2", each "Value 2"), #"Added Custom Columns2" = Table.AddColumn(#"Added Custom Columns1", "Custom Column 3", each "Value 3") in #"Added Custom Columns2" - User
this means that there is no function to add multiple columns at once?
- Assistant
I apologize for any confusion, there is no built-in function in Power Query or Power BI to add multiple new columns at once.
However, there are a few ways to accomplish this by chaining multiple "Add Column" steps together, using a function such as
Table.AddColumnwhich can be used multiple times, or usingTable.ExpandRecordColumnfunction to add multiple columns from a nested table or record.You can also use formula in Power Query Editor to generate new columns based on existing columns or your own logic.
For example, you can use the
Table.DuplicateColumnfunction to create multiple new columns at once, by passing the name of the column you want to duplicate and the names of the new columns as a list of strings.codelet Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUzI1V...", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Duplicate Columns" = Table.DuplicateColumn(Source, "Column1", {"Column2", "Column3", "Column4"}) in #"Duplicate Columns" - User
can you write a custom function for me to accomplish that?
- Assistant
Sure, here's an example of a custom function in Power Query that you can use to add multiple new columns at once:
codelet AddMultipleColumns = (table as table, columns as list, values as list) => let newtable = table, index = 0 in for column in columns do newtable = Table.AddColumn(newtable, column, each values{index}) index = index + 1 newtable in AddMultipleColumnsYou can use this function by passing in the table you want to add columns to, a list of column names, and a list of values.
For example:
code
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.