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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
rlcec
Frequent Visitor

Add custom column with sheet name

Hello all,

 

is it possible to add a custom column with the sheet name as values? 

 

We have one workbook with different worksheets per country (Portugal, Austria, ...). The Portugal sheet for example looks as follows:

DateCategoryAmount
1-1-2019Alpha

100

14-4-2019Beta250

 

The aim is to get to this table:

 

DateCategoryAmountCountry
1-1-2019Alpha

100

Portugal

14-4-2019Beta250Portugal

 

Thank you very much in advance for any help on this topic!

 

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi rlcec, 

You could try to use below M code to add sheet name

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\<username>\Desktop\New Microsoft Excel Worksheet (5).xlsx"), null, true),
    country2_Sheet = Source{[Item="country2",Kind="Sheet"]}[Data],
    #"Promoted Headers1" = Table.PromoteHeaders(country2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"country", Int64.Type}, {"anme", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Source{[Item="country2",Kind="Sheet"]}[Name])
in
    #"Added Custom"

 

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
dax
Community Support
Community Support

Hi rlcec, 

You could try to use below M code to add sheet name

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\<username>\Desktop\New Microsoft Excel Worksheet (5).xlsx"), null, true),
    country2_Sheet = Source{[Item="country2",Kind="Sheet"]}[Data],
    #"Promoted Headers1" = Table.PromoteHeaders(country2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"country", Int64.Type}, {"anme", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Source{[Item="country2",Kind="Sheet"]}[Name])
in
    #"Added Custom"

 

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Anonymous
Not applicable

You can merge the tables in Power Query Editor after you have imported the Excel file.

 

1. import the Excel File in Power Query Editor (each sheet as new table)
2. Create a new query:

( I used Portugal and Spain as table names for reference.)

 

let
Source = #table({"Tables"}, {{"Portugal"}, {"Spain"}}),
Evaluate = Table.AddColumn(Source, "Custom", each Expression.Evaluate([Tables], #shared)),
Merged = Table.ExpandTableColumn(Evaluate, "Custom", List.Union(List.Transform(Evaluate[Custom], each Table.ColumnNames(_))))
in 
Merged

 

 

This should do the trick.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors