Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

Making a switch command into a named formula

(1) ShareShare
ReportReport
Posted on by
Hi,
im new to powerapps and coding in general, i was wondering if i could take the switch function shown below and make it global, so i dont have to enter the whole thing everytime and if i change it once all of the items using it change at the same time. The ThisItem identifier gave me a tough time, 
Categories:
  • Suggested answer
    MM-24071711-0 Profile Picture
    on at
    Making a switch command into a named formula
    I tried this, but it says I'm not allowed to put context in a component
    yeh, I did some reading and I'm not sure if i can do that, i also tried using an output from the gallery selection and a
    record input as variable, but also that didn't work, and it doesn't navigate
     
    when i removed the context in the component it navigates but it doesn't select the clicked value, so i created an output value from the Gallery.selection and an input from that output which i linked to the form and it worked!
     
    what a day, thanks for your help!
  • level36 Profile Picture
    71 on at
    Making a switch command into a named formula
    No problem! Happy to help, for since its inside a container you may need to reference it using GComp.CurrentItem. Hope that fixes it!
  • MM-24071711-0 Profile Picture
    on at
    Making a switch command into a named formula
    great, only thing not working is current item:
    it goes to the screen but the item selected in the gallery isn't selected in the form like before
    i feel this is a hassle, initially everything was working fine :(
     
    i really appreciate your help <3
  • level36 Profile Picture
    71 on at
    Making a switch command into a named formula
    NavScreen should be a property with type "Screen", then when you use your component set that property to Edit_infoScreen.
  • MM-24071711-0 Profile Picture
    on at
    Making a switch command into a named formula
    can you elaborate more, I'm very new to this
    so, I created the custom property, but what do i write for 'Navscreen' record?
  • level36 Profile Picture
    71 on at
    Making a switch command into a named formula
    Hi,
     
    Hope this helps,
     
    To get the OnSelect() function to work, create a new input property on your component with data type Record. In your component use this record in your Navigate statement, then set this property to ThisItem where your component is used. FYI you can also pass a screen in the same way, this may make transitioning your screen easier.
     
    In your component gallery OnSelect: Navigate(NavScreen,ScreenTransition.None,{view:CurrentItem})
     
     
    If you are always navigating to the page with your edit form in this way, you can simply set the OnVisible property of your Edit_infoScreen to: EditForm(Form1).
     
    If not, you may want to use: If(!IsBlank(view), EditForm(Form1));
  • MM-24071711-0 Profile Picture
    on at
    Making a switch command into a named formula
    thank you! your advice on making a custom component was beautiful, although the first solution was easier
     
    so, with great pain I have managed to make most of the original gallery into a custom component and it took alot of figuring out, however I'm stuck at the Onselect function. Initially it was 
     
    EditForm(Form1);Navigate(Edit_infoScreen,ScreenTransition.None,{view:ThisItem})
     
    But i have no idea how to incorporate this in the custom component,
    the idea is that the selected item
    from the gallery takes me to a form for editing that changes the value in the table.
     
    copying it to the onselect in the component doesn't work and i don't know how
    to reference it to the form in the mainscreen
  • Verified answer
    level36 Profile Picture
    71 on at
    Making a switch command into a named formula
    Hi,
     
    A non-experimental way of solving this is to use a Custom Component. Custom Components can be created from the App Tree View under the Components Tab. Microsoft details for component creation are here: Canvas component overview - Power Apps | Microsoft Learn (This guide explains it much better than I can).
     
    By using a component, you could tackle this issue in two ways,
     
    The first would be to template all your visuals in the component, and intake the values that need to be displayed. 
     
    The second would more closely emulate the behavior of the user defined functions system.  You can create a hidden component that takes the name as a parameter, then returns the color. To do this I created a new component with a height and width of 0. I then created an input property "Name" and an output property of "Color"
     
     
    Advanced menu I added the switch statement to the "Color (Output)" Data field.
     
     
    To use this component, add it to your container from the Custom Menu in the add components dropdown.
     
     
    Then set the Name property to be ThisItem.consultant, then set the Fill value of whatever you need to color to ColorSwitcher_1.Color (or whatever the name is in the tree view) and it should return the correct color. I would also set the visible property to false, even though the component already has a size of 0.
     
  • MM-24071711-0 Profile Picture
    on at
    Making a switch command into a named formula
    Thanks works, perfectly!
    Any other way other than the experimental feature?, or you would just recommend to copy and paste it manually everytime i want to use the switch function?
  • level36 Profile Picture
    71 on at
    Making a switch command into a named formula
    Hello,
     
    Traditionally PowerApps has not supported User-Defined Functions, However, there is an Experimental Feature that allows for this behavior.
     
    As this feature is experimental, it may break as PowerApps releases updates. Consider the impact this could have on your application and evaluate its importance before using experimental features.
     
    To enable this, open your app settings and go to the updates tab. Once there go to Experimental and look for "User-defined functions"
     
    Settings -> Updates -> Experimental -> User-defined functions
     
     
    Once this setting is enabled, you can create functions in the App -> Advanced -> Formulas section.
     
    To Achieve the behavior you are looking for, you could use a function like what I've listed below.
     
    //ColorSwitcher
    ColorSwitcher(input:Text):Color = Switch(input,
        "Name 1", ColorValue("#274001"),
        "Name 2", ColorValue("#274001"),
        "Name 3", ColorValue("#274001")
    );
     
    Then where you want to use this function simply use:
     
    ColorSwitcher(ThisItem.consultant);
     
    Any update you make to the function definition will cascade to all of its uses.
     
    If you need a solution that does not use an experimental feature, please look into creating a standard component that you can use in the locations where you need it. Documentation for components can be found here: Canvas component overview - Power Apps | Microsoft Learn
     
    Please reply if you need any more info or mark this post as an answer if it helped you!
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard