top of page

How to Insert Blank Rows After Every Group in Excel and Power Query

Here is how to insert blank rows after each group, sample data:

OrderID

OrderDate

Product

Category

Quantity

NetPrice

1001

2025-01-01

Laptop

Electronics

1

60000

1002

2025-01-02

Smartphone

Electronics

2

42000

1003

2025-01-03

Headphones

Accessories

3

4200

1004

2025-01-04

Laptop

Electronics

1

65000

1005

2025-01-05

Smartphone

Electronics

1

21000

1012

2025-01-12

Headphones

Accessories

4

5500

With Excel's Dynamic Array Functions:

=LET(
    Table, Sales,
    u, UNIQUE(Sales[Product]),
    n, 1,
    AppendBlanks, REDUCE(
        Sales[#Headers],
        u,
        LAMBDA(acc, curr,
            LET(
                a, FILTER(Sales[#Data], Sales[Product] = curr),
                b, EXPAND({""}, n, COLUMNS(a), ""),
                VSTACK(acc, a, b)
            )
        )
    ),
    DROP(AppendBlanks, -n)
)

PowerQuery:

let
    Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    ChangedType = Table.TransformColumnTypes (
        Source,
        {
            {"OrderID", Int64.Type}, 
            {"Product", type text}, 
            {"Category", type text}, 
            {"Quantity", Int64.Type}, 
            {"NetPrice", Int64.Type}, 
            {"OrderDate", type date}
        }
    ), 
    Group = Table.Group (
        ChangedType, 
        "Product", 
        { 
            "G", 
            each _ & Table.TransformColumns ( 
                Table.FirstN ( _, 1 ), 
                {}, 
                each null 
            ) 
        }
    ), 
    Combine = Table.Combine ( Group[G] ), 
    ChangeTypeCustom = 
        let
            a = Type.TableRow ( Value.Type ( ChangedType ) ), 
            b = Type.RecordFields ( a ), 
            c = Record.ToList ( b ), 
            d = List.Transform ( c, ( r ) => r[Type] ), 
            e = List.Zip ( { Record.FieldNames ( b ), d } )
        in
            Table.TransformColumnTypes ( Combine, e ),
    RemoveLastBlankRow = Table.RemoveLastN ( ChangeTypeCustom, 1 )
in
    RemoveLastBlankRow


Comments


bottom of page