cancel
Showing results for
Did you mean:
Kudo Collector

## Find next available number in a list?

I need to find the next available number in a list. This is not the same as finding the highest number and adding 1. For example, I have the following numbers in my database:

• 1000011
• 1000045
• 1005646
• 1006565
• 1568795
• 1894834

The first and last digit are meaningful, but the middle 5 are not. So the "core numbers" would be:

• 00001
• 00004
• 00564
• 00565
• 56879
• 89483

In Excel, I'd simply use PowerQuery to generate a list of numbers 00001 - 99999, then do an antijoin to only keep numbers in my big list that aren't in the original list, then pick the minimum number. Which, in this case, would be 00002.

If that were added to the database then, 00003 would be next, then 00005, as an 00004 already exists.

Getting the core number in PowerApps is easy - the MID() function.

I cannot figure out how to do the logic though to start moving through those numbers to find the first available number.

Ideas where to start?

1 ACCEPTED SOLUTION

Accepted Solutions
Power Apps

This can be done natively in PowerApps, using a logic similar to the one you described. The main problem here is to create the list of numbers from 1 to 99999 - there's nothing in PowerApps that can do that, but you can do it "by hand" (or using code) by using this construct:

`[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,...]`

Here's the expression that would give you the "next" number (assuming that the numbers are stored in a collection or a table called 'numbersInDatabase'):

```First(
Filter(
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
numbersInDatabase,
"CoreValue",
Value(Mid(Text(Value), 2, Len(Text(Value)) - 2))).CoreValue)),
).Value```

The idea is to extract the value from the original table, and use the Not operator with the in operator to implement the anti-join pattern that you mentioned, then taking the first of the remaining values.

The attached app shows how this can be implemented; to open it save it locally, then go to https://create.powerapps.com, select Open, then Browse, then find the file that you saved before.

Another alternative that you can use instead of creating a list of all 100k numbers, is to have a list of ~100 (or 1000) numbers that start with the next available number, and every time you add a new number, you "shift" the list to start from that number. The attached app also shows this alternative.

Hope this helps!

11 REPLIES 11
Community Champion

Hi @EdHansberry ,

Initially, i'm thinking of the string manipulation functions like Split(), Len(), Right(), Left() etc. You can wrap the results of these manipulations in Value() to perform numeric calculations on them once you have em' cornered.

Good luck!
Let us know what you end up with! Thats a cool problem

Kudo Collector

Yeah. Converting from numbers to text and back are not the problem. A Do-While loop would work, but that doesn't exist in PowerApps. Nor, to my knowledge, the ability to create an array of sequential numbers. Kinda stumped.

Impactful Individual

You say database.  What type of database?  Is it SQL Server?  You could write a stored procedure and call it from Flow.

Kudo Collector

Right now it is an Excel file as the database. It will be in SQL Server, but I don't know anything about writing stored procedures, or doing SQL stuff natively in SQL server. I use SSMS sparingly, and use PowerQuery for all of my data transformational needs.

I'm hoping for a native PowerApps solution.

Impactful Individual

Doing what you want requires a procedural language.  The M-Language is pretty powerful and provides a lot of interesting capabilities.  Stored procedures are not that difficult to learn and if you are going to use SQL Server for anything significant you are likely going to need them for other purposes.

Community Champion

Mmmm. I’m fairly certain you can do something like (pseudo code here cuz I’m on my phone):
Sort(
Mid(<midStuff>), Ascending)
)

Basically, for all the numbers in the given column, peel off the first and last numbers, sort the results in ascending order.
I’m fact, I’m certain I’ve seen this somewhere in the forums here.

Community Champion
Power Apps

This can be done natively in PowerApps, using a logic similar to the one you described. The main problem here is to create the list of numbers from 1 to 99999 - there's nothing in PowerApps that can do that, but you can do it "by hand" (or using code) by using this construct:

`[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,...]`

Here's the expression that would give you the "next" number (assuming that the numbers are stored in a collection or a table called 'numbersInDatabase'):

```First(
Filter(
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
numbersInDatabase,
"CoreValue",
Value(Mid(Text(Value), 2, Len(Text(Value)) - 2))).CoreValue)),
).Value```

The idea is to extract the value from the original table, and use the Not operator with the in operator to implement the anti-join pattern that you mentioned, then taking the first of the remaining values.

The attached app shows how this can be implemented; to open it save it locally, then go to https://create.powerapps.com, select Open, then Browse, then find the file that you saved before.

Another alternative that you can use instead of creating a list of all 100k numbers, is to have a list of ~100 (or 1000) numbers that start with the next available number, and every time you add a new number, you "shift" the list to start from that number. The attached app also shows this alternative.

Hope this helps!

Community Support

Hi @EdHansberry ,

Could you please share a bit more the data structure of your Original List and Big List?

Further, do you want to find the next available minimum number within your Original List?

I assume that you add a Text type column to store the list of numbers 00001 - 99999 in your Big List, is it true?

I have made a test on my side, please take a try with the following workaround:

Set the OnStart property of the App control or OnVisible property of the first screen of your app to following:

`ClearCollect(OriginalList, 1000011, 1000045, 1005646, 1006565, 1568795, 1894834);ClearCollect(BigList, "00001", "00002", "00003", "00004", "00005", "00007", "00006","00008","00010","00009")`

Set the Text property of the Label control to following:

`First(       SortByColumns(                     AddColumns(Filter(BigList, Not(Text(Value) in Mid(AddColumns(OriginalList,"TextValue",Text(Value)).TextValue,2,5))), "IntegerValue", Value(Value)),                     "IntegerValue",                      Ascending       )).Value`

On your side, you should type the following:

```First(
SortByColumns(
"IntegerValue",
Ascending
)
).BigNumberColumn```

Note: The BigNumberColumn represents the Text type column in your Big List, which used to store the list of numbers 00001 - 99999. The OriginalNumberColumn represents the column in your Original List to store the original number, e.g. 1000011, 1000045, 1005646, 1006565, ... etc.

Please take a try with above solution, then check if it help in your scenario.

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Announcements

#### Celebrating the May Super User of the Month: Laurens Martens

@LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!

#### Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

#### Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!

#### Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!