cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
NewBee117
Level: Powered On

How to update OR create new row in different table?

The following script updates an existing record in the rev_ForecastDetails table. Is it possible to also create a new row if the First(Filter... condition returns zero result? How? Thanks.

 

Patch(
    '[dbo].[rev_ForecastDetails]',
    First(
        Filter(
             '[dbo].[rev_ForecastDetails]',
             ForecastID = ThisItem.id And WeekNumber = 1
        )
     ),
     {Forecast: Value(TxtWk1.Text)}
);

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: How to update OR create new row in different table?

@NewBee117 

The ISEMPTY function can be used to check if a FILTER returns no records...  I also like the idea of using SET to store the record in advance.  It makes the code more readable IMHO.

 

Set(
forecastRecord,
First(Filter('[dbo].[rev_ForecastDetails]',ForecastID = ThisItem.id And WeekNumber = 1))
);

Patch( '[dbo].[rev_ForecastDetails]', If(
IsEmpty(forecastRecord),
Defaults('[dbo].[rev_ForecastDetails]'),
forecastRecord
), {Forecast: Value(TxtWk1.Text)} );

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

2 REPLIES 2
Dual Super User
Dual Super User

Re: How to update OR create new row in different table?

You'll need to duplicate the Filter statement to check for results in an If() and then put a Patch() with defaults('[dbo].[rev_ForcastDetails]'.  Like this.

if(First(
        IsEmpty(Filter(
             '[dbo].[rev_ForecastDetails]',
             ForecastID = ThisItem.id And WeekNumber = 1
        )
     )),
Patch(
    '[dbo].[rev_ForecastDetails]',
   defaults(
             '[dbo].[rev_ForecastDetails]'),
             ForecastID = ThisItem.id And WeekNumber = 1
        )
     ),
     {Forecast: Value(TxtWk1.Text)}
),
Patch(
    '[dbo].[rev_ForecastDetails]',
    First(
        Filter(
             '[dbo].[rev_ForecastDetails]',
             ForecastID = ThisItem.id And WeekNumber = 1
        )
     ),
     {Forecast: Value(TxtWk1.Text)}
)
);


-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Super User
Super User

Re: How to update OR create new row in different table?

@NewBee117 

The ISEMPTY function can be used to check if a FILTER returns no records...  I also like the idea of using SET to store the record in advance.  It makes the code more readable IMHO.

 

Set(
forecastRecord,
First(Filter('[dbo].[rev_ForecastDetails]',ForecastID = ThisItem.id And WeekNumber = 1))
);

Patch( '[dbo].[rev_ForecastDetails]', If(
IsEmpty(forecastRecord),
Defaults('[dbo].[rev_ForecastDetails]'),
forecastRecord
), {Forecast: Value(TxtWk1.Text)} );

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,896)