Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

Increment number without duplicating

(1) ShareShare
ReportReport
Posted on by 745
Hello,
 
I am trying to create an expression to generate incremented text numbers from a ComboBox that is connected to a Dataverse Choice column, without creating duplicate text numbers.
 
I was using this expression below:
 
DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-" & 
   If(
      IsBlank(
         Max(
            Filter(
               Table,
               StartsWith(Column, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
            ),
            Value(Mid(Column, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(TrialCode)))
         )
      ),
      1,
      Max(
         Filter(
            Table,
            StartsWith(Column, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
         ),
         Value(Mid(Column, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(TrialCode)))
      ) + 1
   )
 
 
Below is the values of the ComboBox.
 
["Dave", "Joe", "John", "Emily", "James", "Joe"]
 
 
Below is the example of the output I am trying to achieve.
Example :
 
Dave25-1
Joe25-2
John25-3
John25-4
Emily25-5
James25-6
Joe25-7
  • WarrenBelz Profile Picture
    WarrenBelz 145,580 on at
    Increment number without duplicating
    I would need to build a model, which I cannot do presently - @mmbr1606 might want to have a go at this.
  • Caleb62881177 Profile Picture
    Caleb62881177 745 on at
    Increment number without duplicating
    Hi @WarrenBelz, Thank you for your response.

    The number doesn’t increment unless the value before the "-" is already present in Dataverse.
     
    For example:
     
    ["Dave", "Joe", "John", "Emily", "James", "Joe"]
     
    I get this
     
    Dave25-1
    Joe25-1
    John25-1
    John25-2
    Emily25-1
    James25-1
    Joe25-2
     
    Instead of that
     
    Dave25-1
    Joe25-2
    John25-3
    John25-4
    Emily25-5
    James25-6
    Joe25-7
     
    I hope this gives more clarity
  • WarrenBelz Profile Picture
    WarrenBelz 145,580 on at
    Increment number without duplicating
    Try this structure
    With(
       {
          _Year: 
          Mod(
             Year(Today()),
             100
          ) & "-",
          _Name: DataCardValue7.Selected.Name
       },
       With(
          {
             _Code: 
             Filter(
                'AGTRS Trial Request Tables',
                StartsWith(
                   TrialCode,
                   _Name & _Year
                )
             )
          },
          _Name & _Year & 
          With(
             {
                _Max: 
                Max(
                   _Code,
                   Value(
                      Mid(
                         TrialCode,
                         Len(_Name & _Year) + 1,
                         Len(TrialCode)
                      )
                   )
                )
             },
             If(
                IsBlank(_Max),
                1,
                _Max + 1
             )
          )
       )
    )

    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee

     
  • Caleb62881177 Profile Picture
    Caleb62881177 745 on at
    Increment number without duplicating
    Hi @WarrenBelz, Sorry for responding now.
     
     
    This is my original formula 
    DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-" & 
       If(
          IsBlank(
             Max(
                Filter(
                   'AGTRS Trial Request Tables',
                   StartsWith(TrialCode, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
                ),
                Value(Mid(TrialCode, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(TrialCode)))
             )
          ),
          1,
          Max(
             Filter(
                'AGTRS Trial Request Tables',
                StartsWith(TrialCode, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
             ),
             Value(Mid(TrialCode, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(TrialCode)))
          ) + 1
       )
     
    I am getting this error with the formula you have provided.
    Invalid number of arguments: received 3, expected 2.
     
    With(
       {
          _Year: Mod(Year(Today()), 100) & "-",
          _Name: DataCardValue7.Selected.Name
       },
       With(
          {
             _Table:
             Filter(
                'AGTRS Trial Request Tables',
                StartsWith(TrialCode, 
                   _Name & _Year,
                )
             )
          },
          _Name & _Year & 
          If(
             CountRows(_Table) = 0
             Value(
                Mid(
                   TrialCode, 
                   Len(_Name & _Year) + 1, 
                   Len(TrialCode)
                )
             ),
             1,
             Max(
                _Table,
                Value(
                   Mid(
                      TrialCode, 
                      Len(_Name & _Year) + 1, 
                      Len(TrialCode)
                   )
                ) + 1
             )
          )
       )
    )
    I hope that helps
     
  • WarrenBelz Profile Picture
    WarrenBelz 145,580 on at
    Increment number without duplicating
    Just adding some input here, mainly to get rid of all that duplicated code. One other comment is your reference to column in the text part of the formula does not refer to a specific record.
    With(
       {
          _Year: Mod(Year(Today()), 100) & "-",
          _Name: DataCardValue7.Selected.Name
       },
       With(
          {
             _Table:
             Filter(
                Table,
                StartsWith(
                   Column, 
                   _Name & _Year
                )
             )
          },
          _Name & _Year & 
          If(
             CountRows(_Table) = 0,
             Value(
                Mid(
                   Column, 
                   Len(_Name & _Year) + 1, 
                   Len(TrialCode)
                )
             ),
             1,
             Max(
                _Table,
                Value(
                   Mid(
                      Column, 
                      Len(_Name & _Year) + 1, 
                      Len(TrialCode)
                   )
                ) + 1
             )
          )
       )
    )
  • Caleb62881177 Profile Picture
    Caleb62881177 745 on at
    Increment number without duplicating
     
    I am getting the error of "The function 'Text' has some invalid arguments." when using the expression you provided.
  • Suggested answer
    mmbr1606 Profile Picture
    mmbr1606 10,450 on at
    Increment number without duplicating
    hey
     
    sorry copy/paste error
     
    pls try this:
    DataCardValue7.Selected.Name & 
        Text(Year(Today()) Mod 100) & 
        "-" & 
        (
            Max(
                ForAll(
                    Filter(
                        Table,
                        StartsWith(Column, DataCardValue7.Selected.Name & Text(Year(Today()) Mod 100) & "-")
                    ),
                    Value(Mid(Column, Len(DataCardValue7.Selected.Name & Text(Year(Today()) Mod 100) & "-") + 1, Len(Column)))
                )
            ) + 1
        )
    )
    
    cheers
  • Caleb62881177 Profile Picture
    Caleb62881177 745 on at
    Increment number without duplicating
    Hi @mmbr1606​​​​​​​,
     
    Your expression looks exactly the same as mine.
  • mmbr1606 Profile Picture
    mmbr1606 10,450 on at
    Increment number without duplicating
    hey
     
     
    can u give this a try:
    DataCardValue7.Selected.Name & 
        Mod(Year(Today()), 100) & 
        "-" & 
        If(
            IsBlank(
                Max(
                    Filter(
                        Table,
                        StartsWith(Column, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
                    ),
                    Value(Mid(Column, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(Column)))
                )
            ),
            1,
            Max(
                Filter(
                    Table,
                    StartsWith(Column, DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-")
                ),
                Value(Mid(Column, Len(DataCardValue7.Selected.Name & Mod(Year(Today()), 100) & "-") + 1, Len(Column)))
            ) + 1
        )
    
    if it helped please mark as verified answer
     
     
    cheers

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,580

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,909

Leaderboard