cancel
Showing results for
Did you mean:
New Member

## i don't know how to apply Switch statement to bmi calculator

Hello, 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")).

1 ACCEPTED SOLUTION

Accepted Solutions
New Member

Thank you all for your help, the calculation error is from my end.
Below is the correct equation
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"
)
)

5 REPLIES 5
Super User

Hi @ojobabs

The Switch function looks for an exact match, therefore, a simple way to do this would be to use if statements.

With this example, you could set the text property of a label to the following formula to show the BMI result.

``````With({res:Value(tx1.Text)/(Value(tx2.Text)^2)*703},
If(res<=18.5, "Underweight",
res<=24.5, "Normal",
res<=30, "Overweight",
"Obese"
)
)

``````

New Member

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.

Super User

Your With statement is incorrect. Also there is no need for Value() arithmetic operators automatically convert text to numbers in PowerApps.

``````With({res:(tx1.Text) / (tx2.Text) ^ 2 * 703},
If(res <=18.5, "Underweight",
res <=24.5, "Normal",
res <=30, "Overweight",
"Obese"
)
)``````

Super User

The only thing that might be wrong here is the placing of the parenthesis.

``````With({res:(tx1.Text / (tx2.Text) ^ 2) * 703},
If(res <=18.5, "Underweight",
res <=24.5, "Normal",
res <=30, "Overweight",
"Obese"
)
)``````

Whilst, @Drrickryp is correct in that PowerApps will implicitly convert text to numbers, I used Value to explicitly carry out the conversion to avoid any ambiguity.

If that still doesn't work, I would set the text property of a label to the following formula to investigate whether the underlying calculation produces the expected value:

(tx1.Text / (tx2.Text) ^ 2) * 703

New Member

Thank you all for your help, the calculation error is from my end.
Below is the correct equation
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"
)
)

Announcements

#### 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. Our team will be reviewing posts using the new "Copilot" label to ensure we highlight and amplify the most relevant and recent content, so you're assured of high-quality content every time you visit. If you share a post that gets featured in the curated gallery, you'll get a PM in the Community to let you know!The curated gallery is ready for you to experience now, so visit the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community. We can't wait to see what you "cook" up!