Add a new column in Excel repeating data in a single Cell in a column that also has data that I do not want in the column using Power Query

Willforth

New Member
Joined
Aug 2, 2011
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I apologise for the confused title but I do not know how to succinctly describe my request for help. I thought it best to create a table to show what I mean (see below).

The text in green shows the name I already have in my table and it's location. I want to use Power Query to add a new column (shown in red) and insert the green text in all the rows where there is a day of the week showing in Column A (See yellow text for where I want the copied data inserted).

The next block of data starts with a new name (see second instance of green text) and I want that placed in the next set of rows where there is a day of the week showing in Column A (See yellow text for where I want the copied data inserted). I have around 100 names that need to be inserted in this manner so it is a long list.

I have tried Inserting a column using a conditional statement but frankly, all I am getting is frustration.

Please can you help a Newbie?

Name 1
DayDateSomethingSomething elseSomething moreStaff Name
MonName 1
TueName 1
SunName 1
Name 2
Day
MonName 2
ThuName 2
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
your source table is not 1NF table and imo not representative
but
Column1Column2Column3Column4Column5Column1Column2Column3Column4Column5Custom
Name 1Name 1
DayDateSomethingSomething elseSomething moreDayDateSomethingSomething elseSomething moreStaff Name
MonMonName 1
TueTueName 1
SunSunName 1
Name 2Name 2
DayDay
MonMonName 2
ThuThuName 2

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    IF = Table.AddColumn(Source, "FD", each if not Text.Contains([Column1], "Name") or [Column1] = "Day" then null else [Column1]),
    FD = Table.FillDown(IF,{"FD"}),
    IF2 = Table.AddColumn(FD, "Staff", each if [Column1] = "Day" or Text.Contains([Column1], "Name") then null else [FD]),
    Replace = Table.ReplaceErrorValues(IF2, {{"Staff", null}}),
    IF3 = Table.AddColumn(Replace, "Custom", each if [Column1] = "Day" and [Column2] <> null then "Staff Name" else [Staff]),
    TSC = Table.SelectColumns(IF3,{"Column1", "Column2", "Column3", "Column4", "Column5", "Custom"})
in
    TSC
 
Upvote 0
your source table is not 1NF table and imo not representative
but
Column1Column2Column3Column4Column5Column1Column2Column3Column4Column5Custom
Name 1Name 1
DayDateSomethingSomething elseSomething moreDayDateSomethingSomething elseSomething moreStaff Name
MonMonName 1
TueTueName 1
SunSunName 1
Name 2Name 2
DayDay
MonMonName 2
ThuThuName 2

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    IF = Table.AddColumn(Source, "FD", each if not Text.Contains([Column1], "Name") or [Column1] = "Day" then null else [Column1]),
    FD = Table.FillDown(IF,{"FD"}),
    IF2 = Table.AddColumn(FD, "Staff", each if [Column1] = "Day" or Text.Contains([Column1], "Name") then null else [FD]),
    Replace = Table.ReplaceErrorValues(IF2, {{"Staff", null}}),
    IF3 = Table.AddColumn(Replace, "Custom", each if [Column1] = "Day" and [Column2] <> null then "Staff Name" else [Staff]),
    TSC = Table.SelectColumns(IF3,{"Column1", "Column2", "Column3", "Column4", "Column5", "Custom"})
in
    TSC
Thank you so much for this. NEVER IN A MILLION YEARS would I have got this. All I need do now is figure out where to put this code! I told you I was a Newbie! :-) Once again. Many thanks
 
Upvote 0
will be much easier if you update your profile (Account details) about Excel version and OS, don't forget to scroll down and hit Save
 
Upvote 0
Solution
ok, thanks for update
so try
  • 1. select your whole range then use Ctrl+T to create Excel Table (with My Table has headers unchecked)
  • 2. put cursor somewhere on this table and from the ribbon Data tab use From Table, it will open Power Query Editor
  • 3. Go to Advanced Editor and replace code there with code copied from the post
  • 4. make sure the name of the table in the code is the same as your source table (here is Table1, change in the code if necessary)
  • 5. OK then Close&Load

hope I haven't forgotten something :unsure:
 
Upvote 0
Hi,

I think I am NOT providing you with the full information within the previous table I posted. I got your code to run fine (thanks for the instructions on how to do that) However, you can see from the attached that it posts "Staff" not the actual names ....in this case Steve Smith and Claire Williams in the rows that contains days in the first column. I hope the attached image is OK. This is taken from a live table that I have altered slightly to preserve confidentiality but all the columns are there and accurate

What is wrong?

Can you help....again!!
 

Attachments

  • Spreadsheet.jpg
    Spreadsheet.jpg
    171.6 KB · Views: 15
Upvote 0
your image from the post#6 is quite different then your example from post#1
if you will use example from post#1 it should work. Solution is tailored to the example.
I suggest to post a link to the shared excel file with representative source example and expected result. Use a free service like onedrive, googledrive or any similar
or
use XL2BB to post these two things
 
Last edited:
Upvote 0
Thanks for all your help so far. I downloaded and installed XL2BB and below is the data I have pasted from th etable. I have changed names etc. for confidentiality but all else is the same. The second table shows the desired output.

The table has already been edited via Power Query to get rid of some of the rows. 2 columns "Timesheet" and "Comments" are a split from a single column made in Power Query. Not sure this is relevant just wanted to give full disclosure.

