Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Transforming merged and centered excel files

Hello,

 

I am trying to transform a table in excel that has double headers.  I got rid of rows 1-5, and rows 13 and below but do not know how to deal with these sub headers

 

You can see in row 6 of the original image below there is a header for which bank the columns fall under.  Column C and D are bank 1 (the two columns are merged), E is 2, F is 3, and so on.

 

Original

RBraun_0-1661888284774.png

 

I would like to be able to transform the data so that it appears as such:

RBraun_1-1661888614415.png

Combining the appropriate bank # with the rows below it.

 

Thank you in advance for your help

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Anonymous here is the one file I used and you can take it from here:

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\parvi\Downloads\anonymous weekly file.xlsx"), null, true),
    #"US Cash Detail_Sheet" = Source{[Item="US Cash Detail",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"US Cash Detail_Sheet", [PromoteAllScalars=true]),
    #"Removed Top Rows" = Table.Skip(#"Promoted Headers",4),
    #"Removed Other Columns" = Table.SelectColumns(#"Removed Top Rows",{"Company Weekly Cash Recap", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    //get table for merged columns
    merge_row = Record.ToTable(#"Removed Other Columns"{0}),

    //fill the missing merge columns 
    fillmissing = Table.FillDown(merge_row,{"Value"}),

    //get column list for merge row
    merge_columns_aslist = fillmissing[Value],

    //get the actual header row
    header_row = Record.ToTable(#"Removed Other Columns"{1}),

    //get header row column list
    header_columns_aslist  = header_row[Value],

    //concatenate first and 2nd row list
    new_columns_aslist = List.Transform(List.Zip({merge_columns_aslist,header_columns_aslist}), each Text.Combine(_, " ") ),

    //remove top rows from the table
    remove_rows =  Table.RemoveFirstN(#"Removed Other Columns",2),
    
    //get current table column names
    current_columns_aslist = Table.ColumnNames(remove_rows),

    //create list of current column with new columns
    rename_column_aslist = List.Zip({current_columns_aslist,new_columns_aslist}),

    //rename columns in the final output table
    rename_columns = Table.RenameColumns(remove_rows, rename_column_aslist)

in
    rename_columns


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

11 REPLIES 11
parry2k
Super User
Super User

@Anonymous good to know, yes the core logic is there, just need to tweak the step and reference table. All the best!

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Anonymous here is the one file I used and you can take it from here:

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\parvi\Downloads\anonymous weekly file.xlsx"), null, true),
    #"US Cash Detail_Sheet" = Source{[Item="US Cash Detail",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"US Cash Detail_Sheet", [PromoteAllScalars=true]),
    #"Removed Top Rows" = Table.Skip(#"Promoted Headers",4),
    #"Removed Other Columns" = Table.SelectColumns(#"Removed Top Rows",{"Company Weekly Cash Recap", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    //get table for merged columns
    merge_row = Record.ToTable(#"Removed Other Columns"{0}),

    //fill the missing merge columns 
    fillmissing = Table.FillDown(merge_row,{"Value"}),

    //get column list for merge row
    merge_columns_aslist = fillmissing[Value],

    //get the actual header row
    header_row = Record.ToTable(#"Removed Other Columns"{1}),

    //get header row column list
    header_columns_aslist  = header_row[Value],

    //concatenate first and 2nd row list
    new_columns_aslist = List.Transform(List.Zip({merge_columns_aslist,header_columns_aslist}), each Text.Combine(_, " ") ),

    //remove top rows from the table
    remove_rows =  Table.RemoveFirstN(#"Removed Other Columns",2),
    
    //get current table column names
    current_columns_aslist = Table.ColumnNames(remove_rows),

    //create list of current column with new columns
    rename_column_aslist = List.Zip({current_columns_aslist,new_columns_aslist}),

    //rename columns in the final output table
    rename_columns = Table.RenameColumns(remove_rows, rename_column_aslist)

in
    rename_columns


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

I had to mess with it just a little bit but I finnally got it to work.  Thank you so much!

parry2k
Super User
Super User

@Anonymous can you share the pbix file with the sample excel you are using, remove sensitive information before sharing, it is a bit hard to find out what is going on and do the guess work.

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

I sent you an email 

parry2k
Super User
Super User

@Anonymous seems like you didn;t copy my full code, you are missing the comma in the line above

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

I did miss the comma, but now I am running into this error

RBraun_0-1661969648267.png

RBraun_1-1661969692161.png

 

It has something to do with the first column being a date, and the code trying to operate on it like it is text.

 

parry2k
Super User
Super User

@Anonymous this would be your full query:

 

let
    Source = Excel.Workbook(Parameter1, null, true),
    #"US Cash Detail_Sheet" = Source{[Item="US Cash Detail",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"US Cash Detail_Sheet", [PromoteAllScalars=true]),
    #"Removed Top Rows" = Table.Skip(#"Promoted Headers",4),
    #"Kept First Rows" = Table.FirstN(#"Removed Top Rows",3),
 
    //get table for merged columns
    merge_row = Record.ToTable(#"Kept First Rows"{0}),

    //fill the missing merge columns 
    fillmissing = Table.FillDown(merge_row,{"Value"}),

    //get column list for merge row
    merge_columns_aslist = fillmissing[Value],

    //get the actual header row
    header_row = Record.ToTable(Sheet1_Sheet{1}),

    //get header row column list
    header_columns_aslist  = header_row[Value],

    //concatenate first and 2nd row list
    new_columns_aslist = List.Transform(List.Zip({merge_columns_aslist,header_columns_aslist}), each Text.Combine(_, " ") ),

    //remove top rows from the table
    remove_rows =  Table.RemoveFirstN(Sheet1_Sheet,2),
    
    //get current table column names
    current_columns_aslist = Table.ColumnNames(remove_rows),

    //create list of current column with new columns
    rename_column_aslist = List.Zip({current_columns_aslist,new_columns_aslist}),

    //rename columns in the final output table
    rename_columns = Table.RenameColumns(remove_rows, rename_column_aslist)

in
    rename_columns

 

if you run into any issues, let me know.

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

@parry2k I am getting a "Token Comma Expected" error when using this code at the very first line.  

RBraun_0-1661966785431.png

 

parry2k
Super User
Super User

@Anonymous after you have remove unwanted rows, add following steps:

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\my\Downloads\Test Data.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

    //get table for merged columns
    merge_row = Record.ToTable(Sheet1_Sheet{0}),

    //fill the missing merge columns 
    fillmissing = Table.FillDown(merge_row,{"Value"}),

    //get column list for merge row
    merge_columns_aslist = fillmissing[Value],

    //get the actual header row
    header_row = Record.ToTable(Sheet1_Sheet{1}),

    //get header row column list
    header_columns_aslist  = header_row[Value],

    //concatenate first and 2nd row list
    new_columns_aslist = List.Transform(List.Zip({merge_columns_aslist,header_columns_aslist}), each Text.Combine(_, " ") ),

    //remove top rows from the table
    remove_rows =  Table.RemoveFirstN(Sheet1_Sheet,2),
    
    //get current table column names
    current_columns_aslist = Table.ColumnNames(remove_rows),

    //create list of current column with new columns
    rename_column_aslist = List.Zip({current_columns_aslist,new_columns_aslist}),

    //rename columns in the final output table
    rename_columns = Table.RenameColumns(remove_rows, rename_column_aslist)

in
    rename_columns

 

also the sample excel sheet I used is attached, if you want to test, copy the attached file to a folder, change the path in the above script to read the file, start a new blank query, click advanced editor and copy the above code.

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

@parry2k thank you for your help but I don't understand where the code is supposed to fit in my existing code.  Below is my existing code, it is at the point where I have removed all the rows I do not want

let
    Source = Excel.Workbook(Parameter1, null, true),
    #"US Cash Detail_Sheet" = Source{[Item="US Cash Detail",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"US Cash Detail_Sheet", [PromoteAllScalars=true]),
    #"Removed Top Rows" = Table.Skip(#"Promoted Headers",4),
    #"Kept First Rows" = Table.FirstN(#"Removed Top Rows",3)
in
    #"Kept First Rows"

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.