# Creating complex IF/THEN statements with the "Switch" formula

**URL:** <https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289>\
**Category:** Ask us anything\
**Tags:** formulas, binding\
**Created:** [June 29, 2023, 9:22pm UTC](https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289 "2023-06-29T21:22:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brandon](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/brandon/32/2804_2.png) [@brandon](https://community.weweb.io/u/brandon)\
**Post date:** [June 29, 2023, 9:22pm UTC](https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289/1 "2023-06-29T21:22:08Z")

</div>

Big thanks to the video that @Joyce [dropped on YouTube](https://www.youtube.com/watch?v=H8ZmKz_3bIc&t) yesterday showing some of the cool stuff you can do with the Switch formula!

As I understand it, the Switch formula is essentially a big “If THIS then THAT” statement that can include more than just one variable.

This works great when you know the exact possible values, but what if you are dealing with changing numbers?

**My Use Case**  
I’ve got a price comparison page that pulls a list of prices from various merchants via my Xano DB. If I have a price on file, it shows that number (price), if nothing is on file, it returns a 0.

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/1/1dc92636c82b5fa77eb361fe6d214daa19853011.png)

**The Problem**  
On my WeWeb page, I have a text element that I want to either display the price (if there is one) or display “No Stock” if the value is simply 0.

Displaying “No Stock” is easy because I can associate it with an exact value (0) but if an actual price exists, the exact value is unpredictable.

**The Ideal Solution**  
Ideally, it would be nice to simply say “if the value is greater than 0, show this text” but I can’t seem to get that to work. Is it possible to include math equations inside the Switch formula? Here is kind of a pseudo formula showing my ideal solution:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/7/7fae7ba92f507f80611fed385605dedca8707867.png)

**My Current Solution (more work)**  
Right now the plan is to update a variable for each merchant that simply returns a “true/false” bool. Each variable will fire on page load and run a simple math check to see if the price is bigger than 0:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/0/0db8200a1e76303365f4a4868dd711a6d7447714.png)

I then still run the Switch formula, but I instead us the “Amazon” variable that checks for a true/false statement.

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/9/9ba8dc5c721bdefa184845914b09f207ae917dd3.png)

This works, but it’s a little bit more work as I add more merchants and for other use cases that aren’t so black & white (such as a large numerical scale where as the number increases, the font color changes to create a hot/cold effect).

**Does anybody know if it’s possible to accomplish this in a single formula without having to rely on a bunch of variables?**

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 29, 2023, 9:42pm UTC](https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289/2 "2023-06-29T21:42:52Z")

</div>

how about something like `if( price == 0, "no stock", "$"+price)`

---

<div class="post-metadata">

**Author:** ![brandon](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/brandon/32/2804_2.png) [@brandon](https://community.weweb.io/u/brandon)\
**Post date:** [June 29, 2023, 9:58pm UTC](https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289/3 "2023-06-29T21:58:24Z")

</div>

Ahh yes! So much easier! I swear I looked at the “If” formula and had no luck but I was definitely over-complicating it. Thanks for the help @dorilama!

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/6/6aceb495b41c5af5ebed866e9669b23640224273.png)

---

<div class="post-metadata">

**Author:** ![Jean](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jean/32/4106_2.png) [@Jean](https://community.weweb.io/u/Jean)\
**Post date:** [March 12, 2025, 1:33pm UTC](https://community.weweb.io/t/creating-complex-if-then-statements-with-the-switch-formula/3289/4 "2025-03-12T13:33:44Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/bbd387779c65e59a8d5a2d8a0f1c1c43934c66ab.png)

In the scenario above “selectedVariants” is an empty array.

Why is the “true” scenario evaluated in the formula above while the condition evaluates to false?  
I understand there are workarounds but I don’t understand why it needs to calculate value1 if the condition evaluates to render value2

I’ve tried to change value1 to “String1”, and value2 to “String2” and it correctly returns “String2”

What am I missing?
