cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
seadude
Level 10

Switch If Value is Numeric

Hello,

 

I'm trying to search an SQL data source based on the value of a Search Text Box. The SQL table has both int and varChar columns. Somehow I want to send numeric search values to the int columns and text search values to the varChar columns.

 

Something to the effect of:

 

Switch(textbox.text, 
    IsNumeric(textbox.text), 
        Search(dbo.azure, textbox.text = azure_number_column),
    !IsNumeric(textbox.text),
        Search(dbo.azure, textbox.text = azure_text_column))

Anyone have some pointers for this one?


Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Switch If Value is Numeric

Hi @seadude

 

I think you might just be missing the .Text property of TextInput1 in your call to the IsNumeric function.

 

If(
    IsNumeric(TextInput1.Text),
    Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
    Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state")
)

 

View solution in original post

8 REPLIES 8
Super User
Super User

Re: Switch If Value is Numeric

Hi @seadude

You should be able to do this with a simple If statement. The Switch function evaluates a single condition against multiple possible matches. In your case, you only need to evaluate two possible conditions ("is numeric", or "not numeric"). Therefore, an If statement will be sufficient.

Also, the Search function is designed to work against text columns. It carries out a 'contains' type search so if you use this against a numeric column, you can run into delegation issues. Therefore, it might be better to carry out an exact search with the Filter function.

The following formula should take you a bit closer:

If(IsNumeric(textbox.text),
   Filter(dbo.azure, Value(textbox.text) = azure_number_column),
   Search(dbo.azure, textbox.text, "azure_text_column")
)
seadude
Level 10

Re: Switch If Value is Numeric

Hi @timl. Thanks for the reply. I can't seem to get the formula to work.

 

If(
    IsNumeric(TextInput1),
   Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
   Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state")
)

TextInput1 is in the top right corner of the app. You can see that when I type in "22", no results are returned.

Screenshot from 2018-06-09 18-57-22.png

 

However, when I search for "wa" it returns all instances

Screenshot from 2018-06-09 18-57-35.png

 

If I ONLY use the

Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum)

function, it returns results for "22" just fine. Any other ideas?

Screenshot from 2018-06-09 19-16-57.png

KroonOfficeSol
Level 10

Re: Switch If Value is Numeric

He seadude, sure there are no spaces in there, because the formula should work. Did you try to put the first part in a label to see if this give the right boolean value?

 

What you could try is to surround the textinput.text with the value() formula and then surround this with a IsBlank() formula:

IsBlank(Value(TextInput.Text))

The value formula can't be avaluated when the textbox contains everythink different then numbers and returns a blank, so the IfBlank formula is then true. If so you do the search, else the filter part.

Super User
Super User

Re: Switch If Value is Numeric

Hi @seadude

 

I think you might just be missing the .Text property of TextInput1 in your call to the IsNumeric function.

 

If(
    IsNumeric(TextInput1.Text),
    Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
    Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state")
)

 

View solution in original post

Community Support Team
Community Support Team

Re: Switch If Value is Numeric

Hi @seadude,

 

I think there is something wrong with the formula that you provided. I agree with @timl's thought almost, I have made a test on my side and the formula works well on my side. Please take a try with the following formula:

 

Set the Items property of the Gallery control to the following formula:

If(
IsNumeric(TextInput1.Text),
Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state")
)

In addition, you could also take a try to set the Items property of the Gallery control to the following formula (using Switch function😞

Switch(
IsNumeric(TextInput1.Text),
true,Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
false,Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state")
)

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
seadude
Level 10

Re: Switch If Value is Numeric

I was indeed missing the .Text parameter though the intellisense didn't pick that up! It showed the forumla syntax as correct.

 

Can we take this a step further?

 

Ideally I want to use the single search box in the top right ("Search Anything") to filter and/or search all columns in the table. Since the columns are a mix of strings and numbers, we went down the path of IsNumeric, True/False. Unfortunately, this only seems to work 1 level deep.

 

How can I use Switch or If to do something like this:

Switch(
    IsNumeric(TextInput1.Text),
        true,
            Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = sequenceNum),
            Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = annual_inc),
            Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = int_rate),
            Filter('[dbo].[LoanStats3a]', Value(TextInput1.Text) = installment),
        false,
            Search('[dbo].[LoanStats3a]', TextInput1.Text, "addr_state"),
            Search('[dbo].[LoanStats3a]', TextInput1.Text, "zip_code"),
            Search('[dbo].[LoanStats3a]', TextInput1.Text, "term"))

