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
MichaelF1
Helper III
Helper III

Power query: How to reference the row below a key word

I'm trying to manipulate some data from an accounting spreadsheet.

 

I need to retain all lines that include the word "Budget" and the line directly below that word.

 

What I have:

(The cells xxxx are numbers that need to be deleted)

 

DateColumn1
  
01/07/2023Budget
01/07/202367250
01/07/2023xxxx
01/07/2023xxxx
01/07/2023xxxx
02/07/2023Budget
02/07/202367100
02/07/2023xxxx
02/07/2023xxxx
02/07/2023xxxx
02/07/2023xxxx
03/07/2023Budget
03/07/202367000
03/07/2023xxxx
04/07/2023Budget
04/07/202367500
04/07/2023xxxx
04/07/2023xxxx

 

What I need:

 

01/07/2023Budget
01/07/202367250
02/07/2023Budget
02/07/202367100
03/07/2023Budget
03/07/202367000
04/07/2023Budget
04/07/202367500

 

Thanks in advance 🙂

1 ACCEPTED SOLUTION
mlsx4
Memorable Member
Memorable Member

Hi @MichaelF1 

 

It could be a little messy but this is my approach:

let
    Origen = Excel.Workbook(File.Contents("C:\Users\Equipo2\Downloads\ex.xlsx"), null, true),
    Hoja1_Sheet = Origen{[Item="Hoja3",Kind="Sheet"]}[Data],
    #"Encabezados promovidos" = Table.PromoteHeaders(Hoja1_Sheet, [PromoteAllScalars=true]),
    #"Tipo cambiado" = Table.TransformColumnTypes(#"Encabezados promovidos",{{"Date", type date}, {"Column1", type any}}),
    IndexStep = Table.AddIndexColumn(#"Tipo cambiado", "Index", 0, 1, Int64.Type),
    #"Personalizada agregada" = Table.AddColumn(IndexStep, "personalizado", each try if [Column1]="Budget" then IndexStep[Column1]{[Index] +1} else null otherwise null),
    #"Rellenar hacia abajo" = Table.FillDown(#"Personalizada agregada",{"personalizado"}),
    #"Tipo cambiado1" = Table.TransformColumnTypes(#"Rellenar hacia abajo",{{"personalizado", type text}, {"Column1", type text}}),
    #"Columna condicional agregada" = Table.AddColumn(#"Tipo cambiado1", "Personalizado.1", each if [Column1] = "Budget" then [Column1] else if [Column1] = [personalizado] then [Column1] else null),
    #"Filas filtradas" = Table.SelectRows(#"Columna condicional agregada", each ([Personalizado.1] <> null)),
    #"Columnas quitadas" = Table.RemoveColumns(#"Filas filtradas",{"Index", "Personalizado.1", "personalizado"})
in
    #"Columnas quitadas"

 

1. Add index column

mlsx4_0-1692703276938.png

2. Add custom column

mlsx4_1-1692703306086.png

3. Fill down:

mlsx4_2-1692703347573.png

4. Change both types to text

mlsx4_3-1692703382542.png

5. Add a conditional column to check if it is budget or the same value:

mlsx4_4-1692703420074.png

6. Filter null values:

 

mlsx4_5-1692703441487.png

7. Remove unnecessary columns:

mlsx4_6-1692703469372.png

 

I don't know if it makes the trick but...that's the only thing that comes to my mind

View solution in original post

4 REPLIES 4
mlsx4
Memorable Member
Memorable Member

Hi @MichaelF1 

 

It could be a little messy but this is my approach:

let
    Origen = Excel.Workbook(File.Contents("C:\Users\Equipo2\Downloads\ex.xlsx"), null, true),
    Hoja1_Sheet = Origen{[Item="Hoja3",Kind="Sheet"]}[Data],
    #"Encabezados promovidos" = Table.PromoteHeaders(Hoja1_Sheet, [PromoteAllScalars=true]),
    #"Tipo cambiado" = Table.TransformColumnTypes(#"Encabezados promovidos",{{"Date", type date}, {"Column1", type any}}),
    IndexStep = Table.AddIndexColumn(#"Tipo cambiado", "Index", 0, 1, Int64.Type),
    #"Personalizada agregada" = Table.AddColumn(IndexStep, "personalizado", each try if [Column1]="Budget" then IndexStep[Column1]{[Index] +1} else null otherwise null),
    #"Rellenar hacia abajo" = Table.FillDown(#"Personalizada agregada",{"personalizado"}),
    #"Tipo cambiado1" = Table.TransformColumnTypes(#"Rellenar hacia abajo",{{"personalizado", type text}, {"Column1", type text}}),
    #"Columna condicional agregada" = Table.AddColumn(#"Tipo cambiado1", "Personalizado.1", each if [Column1] = "Budget" then [Column1] else if [Column1] = [personalizado] then [Column1] else null),
    #"Filas filtradas" = Table.SelectRows(#"Columna condicional agregada", each ([Personalizado.1] <> null)),
    #"Columnas quitadas" = Table.RemoveColumns(#"Filas filtradas",{"Index", "Personalizado.1", "personalizado"})
in
    #"Columnas quitadas"

 

1. Add index column

mlsx4_0-1692703276938.png

2. Add custom column

mlsx4_1-1692703306086.png

3. Fill down:

mlsx4_2-1692703347573.png

4. Change both types to text

mlsx4_3-1692703382542.png

5. Add a conditional column to check if it is budget or the same value:

mlsx4_4-1692703420074.png

6. Filter null values:

 

mlsx4_5-1692703441487.png

7. Remove unnecessary columns:

mlsx4_6-1692703469372.png

 

I don't know if it makes the trick but...that's the only thing that comes to my mind

Hi @Anonymous That worked great, thanks very much!

MichaelF1
Helper III
Helper III

Hi @Idrissshatila,  Unfortunately I need this to be a dynamic process as more data will be added in the future. Sorry, probably should have mentioned this in the original post.

Idrissshatila
Super User
Super User

Hello @MichaelF1 ,

 

an easy way would be to filter out the rows you don't need.

Idrissshatila_0-1692702188907.png

 

Idrissshatila_1-1692702196861.png

 

If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

Follow me on Linkedin
Vote For my Idea💡

 



Did I answer your question? Mark my post as a solution! Appreciate your Kudos
Follow me on LinkedIn linkedIn
Vote for my Community Mobile App Idea

Proud to be a Super User!




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