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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
md8422
Frequent Visitor

M query add new rows based on a condition

Hi All,

 

I've been hitting a wall for the last few days on this problem, maybe somebody here has an idea on how to solve it.

 

I have 2 tables: Records and Requirements.

The Records contains which attribute are present for a specific object and their values.

Each object is part of a Class

 

The other table "Requirements" store all the attributes required for a specific object class

 

I want to create a table in M query where for each object_id, I'll add new rows in Records with the missing attributes required for that specific class.

 

Here the example:

 

Any idea? 

 

 

THanks!

2022-06-20 14_49_00-Book1 - Excel.png

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @md8422 ,

 

Start with the Requirements table and crossjoin a distinct list of [object_id] by adding a custom column something like this:

Table.Distinct(
    Table.SelectColumns(
        Records,
        {"object_id"}
    )
)

 

Then merge this with your Records table on Requirements[attribute], [object_id] = Records[attribute], [object_id].

Expand your merge column to add the [Value] field.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
BA_Pete
Super User
Super User

Hi @md8422 ,

 

Start with the Requirements table and crossjoin a distinct list of [object_id] by adding a custom column something like this:

Table.Distinct(
    Table.SelectColumns(
        Records,
        {"object_id"}
    )
)

 

Then merge this with your Records table on Requirements[attribute], [object_id] = Records[attribute], [object_id].

Expand your merge column to add the [Value] field.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thanks @BA_Pete for your answer... just one question: is there a better way than using crossjoin? .... expecially if these table contains +1000 rows each ?

 

Thanks again 

 

Hi @md8422 ,

 

This is the quickest way I can think of to get your desired output.

To try and do it the way you were thinking, i.e. match conditions and insert a row, would essentially require thousands of scans and insert row operations.

Try my solution and let me know how you get on. I think the crossjoin performance will be better than you are expecting.

You could also potentially speed it up by buffering the table to crossjoin:

Table.Buffer(
    Table.Distinct(
        Table.SelectColumns(
            Records,
            {"object_id"}
        )
    )
)

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.