Basically, what ever is typed in the search box will return results from ANY column.

 

Example: Typing "36" in the search bar would return:

  • All records with "36 Months" in the Term column
  • Record number "36" from the ID column
  • Any records with "36" in the Income column (such as $36,000)
  • AND any records with "36" as part of the Zip Code

Too bold? Or is it possible? The above code doesn't work; Switch doesn't like Filter or Searches beyond 1 level when using the True/False boolean.

 

KroonOfficeSol
Level 10

Re: Switch If Value is Numeric

This is difficult to achieve, but lets try.

 

Solution 1: (I think this might work, did not simulated is)

Make a new view on your data where you cast all the numeric field you want to search on to varchar. Then use this view for your datasource and you can probably use a simple Search function.

 

On selection off a record you then store the ID to a local variable, like 

UpdateContext({IDValue: Value(Gallery.Selected.ID)}) 

and go on from there. (Also you can pass this in the contextpart off the navigation formula if you want to go to other screen)

 

Solution 2:

Add two variables, one for a number and one for a textvalue, which you set on the onchange event off your Textinput. This formula will do the trick I think: 

UpdateContext({SearchValue: Value(TextInput1.Text)});
UpdateContext({SearchText: If(IsBlank(SearchValue),TextInput1.Text,Blank())})

Basically what happens is that first the TextInput1.Text is stored as a value in the SearchValue then the SearchText will be blank(). As sone there is a string character in the textinput control the SearchValue will be blank() and the SearchText will be set to the TextInput1.Text. 

 

Then use the filter function to get the right records back. This formula will do this:

Filter('[dbo].[vwSalesOrderHeader_SelectAll]',
    IsBlank(SearchValue) || CustomerID = SearchValue,
    IsBlank(SearchText) || SearchText in Customer
)

What this will do is evaluate the first line when the input is a number and the second line when input is a text. If it a number then the searchtext will be blank, which sets the second line to true on the IsBlank() part. Because the SearchValue is not blank (so the IsBlank() returns a false) it will evaluate the other condition for a match. 

 

OffCourse add more conditions (|| NextField = Searchxxxx) when needed and be aware that a match on a SearchValue is a full match, so when you enter a number the gallery will be empty when there are no matches. If you want to filter on a part of a ordernummer then try solutions 1. 

 

In this solution we don't use the Search() formula. But we can do that also if we want in this way:

Search(Filter('[dbo].[vwSalesOrderHeader_SelectAll]',
   IsBlank(SearchValue) || CustomerID = SearchValue
    ),
SearchText,
"Customer"
)

 

btw: We can't replace the SearchText variable by the TextInput1.Text in above example, because we need it to be blank in case off a number.

 

As far I know and can see both the Filter() and Search() formula used like in the examples are delegated to the server, so you can get a maximum off 2.000 records back for the gallery if needed.

 

Hope this is a good solutions to concur your challenge.

 

Greatings, Paul Kroon

KroonOfficeSol
Level 10

Re: Switch If Value is Numeric

Btw, your formula should be like this:

 

If(IsNumeric(TextInput1.Text),
    Filter('[dbo].[LoanStats3a]', 
		Value(TextInput1.Text) = sequenceNum || Value(TextInput1.Text) = annual_inc) || Value(TextInput1.Text) = int_rate || Value(TextInput1.Text) = installment
		),
    Search('[dbo].[LoanStats3a]', 
		TextInput1.Text, 
		"addr_state",
		"zip_code",
		"term"
	)
)

Like this you get only one recordset back for your items. In your example you returned with the Filter() 4 recordsets and with the Search() 3 recordsets where the items only excepts one Smiley Happy

 

For course you can use the variables from my other example also in this solution:

If(!IsBlank(SearchValue),
    Filter('[dbo].[LoanStats3a]', 
		SearchValue = sequenceNum || SearchValue = annual_inc) || SearchValue = int_rate || SearchValue = installment
		),
    Search('[dbo].[LoanStats3a]', 
		SearchText, 
		"addr_state",
		"zip_code",
		"term"
	)
)

This keeps PowerApps to have to calculate a number from you textinput multiple times. I don't know how that backend off PowerApps works but I can imagine this will give a slightly better performance.

 

Also: the Switch function has no advantage in this solutions, because you basically want to evaluate a boolean (so true or false). The Switch function is primary handy when you want to evaluate a value/variable/combobox which can have more output values which are not a boolean).  

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 91 members 4,869 guests
Please welcome our newest community members: