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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
mofu1401
Helper I
Helper I

Transform data with 2 different headers

Hi all, 

I have a data sheet with sales and visits in a single worksheet.

I was trying to transform into a single table but could get it right.

Can someone help me with it. Thank you so much.

FY23 Sales Visit.xlsx

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @mofu1401 

Please see this sample query

let
    Source = Excel.Workbook(File.Contents("filepath"), null, true),
    FY23_Sheet = Source{[Item="FY23",Kind="Sheet"]}[Data],
    #"Filtered Rows" = Table.SelectRows(FY23_Sheet, each [Column1] <> null and [Column1] <> ""),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Category", each if [Column1] = "Sales" or [Column1] = "Visit" then  [Column1] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Category"}),
    #"Grouped Rows" = let
    Source = #"Filled Down",  // Your source table
    ColumnsList = Table.ColumnNames(Source),
    TypesList = List.Transform(ColumnsList, each if _ = "Category" then "text" else "any"),
    TypeExpression = "type table [" & Text.Combine(List.Transform(List.Zip({ColumnsList, TypesList}), each _{0} & "=" & _{1}), ", ") & "]",
    TypeRecord = Expression.Evaluate(TypeExpression, #shared),
    Grouped = Table.Group(Source, {"Category"}, {{"Grouped", each _, TypeRecord}})
    
in
   Grouped,
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Grouped Transformed", each let 
PromotedHeaders = 
Table.PromoteHeaders([Grouped], [PromoteAllScalars = true]),
AutomaticallyRenamedColumns = Table.RenameColumns(PromotedHeaders, {{Table.ColumnNames(PromotedHeaders){0}, "Customer"}} ),
Unpivoted = Table.UnpivotOtherColumns ( AutomaticallyRenamedColumns, {"Customer"}, "Date", "Value")
in Unpivoted),
    #"Expanded Grouped Transformed" = Table.ExpandTableColumn(#"Added Custom1", "Grouped Transformed", {"Customer", "Date", "Value"}, {"Customer", "Date", "Value"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Grouped Transformed",{{"Date", type date}}),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Date"}),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Errors",{"Grouped"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Category]), "Category", "Value", List.Sum),
    #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Customer", type text}, {"Sales", type number}, {"Visit", Int64.Type}})
in
    #"Changed Type1"

danextian_0-1740715696035.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
mofu1401
Helper I
Helper I

@danextian hi sir, can you share the pbix?

Thank you.

Here it is. Just change the path to the file.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
danextian
Super User
Super User

Hi @mofu1401 

Please see this sample query

let
    Source = Excel.Workbook(File.Contents("filepath"), null, true),
    FY23_Sheet = Source{[Item="FY23",Kind="Sheet"]}[Data],
    #"Filtered Rows" = Table.SelectRows(FY23_Sheet, each [Column1] <> null and [Column1] <> ""),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Category", each if [Column1] = "Sales" or [Column1] = "Visit" then  [Column1] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Category"}),
    #"Grouped Rows" = let
    Source = #"Filled Down",  // Your source table
    ColumnsList = Table.ColumnNames(Source),
    TypesList = List.Transform(ColumnsList, each if _ = "Category" then "text" else "any"),
    TypeExpression = "type table [" & Text.Combine(List.Transform(List.Zip({ColumnsList, TypesList}), each _{0} & "=" & _{1}), ", ") & "]",
    TypeRecord = Expression.Evaluate(TypeExpression, #shared),
    Grouped = Table.Group(Source, {"Category"}, {{"Grouped", each _, TypeRecord}})
    
in
   Grouped,
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Grouped Transformed", each let 
PromotedHeaders = 
Table.PromoteHeaders([Grouped], [PromoteAllScalars = true]),
AutomaticallyRenamedColumns = Table.RenameColumns(PromotedHeaders, {{Table.ColumnNames(PromotedHeaders){0}, "Customer"}} ),
Unpivoted = Table.UnpivotOtherColumns ( AutomaticallyRenamedColumns, {"Customer"}, "Date", "Value")
in Unpivoted),
    #"Expanded Grouped Transformed" = Table.ExpandTableColumn(#"Added Custom1", "Grouped Transformed", {"Customer", "Date", "Value"}, {"Customer", "Date", "Value"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Grouped Transformed",{{"Date", type date}}),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Date"}),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Errors",{"Grouped"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Category]), "Category", "Value", List.Sum),
    #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Customer", type text}, {"Sales", type number}, {"Visit", Int64.Type}})
in
    #"Changed Type1"

danextian_0-1740715696035.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.