cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
seadude
Memorable Member
Memorable Member

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

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
timl
Super User
Super User

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")
)

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

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.

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")
)

 

v-xida-msft
Community Support
Community Support

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.

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.

 

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

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

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!   This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08  23 @WarrenBelz  31 @DBO_DV  10 @Amik  19 AmínAA 6 @mmbr1606  12 @rzuber  4 @happyume  7 @Giraldoj  3@ANB 6 (tie)   @SpongYe  6 (tie)     Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08  10@WarrenBelz 25 @DBO_DV  6@mmbr1606 14 @AmínAA 4 @Amik  12 @royg  3 @ANB  10 @AllanDeCastro  2 @SunilPashikanti  5 @Michaelfp  2 @FLMike  5 @eduardo_izzo  2   Meekou 2   @rzuber  2   @Velegandla  2     @PowerPlatform-P  2   @Micaiah  2     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27     Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8   SunilPashikanti8

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages