web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Where should I place i...
Power Apps
Unanswered

Where should I place if statements to increment a counter?

(0) ShareShare
ReportReport
Posted on by

Hey everyone,

 

I am new to PowerApps and struggling with where to store some logical tests that I want to evaluate upon starting the app in order to determine the fill color of a status bubble. 

 

In App.OnStart, I have established a counter variable by using: Set(counter, 0)

 

I then would like to use several if statements to increment this counter and then a switch statement to determine the fill color based on the counter value. DaysUp, Off, and Rebooted are all fields from my datasource and I need the counter value to be unique to each data field. My if statements are:

If(DaysUp < 10, counter + 1);
If(Off = "true", counter + 1);
If(Rebooted = "true", counter + 1);

 

I would love feedback on my syntax to ensure it is correct as well as advice on where I should place these logical tests so that I can use my switch statement, Switch(counter, 1, Color.Yellow, 2, Color.Red, 3, Color.Red, Color.Green), to determine the fill color. 

 

Thank you in advance.

Categories:
I have the same question (0)
  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @Anonymous,

     

    You can initiate the variable in the OnStart as you currently do. When using a column in an If statement, the column will need to refer to a specific row (e.g. ThisItem in a gallery or a column of a certain record) -> simply referencing the column name will not work.

     

    Your Switch() looks good to me! The calculation of your variable can be done in one go:

    Set(
     counter,
     If(DaysUp < 10, 1) + If(Off = "true", 1) + If(Rebooted = "true", 1);
    )

    Do know that the column will need to be linked to a certain record (e.g. a record saved to a variable or a record in a gallery). Additionally should rebooted and Off be Yes/No or Boolean columns use If(Off, 1) - the column will return true or false.

     

    If this solves your question, would you be so kind as to accept it as a solution & give it a thumbs up.

    Thanks!

  • Community Power Platform Member Profile Picture
    on at

    Thank you so much for your detailed response,

     

    I really appreciate the simplification of my if-statements, I was wondering where I can place that within my app's properties so that my status bubble switch statement can evalute.

     

    When I try to place it in App. OnStart I get the error "Expected Operator". When I place it in my gallery default, I get "ThisItem not recognized" and "Invalid use of .". When I place it in my StatusBubble.fill, I get "Invalid arguments, received 9, expected 2"

     

    This is what I am most struggling with. Thanks!

  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @Anonymous,

     

    That depends on what you want to reach with the function. Do you want to evaluate 1 particular row in your table that then influences the status bubble or do you have a status bubble in a gallery and you want to evaluate it for each record?

  • Community Power Platform Member Profile Picture
    on at

    @LaurensM 

     

    I have a status bubble for each record in my gallery, whose fill color I want to be unique to that record. 

     

    Thanks!

  • Verified answer
    LaurensM Profile Picture
    12,516 Moderator on at

    @Anonymous okay I understand - thanks for the info! In this case we actually don't need a variable and can simply use the Fill property of the status bubble to change the color depending on the current record values.

     

    Fill property of the status bubble:

    With(
     {wCount: If(ThisItem.DaysUp < 10, 1) + If(ThisItem.Off = "true", 1) + If(ThisItem.Rebooted = "true", 1)},
     Switch(
     wCount,
     1,
     Color.Yellow,
     2,
     Color.Red,
     3,
     Color.Red,
     Color.Green
     )
    )

    Should you receive errors after ThisItem, remove the dot and column name. Write the dot again (e.g. ThisItem.) and see what columns Power Apps recommends.

     

    Additionally, keep the comment regarding yesNo/boolean fields in mind. Should 'ThisItem.Off = "true"' result in an error, you could try If(ThisItem.Off, 1) (same applies for rebooted).

     

    I hope this helps!

  • Community Power Platform Member Profile Picture
    on at

    @LaurensM Wow, this works exactly as intended! I had been fiddling with it for hours, thank you so much. 

     

    It worked exactly as you provided, thank you! Solved.

  • LaurensM Profile Picture
    12,516 Moderator on at

    Glad I could help! 😊

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard