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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
samahiji
Frequent Visitor

Fill in BUS Name column from a header

Hi,

I have the table below in Excel that schedules different buses as per their rout. the column BUS has to be filled with bus name extracted from a header on top of each ROUT (NEXT BUS -) using Power Query.

 

NEXT BUS - ABC 123 (24-HR)  
ROUTEAREACODESBUS
9001G13CYABC 123
9002E13OYABC 123
9012F12OYABC 123
9003D12CYABC 123
     
NEXT BUS - ABC 456 (24-HR)  
ROUTEAREACODESBUS
9109C21CYABC 456
9110E16CYABC 456
     
NEXT BUS - ABC 789 (24-HR)  
ROUTEAREACODESBUS
9212E14OYABC 789
9223C6OYABC 789
9200C7CYABC 789
2 ACCEPTED SOLUTIONS
AlienSx
Super User
Super User

fromfrom

 

 

 

 

 

 

 

 

let
    fx =(tbl) => Table.FromColumns(
        Table.ToColumns(tbl) & 
        {
            {null, "BUS"} & 
            List.Repeat(
                {Text.BetweenDelimiters(Table.FirstValue(tbl), "- ", " (")}, 
                List.Count(List.Select(tbl[Column1], (x) => x is number))
            )
        }
    ),
    Source = Excel.CurrentWorkbook(){[Name="buses"]}[Content],
    group = Table.Group(
        Source, 
        "Column1", 
        {"x", fx}, 
        GroupKind.Local, 
        (s, c) => Number.From(c is text and Text.StartsWith(c, "NEXT BUS"))
    ),
    result = Table.Combine(group[x])
in
    result

 

 

 

toto

View solution in original post

Anonymous
Not applicable

Hi,

Thanks for the solution @AlienSx  and @Omid_Motamedise  offered, and i want to offer some more information for user to refer to.

hello @samahiji , you can create a blank query and put the following code to advanced editor in power query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDLCsIwEEV/ZchKoYWZaWzNso+oKwN9gCX0/3/DZFSadmcXk3DPhHC43qunfY3QTAPkUDctEBdwYp0/+rPKFHxHLZlXvZtGG0Ld2zpcretiGmRnECmEOxVxE2b+YQ7BCnYJpohvcqYY47tO8PoJJCIgZOesL+UhZ0ITGdPWmQjFufzPorqaQxZMn4r0tgtmqbLcUYxubbWqLW8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1],"NEXT BUS") then [Column1] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Down",each [Custom],each if Text.Contains([Column1],"NEXT BUS") or [Column1]=" " then "" else if [Column1]="ROUTE" then "BUS" else Text.BetweenDelimiters([Custom],"-","(") ,Replacer.ReplaceValue,{"Custom"}),
    #"Trimmed Text" = Table.TransformColumns(#"Replaced Value",{{"Custom", Text.Trim, type text}})
in
    #"Trimmed Text"

Output

vxinruzhumsft_0-1730860289359.png

Best Regards!

Yolo Zhu

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

3 REPLIES 3
Anonymous
Not applicable

Hi,

Thanks for the solution @AlienSx  and @Omid_Motamedise  offered, and i want to offer some more information for user to refer to.

hello @samahiji , you can create a blank query and put the following code to advanced editor in power query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDLCsIwEEV/ZchKoYWZaWzNso+oKwN9gCX0/3/DZFSadmcXk3DPhHC43qunfY3QTAPkUDctEBdwYp0/+rPKFHxHLZlXvZtGG0Ld2zpcretiGmRnECmEOxVxE2b+YQ7BCnYJpohvcqYY47tO8PoJJCIgZOesL+UhZ0ITGdPWmQjFufzPorqaQxZMn4r0tgtmqbLcUYxubbWqLW8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1],"NEXT BUS") then [Column1] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Down",each [Custom],each if Text.Contains([Column1],"NEXT BUS") or [Column1]=" " then "" else if [Column1]="ROUTE" then "BUS" else Text.BetweenDelimiters([Custom],"-","(") ,Replacer.ReplaceValue,{"Custom"}),
    #"Trimmed Text" = Table.TransformColumns(#"Replaced Value",{{"Custom", Text.Trim, type text}})
in
    #"Trimmed Text"

Output

vxinruzhumsft_0-1730860289359.png

Best Regards!

Yolo Zhu

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

 

Omid_Motamedise
Super User
Super User

you should use the custom name in excel starting from the row before each table and then use the following formula to access to the buss name.

 

Source[Column1]{0}

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
AlienSx
Super User
Super User

fromfrom

 

 

 

 

 

 

 

 

let
    fx =(tbl) => Table.FromColumns(
        Table.ToColumns(tbl) & 
        {
            {null, "BUS"} & 
            List.Repeat(
                {Text.BetweenDelimiters(Table.FirstValue(tbl), "- ", " (")}, 
                List.Count(List.Select(tbl[Column1], (x) => x is number))
            )
        }
    ),
    Source = Excel.CurrentWorkbook(){[Name="buses"]}[Content],
    group = Table.Group(
        Source, 
        "Column1", 
        {"x", fx}, 
        GroupKind.Local, 
        (s, c) => Number.From(c is text and Text.StartsWith(c, "NEXT BUS"))
    ),
    result = Table.Combine(group[x])
in
    result

 

 

 

toto

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors
Top Kudoed Authors