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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Pertemhru
New Member

Need Help with M Code

Hi, Guys!

I need to insert a new row into a table based on a conditional check of the highest date in this table with the Date field in another table.
The new row should only be inserted if the maximum Date in the table is greater than the Date in the other table (which is unique).
It would basically be an auto-increment of new rows as the values ​​are different.

6 REPLIES 6
Omid_Motamedise
Super User
Super User

try this approach (if it not clear, let me know to provide you an example)
Find the max date from the first table (MainTable).
Compare it with the date in the second table (ReferenceTable).
If the max date is greater, add a new row with the necessary values.
Append the new row (if needed) to the existing table.

First of all, thank you all very much!

I will be more specific about the problem itself. I apologize for the unclear request.

I have these two tables in BI:

1st - Table Total

Final Status Count Update_Date
Completed 36800 02/21/2025

 

2nd - Table Evolution

Final Status Count Update_Date
Completed 36800 02/21/2025
Completed 38123 02/22/2025

 

In the first table there will always be only one row that will be updated frequently, with the fields "update date" and "count" being changed.

According to verification, new rows will be inserted in the second table each time the first table is updated. This will increase over time and accumulate the update values.
I tried in some ways to perform the conditional check and insert the new row using the Table.InsertRows function, but Microsoft Power BI always issues error messages. I tried more than one code.

MarkLaf
Solution Sage
Solution Sage

As noted, it's a little ambiguous as to what you want. What if there are multiple later dates in the other table? When you say "increment", do you really mean adding a single date, or do you mean adding all dates up to the latest in the other table? To give you an idea, I walked through a few different options.

 

Given the starting table that we plan to increment and the other table we want to check against...

 

Increment:

DateColumn AColumn B
1/1/2024a1
3/1/2024b2
6/1/2024c3

 

Check:

DateColumn CColumn D
1/1/2024a120
3/1/2024b130
6/1/2024c112
6/3/2024d110
6/10/2024e160

 

Here is some M that figures out potential different lists of dates you may want to consider the "increment" to add and converts them into Increment table rows (as a table) that you can easily add to Increment (i.e. with Table.Combine)

 

 

let
    //A function that converts list of dates into same dates in Increment table schema.
    //If input is an empty list, we just get an empty table as output.
    DatesToIncrRows = 
        (dates as list) as table => 
        Table.FromRecords(
            List.Transform( dates, each [Date=_] ), 
            Value.Type( Increment ), 
            MissingField.UseNull 
        ),
    
    //Get max date from Increment.
    MaxFromIncr = List.Max( Increment[Date] ),

    //Get ALL dates in other table later than Increment max.
    //Will be empty list if no later dates.
    AllLaterFromCheck = 
        List.Select( Check[Date], each _ > MaxFromIncr ),

    //Get max date out of dates later than Increment max.
    //Will be null if AllLaterFromCheck is empty list {}.
    MaxFromLater = List.Max( AllLaterFromCheck ),
    
    //If a MaxFromLater exists, wrap in list to work with our function.
    //Otherwise, return empty list {}.
    MaxToList = 
        if MaxFromLater = null then {} else { MaxFromLater },

    //Get all dates between Increment max (exclusive) and Check max (inclusive).
    //Or return empty list if no later dates exist in Check.
    AllBetweenIncrAndCheck = 
        if MaxFromLater = null then {} else List.Dates( 
            Date.AddDays( MaxFromIncr, 1 ), 
            Duration.Days( List.First(MaxToList) - MaxFromIncr ), 
            #duration(1,0,0,0) 
        ),

    //Sticking different options into a record for easy access.
    //Use this in other queries with reference like: IncrementOptions[All Later Dates From Check].
    Options = [
        Only Latest Date In Check = DatesToIncrRows( MaxToList ),
        All Later Dates From Check = DatesToIncrRows( AllLaterFromCheck ),
        All Later Dates Between Check And Incr = DatesToIncrRows( AllBetweenIncrAndCheck )
    ]

in
    Options

 

 

Output:

MarkLaf_0-1740104489163.png

 

And to be clear, the idea would be to union the desired output with your original Increment using Table.Combine to get the final table. And note that Table.Combine( { Whatever Table, Empty Table } ) = Whatever Table - i.e. it handles the case where there are no later dates in Check. Example where the above query is called IncrementOptions:

 

 

= Table.Combine( { Increment, IncrementOptions[All Later Dates Between Check And Incr] } )

 

 

MarkLaf_1-1740104669921.png

First of all, thank you all very much!

I will be more specific about the problem itself. I apologize for the unclear request.

I have these two tables in BI:

1st - Table Total

Final StatusCountUpdate_Date
Completed3680002/21/2025


2nd - Table Evolution

Final StatusCountUpdate_Date
Completed3680002/21/2025
Completed3812302/22/2025


In the first table there will always be only one row that will be updated frequently, with the fields "update date" and "count" being changed.

According to verification, new rows will be inserted in the second table each time the first table is updated. This will increase over time and accumulate the update values.
I tried in some ways to perform the conditional check and insert the new row using the Table.InsertRows function, but Microsoft Power BI always issues error messages. I tried more than one code.

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

First of all, thank you all very much!

I will be more specific about the problem itself. I apologize for the unclear request.

I have these two tables in BI:

1st - Table Total

Final StatusCountUpdate_Date
Completed3680002/21/2025


2nd - Table Evolution

Final StatusCountUpdate_Date
Completed3680002/21/2025
Completed3812302/22/2025


In the first table there will always be only one row that will be updated frequently, with the fields "update date" and "count" being changed.

According to verification, new rows will be inserted in the second table each time the first table is updated. This will increase over time and accumulate the update values.
I tried in some ways to perform the conditional check and insert the new row using the Table.InsertRows function, but Microsoft Power BI always issues error messages. I tried more than one code.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors