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

Big thanks to the video that @Joyce dropped on YouTube 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

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:

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:

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

image

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?

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

3 Likes

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

1 Like