1

I recently started with Power Apps and one of the very first app I am building is a Body Mass Index (BMI) calculator. I was able to set it to automatically convert from Kg to pounds and cm to feet.

The problem I am facing now is outputting the result using If or Switch statement to show the BMI categories:

BMI Categories:
Underweight = <18.5
Normal weight = 18.5–24.9
Overweight = 25–29.9
Obesity = BMI of 30 or greater

The formula I intend to use is

BMI = 703* (weight/(Height^2))

This is the logic I used

Set(VarResult,If(tx1.Text/(tx2.Text^2)*703)=<18.5,"underweight")
if(VarResult>18.5,"Normal")
if(VarResult>=24.5,"Overweight")
if(VarResult>30, "Obese"))
Robert Dyjas
  • 4,438
  • 3
  • 12
  • 30

2 Answers2

1

Thank you for assisting with the if statement, the error is with the calculation after solving the if statement. The correct logic is

With(
    {res:Value(tx1.Text)/(Value(tx2.Text)^2)*10000},
     If(
        res>= 30, "Obese",
        res> 25 && res< 29.9, "Overweight",
        res> 18.5 && res< 24.9, "Normal",
        res<= 18.4, "Underweight"
      )
)

Thank you all.

0

Proper solution

NOTE: I'm using names WeightInput and HeightInput for my inputs, Their type is Text input. If your configuration is different, you might amend your formula to get value with proper type.

NOTE: To make the process clear, I'll use separate label element for each step. Then I'll be accessing the value of previous step by the name of the label.

Step 1: Get weight as number

The formula WeightInput.Text returns text value. We need to use Value() function to convert it to number:

Value(WeightInput.Text)

I save it to the label named WeightData.

Step 2: Get squared value of height

We already know how to get number. Let's repeat the same with the addition of Power() function (exponent value will be 2 in that case):

Power(Value(HeightInput.Text),2)

I save it to label named SquaredWeight

Step 3: Calculate BMI

Now we can use labels we saved and calculate BMI value:

703*(WeightData/SquaredWeight)

I save it as BMI.

Step 4: Return text value

Now, let's create another label which will contain the text value:

If(
    Value(BMI.Text) > 30,
    "Obese",
    Value(BMI.Text) >= 24.5,
    "Overweight",
    Value(BMI.Text) > 18.5,
    "Normal",
    Value(BMI.Text) < 18.5,
    "underweight"
)

Be careful with the order of conditions. If you do it the other way, you'll receive Normal for all values bigger than 18.5.

This is because in If function, first condition evaluated to true stops the execution of the function.

Why the existing solution doesn't work

I can see few issues with the existing code. Most of them can be easily noticed if you follow the steps above, but I think it might be nice to point them out:

  • No conversion from text to number
  • Incorrect use of multi-condition If
  • Mixing the number value with the descriptive value

To show what I mean by mixing the values. Here you save descriptive value to variable:

Set(VarResult,If(tx1.Text/(tx2.Text^2)*703)=<18.5,"underweight")

And later you try to compare it with number value:

if(VarResult>18.5,"Normal")
Robert Dyjas
  • 4,438
  • 3
  • 12
  • 30
  • You fully broke down the select statement, but your answer does not solve my problem. After running your code, I keep getting "underweight" for correct values. Furthermore, I am working on improving my calculations. Thanks for assisting, – Babatunde Victor Feb 10 '21 at 17:22
  • Are you able to isolate, which part of the solution is not working? Perhaps I made a typo while pasting code – Robert Dyjas Feb 10 '21 at 18:56
  • Not really, you absolutely got it right. I think the problem is from my end. This is javascript code I am trying to shorten to power apps if (bmi <= 18.4) { measure = "Your BMI is " + bmi + " which means " + "you are Underweight"; } else if (bmi >= 18.5 && bmi <= 24.9) { measure = "Your BMI is " + bmi + " which means " + "You are Normal"; } else if (bmi >= 25 && bmi <= 29.9) { measure = "Your BMI is " + bmi + " which means " + "You are Overweight"; } else if (bmi >= 30) { measure = "Your BMI is " + bmi + " which means " + "You are Obese"; } – Babatunde Victor Feb 10 '21 at 21:20