Forum

Power Query Transfo...
 
Notifications
Clear all

Power Query Transform: Header Rows Repeat Until Next Header Adjacent column

8 Posts
3 Users
0 Reactions
333 Views
(@srv1990)
Posts: 15
Eminent Member
Topic starter
 

I think this is possible but I have no idea how to accomplish this.  What I am trying to do is repeat a row.  If you look under, the second screenshot below line description column, you will see headers like Salaires & Wages.  They are always identified as LineSubSequence 0 and maybe this is the key.  Is there a way to make the Salaries and Wages go on every single line blow the subaccounts until it reaches the next header row which in this case be Benefits?  I hope I am making good sense of this description.

 

So it would look like this:

BEFORE:

     
     
     
Salaries & Wages    
6005000000 Productive Wages  
6005050000 Productive Premium  
6025000000 CTO Wages  
6025050000 CTO_COB Payout  
6040000000 Non-Productive Wages  
     
     
AFTER:    
Salaries & Wages 6005000000 Productive Wages
Salaries & Wages 6005050000 Productive Premium
Salaries & Wages 6025000000 CTO Wages
Salaries & Wages 6025050000 CTO_COB Payout
Salaries & Wages 6040000000 Non-Productive Wages

 

thumbnail image 1 of blog post titled 
	
	
	 
	
	
	
				
		
			
				
						
							PowerQuery Help

 
Posted : 23/03/2022 7:00 am
Alan Sidman
(@alansidman)
Posts: 222
Member Moderator
 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type1" = Table.TransformColumnTypes(Source,{{"Salaries & Wages", type text}}),
#"Demoted Headers" = Table.DemoteHeaders(#"Changed Type1"),
#"Added Custom" = Table.AddColumn(#"Demoted Headers", "Custom", each if Text.Contains([Column1],"Salaries") then [Column1] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column1] <> "Salaries & Wages")),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "Column1", "Column2"})
in
#"Reordered Columns"

 
Posted : 23/03/2022 6:40 pm
Riny van Eekelen
(@riny)
Posts: 1193
Member Moderator
 

As a variant and assuming you want to apply this not only to areas in the table (referring to the screenshot) where the header contains the word "Salaries".

let
Source = Excel.CurrentWorkbook(){[Name="myData"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each if [Column2] is null then [Column1] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> null)),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "Column1", "Column2"})
in
#"Reordered Columns"

Screenshot-2022-03-23-121229.png

 
Posted : 24/03/2022 7:15 am
(@srv1990)
Posts: 15
Eminent Member
Topic starter
 

Hi Alan, First of all thank you for your response but I think I have not done a good job explaining this.  It is not only Salaries and Wages it will contain many other expense and revenue categories.  Please see below.  I do apologize but I am super new in PowerQuery and probablyl my lack of experience is hindering my exlanation and what you need.  Can I provide more information somehow to make my help request better?

 

Salaries & Wages
6005000000
6005050000
6025000000
6025050000
6040000000
6041000000
Total Salaries & Wages
Benefits
6102000000
6104020000
6104060000
6106000000
6108000000
6202000000
6206020000
6214000000
Total Benefits
Fees
7202000000
7210000000
7222000000
Total Fees
Utilities
6302000000
Total Utilities
Supplies - Medical
7602000000
Total Supplies - Medical
Supplies - Other
7402000000
7422000000
7424000000
7426000000
7426020000
Total Supplies - Other
Purchased Services
 
Posted : 24/03/2022 7:24 am
(@srv1990)
Posts: 15
Eminent Member
Topic starter
 

PPQ.pngRiny, I placed the formula in but it says it cannot find the excel table.  I tried both MyData nd PS Detailed.  I think maybe it is how I am pulling in the table perhaps.  I added the screnshot so maybe it can offer some clues.  It is a connection to an SQL Server, in the database name PMReporting_Work and it its grabbing table PS Detailed.  Sorry guys I am just so new this and I am probablly frustrating some people.  I hope to get better at PowerQuery.

 

Expression.Error: We couldn't find an Excel table named 'MyData'. 

OR

Expression.Error: We couldn't find an Excel table named 'PS Detailed'.

 
Posted : 24/03/2022 7:34 am
Riny van Eekelen
(@riny)
Posts: 1193
Member Moderator
 

The Source step and Column names used in the query are subject to your own data structure. It it's a structured table, use the correct table name. In my example I used a named range called myData (with a small m) and without headers. So it landed in PQ with two columns called Column1 and Column2.

So, just connect to the source as you normally would and when you get to the point that you want to add the custom column, make sure that the #"Added Custom" step is referring to the correct column names, whatever these are at this point in the query.

You can follow it step-by-step in the file I uploaded with my first answer.

 
Posted : 24/03/2022 9:09 am
(@srv1990)
Posts: 15
Eminent Member
Topic starter
 

How-Data-is-Pulled-in.pngPQ-Help.pngPQError.pngHi Riny,

I am sorry but I just cannot get it to work.  It must be something trivial that I am missing.

I think I fixed the connection to the table issue by adjusting as follows but now I am getting an error saying that the subaccount column was not found. 

let
Source = Sql.Database("vwp-fi-", "PMReporting_Work"),
#"dbo_PS Detailed" = Source{[Schema="dbo",Item="PS Detailed"]}[Data],
#"Added Custom" = Table.AddColumn(Source, "Custom", each if [Subaccount Description] is null then [Subaccount] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Line_Description_Desc] <> null)),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "Subaccount", "Subaccount Description"})
in
#"Reordered Columns"

Sorry I am doing my best I just cant seemt o figure it out.  I changed the two column names so its easier to distinguish.  I tried to put in as much detail for you to see and hopefully it is something you can spot off the bat.

Thank you for your patientce with me.

 
Posted : 26/03/2022 7:06 am
Riny van Eekelen
(@riny)
Posts: 1193
Member Moderator
 

I can't really relate the error message to the script you copied into your message, but are you sure that the column name is really "Subaccount" at this stage? Two steps down you refer to [Line_Description_Desc] which, based on your first message seems to contain what you now refer to as [Subaccount]. Make sure that the column names are indeed as included in the previous step. Attaching a file with my mockup that looks a bit like yours now.

 
Posted : 27/03/2022 8:09 am
Share: