Append data from one column to another in power query

theyaaz

New Member
Joined
Feb 28, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I am using power query to transform a data set. I have two sets of separate columns which need to be appended to one another, while copying over the data from other corresponding columns. This is what I currently have:
MrExcel Help.xlsx
ABCDEFGH
1Employee IDEmployee NameGenderMonthAttribute1Value1Attribute2Value2
21245JulioM01-JanBenefits$ 235.00Salary Increase$ -
33475JulioM01-JanOvertime$ 150.00Benefits Weighted$ 340.00
43425MargeF01-FebBenefits$ 225.00Salary Increase$ -
52342MargeF01-FebOvertime$ 170.00Benefits Weighted$ 400.00
61242GustavM01-MarBenefits$ 300.00Salary Increase$ -
71515GustavM01-MarOvertime$ 190.00Benefits Weighted$ 150.00
81465MarshaF01-AprBenefits$ 340.00Salary Increase$ 400.00
91277MarshaF01-AprOvertime$ 250.00Benefits Weighted$ 230.00
Have


This is what I want my data to look like:
MrExcel Help.xlsx
ABCDEF
1Employee IDEmployee NameGenderMonthAttribute1Value1
21245JulioM01-JanBenefits$ 235.00
33475JulioM01-JanOvertime$ 150.00
43425MargeF01-FebBenefits$ 225.00
52342MargeF01-FebOvertime$ 170.00
61242GustavM01-MarBenefits$ 300.00
71515GustavM01-MarOvertime$ 190.00
81465MarshaF01-AprBenefits$ 340.00
91277MarshaF01-AprOvertime$ 250.00
101245JulioM01-JanSalary Increase$ -
113475JulioM01-JanBenefits Weighted$ 340.00
123425MargeF01-FebSalary Increase$ -
132342MargeF01-FebBenefits Weighted$ 400.00
141242GustavM01-MarSalary Increase$ -
151515GustavM01-MarBenefits Weighted$ 150.00
161465MarshaF01-AprSalary Increase$ 400.00
171277MarshaF01-AprBenefits Weighted$ 230.00
Want


I am new to power query, but have not had any luck with searching or experimentation. Thank you.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
It could be done by using the following M Code.

Excel Ribbon -> Data -> Get Data -> From Other Sources -> Blank Query. Power Query is loaded with a blank query.
Click Advanced Editor on the Power Query Ribbon.
Delete all content of the editor that pops up, and copy and paste the following code into the editor.
Click Done to see the result.
Note: Assuming you already converted the source data range to a table and named it as Table1. If your table is named differently, then change the "Table1" accordingly in the code.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TableAttr1 = Table.SelectColumns(Source,{"Employee ID", "Employee Name",  "Month", "Gender", "Attribute1", "Value1"}),
    TableAttr2 = Table.SelectColumns(Source,{"Employee ID", "Employee Name",  "Month", "Gender", "Attribute2", "Value2"}),
    RenameColumns = Table.RenameColumns(TableAttr2,{{"Attribute2", "Attribute1"}, {"Value2", "Value1"}}),
    Result = Table.Combine({TableAttr1, RenameColumns}) 
in
    Result

How to do it by using the interface instead of using M Code?

Select the table in Excel. Get Data -> From Other Sources -> From Table or Range.
Right-click on the query in the editor (Table1 is the query name if the table name in Excel is Table1) and Duplicate.
(We are going to append tables. )

1679579667494.png


Click on the initial query, Table1, and select Attribute2 and Value2 columns (by using the Control key to make multiple column selections). Then click Remove Columns.

1679579865131.png


Select the second duplicated query and do the same in it for Attribute1 and Value1 columns this time.
Note: You can alternatively use the command menu that will appear on right-click on any selected column for the Remove Columns command.

1679579929327.png


While the duplicated query is still selected, rename Atrribute2 and Value2 columns to Attribute1 and Value1 by using the Rename command in the right-click menu for both columns, so they should match with the columns in the first query since we will append one table to another and the column names must match to get the desired result.
Note: You can also double-click on a column name to rename it.

1679580071075.png


Select Table1 and click Append Queries:
1679580290251.png


Select "Table1 (2)" as the Table to append in the Append dialog, and click Done.
1679580313588.png


"Table1" now contains the combined result.
 
Upvote 0
Solution
Maybe try this way
1. Import tabel to PQ and duplicate it
On new table apply the following steps
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee ID", Int64.Type}, {"Employee Name", type text}, {"Gender", type text}, {"Month", Int64.Type}, {"Attribute1", type text}, {"Value1", Int64.Type}, {"Attribute2", type text}, {"Value2", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Attribute1", "Value1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Attribute2", "Attribute1"}, {"Value2", "Value1"}})
in
    #"Renamed Columns"

On original table apply then apply the following
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee ID", Int64.Type}, {"Employee Name", type text}, {"Gender", type text}, {"Month", Int64.Type}, {"Attribute1", type text}, {"Value1", Int64.Type}, {"Attribute2", type text}, {"Value2", Int64.Type}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Attribute2", "Value2"}),
#"Appended Query" = Table.Combine({#"Removed Columns", #"Table1 (2)"})
in
#"Appended Query"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top