StaffDateStartFinishBreakClientTimesheetCommentPay-HrPay-NamePay-CodeSales-HrSales-NameSales-CodeMultiplierHoursPaySalesMarginColumn from Code
Steve Smith56 hours467.65
Mon16/11/202009:0017:00Client 1TS 344208.2113.5CS1CS1-WK865.6810842.32Staff Name
Tue17/11/202009:0016:00Client 2TS 344238.2113.5CS2CS2-WE757.4794.537.03null
Tue17/11/202020:0008:00Client 3TS 344158.2113.26CS3CS1-WK1298.52159.1260.6null
Fri20/11/202020:0008:00Client 4TS 344138.2113.26CS4CS2-WE1298.52159.1260.6null
Sun22/11/202009:0015:00Client 5TS 344239Sunday BasicSunB14.5CS5CS1-WK6548733null
Sun22/11/202020:0023:59Client 6TS 3177756HRS5089Sunday BasicSunB14.01CS6CS2-WE3.9835.8555.8119.96null
Sun22/11/202023:5908:001Client 7TS 3177756HRS5088.2114.01CS7CS1-WK7.0257.6198.340.7null
Claire Williams44 hours364.39Client 8
Mon16/11/202020:0008:001Client 9TS 341428.2113.01CS9CS1-WK1190.31143.1152.8Staff Name
Thu19/11/202020:0008:001Client 10TS 341428.2113.01CS10CS2-WE1190.31143.1152.8null
Fri20/11/202020:0008:001Client 11TS 341428.2113.01CS11CS1-WK1190.31143.1152.8null


DESIRED OUTCOME

StaffDateStartFinishBreakClientTimesheetCommentPay-HrPay-NamePay-CodeSales-HrSales-NameSales-CodeMultiplierHoursPaySalesMarginDesired Output
Steve Smith56 hours467.65
Mon16/11/202009:0017:00Client 1TS 344208.2113.5CS1CS1-WK865.6810842.32Steve Smith
Tue17/11/202009:0016:00Client 2TS 344238.2113.5CS2CS2-WE757.4794.537.03Steve Smith
Tue17/11/202020:0008:00Client 3TS 344158.2113.26CS3CS1-WK1298.52159.1260.6Steve Smith
Fri20/11/202020:0008:00Client 4TS 344138.2113.26CS4CS2-WE1298.52159.1260.6Steve Smith
Sun22/11/202009:0015:00Client 5TS 344239Sunday BasicSunB14.5CS5CS1-WK6548733Steve Smith
Sun22/11/202020:0023:59Client 6TS 3177756HRS5089Sunday BasicSunB14.01CS6CS2-WE3.9835.8555.8119.96Steve Smith
Sun22/11/202023:5908:001Client 7TS 3177756HRS5088.2114.01CS7CS1-WK7.0257.6198.340.7Steve Smith
Claire Williams44 hours364.39Client 8
Mon16/11/202020:0008:001Client 9TS 341428.2113.01CS9CS1-WK1190.31143.1152.8Claire Williams
Thu19/11/202020:0008:001Client 10TS 341428.2113.01CS10CS2-WE1190.31143.1152.8Claire Williams
Fri20/11/202020:0008:001Client 11TS 341428.2113.01CS11CS1-WK1190.31143.1152.8Claire Williams
 
Upvote 0
StaffDateStartFinishBreakClientTimesheetCommentPay-HrPay-NamePay-CodeSales-HrSales-NameSales-CodeMultiplierHoursPaySalesMarginDesired Output
Steve Smith56 hours467.65
Mon441510.3750.708333333Client 1TS 344208.2113.5CS1CS1-WK865.6810842.32Steve Smith
Tue441520.3750.666666667Client 2TS 344238.2113.5CS2CS2-WE757.4794.537.03Steve Smith
Tue441520.8333333330.333333333Client 3TS 344158.2113.26CS3CS1-WK1298.52159.1260.6Steve Smith
Fri441550.8333333330.333333333Client 4TS 344138.2113.26CS4CS2-WE1298.52159.1260.6Steve Smith
Sun441570.3750.625Client 5TS 344239Sunday BasicSunB14.5CS5CS1-WK6548733Steve Smith
Sun441570.8333333330.999305556Client 6TS 3177756HRS5089Sunday BasicSunB14.01CS6CS2-WE3.9835.8555.8119.96Steve Smith
Sun441570.9993055560.3333333331Client 7TS 3177756HRS5088.2114.01CS7CS1-WK7.0257.6198.340.7Steve Smith
Claire Williams44 hours364.39Client 8
Mon441510.8333333330.3333333331Client 9TS 341428.2113.01CS9CS1-WK1190.31143.1152.8Claire Williams
Thu441540.8333333330.3333333331Client 10TS 341428.2113.01CS10CS2-WE1190.31143.1152.8Claire Williams
Fri441550.8333333330.3333333331Client 11TS 341428.2113.01CS11CS1-WK1190.31143.1152.8Claire Williams

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FD = Table.FillDown(Table.AddColumn(Source, "Custom", each if [Staff] <> "" and [Date] = null then [Staff] else null),{"Custom"}),
    Result = Table.RemoveColumns(Table.AddColumn(FD, "Desired Output", each if [Staff] <> "" and [Date] = null then null else [Custom]),{"Custom"})
in
    Result
 
Upvote 0

Forum statistics

Threads
1,225,653
Messages
6,186,203
Members
453,340
Latest member
yearego021

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