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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

FInd header row dynamically in Query

Hi,

 

jeongkim_0-1751465444154.png

 

my data has header in row 13 but not sure future change such to row 14 or 12. 

So any way making KPI ID row always header whereever they placed? 

If I use remove top rows then it cannot cover dynamic changes. 

2 ACCEPTED SOLUTIONS
mh2587
Super User
Super User

Try this one might help you
HeaderRowIndex = List.PositionOf(
Table.Column(Sheet, "Column1"),
"KPI ID"
)


✔ Answered? Mark as solution

Muhammad Hasnain | Super User • Fabric • Power BI • Data Engineering

Let's connect on LinkedIn

View solution in original post

bhanu_gautam
Super User
Super User

@Anonymous 
Add a custom column to identify the row containing "KPI ID".
Filter the table to find the row number of the header.
Use that row number to set the headers dynamically.

 

Try using below M code

m
let
Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "IsHeader", each if Text.Contains([Column1], "KPI ID") then "Header" else null),
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Added Index", each ([IsHeader] = "Header")),
HeaderRowIndex = Table.FirstValue(#"Filtered Rows")[Index],
#"Removed Custom" = Table.RemoveColumns(#"Added Index",{"IsHeader", "Index"}),
#"Promoted Headers" = Table.PromoteHeaders(Table.Skip(#"Removed Custom", HeaderRowIndex), [PromoteAllScalars=true])
in
#"Promoted Headers"




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






View solution in original post

5 REPLIES 5
bhanu_gautam
Super User
Super User

@Anonymous 
Add a custom column to identify the row containing "KPI ID".
Filter the table to find the row number of the header.
Use that row number to set the headers dynamically.

 

Try using below M code

m
let
Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "IsHeader", each if Text.Contains([Column1], "KPI ID") then "Header" else null),
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Added Index", each ([IsHeader] = "Header")),
HeaderRowIndex = Table.FirstValue(#"Filtered Rows")[Index],
#"Removed Custom" = Table.RemoveColumns(#"Added Index",{"IsHeader", "Index"}),
#"Promoted Headers" = Table.PromoteHeaders(Table.Skip(#"Removed Custom", HeaderRowIndex), [PromoteAllScalars=true])
in
#"Promoted Headers"




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






mh2587
Super User
Super User

Try this one might help you
HeaderRowIndex = List.PositionOf(
Table.Column(Sheet, "Column1"),
"KPI ID"
)


✔ Answered? Mark as solution

Muhammad Hasnain | Super User • Fabric • Power BI • Data Engineering

Let's connect on LinkedIn
Anonymous
Not applicable

I think it is working thanks


Custom1 = List.PositionOf(
Table.Column(#"Removed Columns", "Custom^Column1"),
"KPI ID"
),
#"Removed Top Rows" = Table.Skip(#"Removed Columns", Custom1),
#"Promoted Headers1" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),

Anonymous
Not applicable

I've got this result which saying correct location of header row. 

what is the next pls? 

 

jeongkim_0-1751469725527.png

 

Greg_Deckler
Community Champion
Community Champion

@Anonymous If you are confident that at least one of the column headers will never change, you could identify the header row by looking for that column header name to dynamically identify the header row. If you post that sample data as text it will be easier for folks to create a solution for you.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.