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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Ackbar-Learner
Resolver I
Resolver I

How to generate new lines of data by subcategories

Hi I need help to do the following. I have the below two tables. What i need is that each line of the first table to repeat itself X times at each change in transaction date but the second table department data should come as a new column in the first table for each line. (X being being the number of lines in the second table)

 

AckbarLearner_0-1693122892951.png

 

AckbarLearner_1-1693122906254.png

For example, the first line in first table with date 01/07/2021 should repeat six times and each of the 6 lines should contain 1 department in a new column named Department.

 

Thank you in advance

 

1 ACCEPTED SOLUTION

or even simplier 

let
    Table1 = your_table1_name,
    Table2 = your_table2_name[Department],
    add_col = Table.AddColumn(Table1, "Department", each Table2),
    expand = Table.ExpandListColumn(add_col, "Department")
in
    expand

View solution in original post

5 REPLIES 5
AlienSx
Super User
Super User

Hi, @Ackbar-Learner 

    size = Table.RowCount(Table2),
    rows = Table.ToRows(Table1),
    txform = 
        List.Transform(
            rows,
            (x) => 
                [a = List.Repeat({x}, size),
                b = Table.FromColumns(Table.ToColumns(Table.FromRows(a)) & {Table2[Department]}, Table.ColumnNames(Table1) & {"Department"})][b]
        ),
    z = Table.Combine(txform)

Hi @AlienSx 

 

Thanks for your reply. I am a bit new to this M power query code. I was like following the applied steps in power query so that I am more at ease understanding what is happening to the data. So I am not sure where to insert the code you provided 😞 

 

Kindly advise


Thanks

@Ackbar-Learner create new (blank) query, run Advanced Editor and replace everything inside with this

let
    Table1 = your_table1_name,
    Table2 = your_table2_name,
    size = Table.RowCount(Table2),
    rows = Table.ToRows(Table1),
    txform = 
        List.Transform(
            rows,
            (x) => 
                [a = List.Repeat({x}, size),
                b = Table.FromColumns(Table.ToColumns(Table.FromRows(a)) & {Table2[Department]}, Table.ColumnNames(Table1) & {"Department"})][b]
        ),
    z = Table.Combine(txform)
in
    z

don't forget to replace your_table1_name and your_table2_name (2nd and 3rd rows of code) with the names of your tables/queries.  

or even simplier 

let
    Table1 = your_table1_name,
    Table2 = your_table2_name[Department],
    add_col = Table.AddColumn(Table1, "Department", each Table2),
    expand = Table.ExpandListColumn(add_col, "Department")
in
    expand

So easy in the end 🙂

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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