March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
hello and thanks for reading
I'm trying to add multiple new columns to a table in Power Query at the same time.
Additional columns can be a bit complex, such as referencing other tables.
For simplicity, let's say we have one fact table `Sales` and one dimension table `Products`.
Suppose the table `Sales` contains daily sales data. So we have the following columns: Date, Amount, Product Code
On the other hand, the table `Products` has two columns: Product Name, Product Code.
When I add new columns for the first day of the month and the product name to this table `Sales` at the same time, I use the following M code:
Table. ExpandRecordColumn(
Table.AddColumn(Sales, "Added Column", each [
#"First day of the month" = #date(Date.Year([Date]),Date.Month([Date],1) ,
#"Product Name" = Products{[#"Product Code" = [#"Product Code"]}[#"Product Name"]
]),
"Added Columns", {"First day of the month", "Product Name"})
I don't think this is a good way. This code is very hard to read.
The biggest reason is based on the specification of the symbol "[ ]".
The symbol "[ ]" is used in this code with two meanings, a record constructor operator and a field selection operator, which are nested many times.
I'm sure there is a better way. if you'd teach me.
Thank you in advance.
It is possible to simplify the M code to make it more readable and easier to understand. One approach is to break down the steps into smaller, more manageable ones. Here's an example:
SalesWithProductName = Table.AddColumn(SalesWithProduct, "Product Name", each [Product][Product Name])
SalesFirstDayOfMonth = Table.SelectRows(SalesWithProductName, each Date.Day([Date]) = 1)
This approach breaks down the process into smaller, more manageable steps, making it easier to understand and modify.
thank you for teaching me.
For simplicity, I've added only two columns, the first day of the month and the product name column, but I might probably add 10 or 20 columns.
Using the method you just taught me, I could add multiple columns from the Products table at the same time, only if I "left outer join" the columns.
However, if I add a calculated column that uses an existing column, such as the first day of the month, the number of steps will increase by the number of columns I add, so I would avoid it if possible.
I'd like to have as few steps as possible just adding columns.
Best Regards,
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
34 | |
30 | |
20 | |
19 | |
12 |