web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Where do I put TrimEnd...
Power Apps
Suggested Answer

Where do I put TrimEnds for this collection

(1) ShareShare
ReportReport
Posted on by 697
Hey folks,
 
So I have a drop down menu that just displays a collection.  This collection is a list of branches it's pulling from SQL.  The problem is on occasion someone will copy/paste a branch and it'll have a space in the beginning or end, and since I'm tired of telling them I just want to fix it, but I can't figure out where to put the TrimEnds().  Where is what my collection declaration looks like : 
 
 
I've tried it around the whole Collect line, around Branchoffice, around Result...and more but I can't seem to get it right? Any help is appreciated.
I have the same question (0)
  • Suggested answer
    Pstork1 Profile Picture
    69,556 Most Valuable Professional on at
    Does thisrecord.value represent a single text field or something else?  If its text then you should be able to do the trimends() around the result field you are generating from the distinct.

    ----------------------------------------------------------------------------------
    If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!

    Paul Papanek Stork, MVP
    Blog: https://www.dontpapanic.com/blog
     
     
  • Suggested answer
    VASANTH KUMAR BALMADI Profile Picture
    322 on at

    You’re very close — the issue is just where TrimEnds() is allowed to be used.

    TrimEnds() works on text values, not on tables or entire collections.
    So you can’t wrap it around Collect() or around the SQL result itself — it must be applied at the column level while you’re building the collection.

    ✅ Correct way to fix it

    You need to trim the column inside the collection creation, using AddColumns or RenameColumns.

    ✅ Example

    If your SQL result column is called BranchOffice, do this:

    ClearCollect(
        colBranches,
        AddColumns(
            SQLBranches,
            "BranchOffice_Clean",
            TrimEnds(BranchOffice)
        )
    );
    

    Then set your dropdown to use:

    • ValueBranchOffice_Clean

    ✅ If you want to overwrite the original column name

    You can do it like this:

    ClearCollect(
        colBranches,
        RenameColumns(
            AddColumns(
                SQLBranches,
                "BranchOffice",
                TrimEnds(BranchOffice)
            ),
            "BranchOffice",
            "BranchOffice"
        )
    );
    

    But normally the first approach is cleaner.

    ✅ Most common and simplest version

    ClearCollect(
        colBranches,
        AddColumns(
            YourSQLCollection,
            "BranchOffice",
            TrimEnds(BranchOffice)
        )
    );
    

    Power Apps evaluates TrimEnds() row by row, which is exactly what you want.

    ❌ Why your attempts didn’t work

    These don’t work because:

    TrimEnds(Collect(...))   // ❌ TrimEnds expects text, not a table
    
    TrimEnds(Result)         // ❌ Result is a record/table
    
    TrimEnds(SQLBranches)    // ❌ table, not text
    

    Power Fx functions are strongly typed — trimming must be applied to a text column, not to the dataset.

    ✅ Final dropdown setup

    Dropdown.Items = colBranches
    Dropdown.Value = "BranchOffice"
    

    Now whether users enter:

    "New York"
    " New York"
    "New York "
    "   New York   "
    

    they will all display identically.

    ✅ Bonus tip (recommended)

    If duplicates are possible due to spaces, you can also normalize it:

    ClearCollect(
        colBranches,
        Distinct(
            AddColumns(
                SQLBranches,
                "BranchOfficeClean",
                TrimEnds(BranchOffice)
            ),
            BranchOfficeClean
        )
    );
    

    This removes duplicates caused only by leading/trailing spaces.

    ✔ Bottom line

    • TrimEnds() must be applied inside AddColumns

    • It works per record, not on collections

    • This permanently cleans the dropdown display

    Once you do this, you’ll never have to worry about leading or trailing spaces again 👍

  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    Please be mindful of the Responsible AI policies for the Community guidelines. This also applies to this post.
     
    While we all appreciate responses to community posts, the enquirer needs to know the source of the answer. Also other responders may want to provide a more focussed and considered response as an option. 
     
    There is an Assisted by AI option where you can flag this so the material can be considered with appropriate weight.
  • CharlesBrewer Profile Picture
    697 on at
    @Pstork1 Sorry for the late reply, but it didn't email me to let me know someone replied :/
    So I'm not sure how you mean, but I've tried : 
     
    {Result: TrimEnds(ThisRecord.Value)}),Result));
    {Result: ThisRecord.Value}),TrimEnds(Result)));
     
    Any other variation instantly gets the red lines of "no".
     
    And I had no idea what to do with @VASANTH KUMAR BALMADI response since that seems more like a rewrite of some sort :(
  • Suggested answer
    WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    Yes - the post from @VASANTH KUMAR BALMADI seems to be a straight copy and paste ChatGPT response (which is why I noted the forum code that applies). I suggest a couple of things - firstly you do not need the Result complexity - this was only a workaround when the output of Distinct was changed from Result to Value a few years back. You can also do this in one collection and have a field Value as well as trimming your Branchoffice field as below
    ClearCollect(
       collectAll,
       {Value: "All Branches"},
       Sort(
          Distinct(
             AddColumns(
                'View.PowerApps.ServiceLaptops',
                Trimmed,
                TrimEnds(Branchoffice)
             ),
             Trimmed
          ),
          Value
       )
    );
    Be aware that Distinct is a "local" client-side operation, so you will only be able to address record numbers in thta view up to your Data Row Limit.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like
    Visit my blog
    Practical Power Apps    LinkedIn  
  • CharlesBrewer Profile Picture
    697 on at
    @WarrenBelz Thanks for the reply. It's still not working and I feel like I'm missing some info I should be giving you but I don't know what it is.  So to take you through it more, I copy/pasted your code, and I did get no errors, but I got errors elsewhere, specifically on my Gallery Items, which looks like this (obviously you can ignore all the custom column name changes)
     
    Sort(
    Filter(
    Filter(
    Filter(
    RenameColumns('View.PowerApps.ServiceLaptops',
    Custom1,Allison,
    Custom2,AllisonExp,
    Custom3,Bendix,
    Custom4,BendixExp,
    Custom5,CatET,
    Custom6,CatETExp,
    Custom7,Insite,
    Custom8,InsiteExp,
    Custom9,Davie,
    Custom10,ESA,
    Custom11,JPRO,
    Custom12,JPROExp,
    Custom13,WabcoExp,
    Custom14,Wabco,
    Custom16,WabcoOrder,
    Custom17,WabcoSN,
    Custom18,SR4Order,
    Custom19,SR4,
    Custom20,SR4Exp),
    Branchoffice=Office365Users.MyProfile().OfficeLocation || IsO365GroupMember=true),
    Branchoffice=Dropdown1.Selected.Result || Dropdown1.Selected.Result="All Branches"),
    Switch(varSoftwareSelected,
        "Allison",
        !IsBlank(Allison),
        "Bendix",
        !IsBlank(Bendix),
        "CatET",
        !IsBlank(CatET),
        "Davie",
        !IsBlank(Davie),
        "ESA",
        !IsBlank(ESA),
        "Insite",
        !IsBlank(Insite),
        "JPRO",
        !IsBlank(JPRO),
        "Wabco",
        !IsBlank(Wabco),
        "SR4",
        !IsBlank(SR4),
        //Default
        IsBlank(varSoftwareSelected)
    )), 
    AssetName,SortOrder.Ascending)
    The problem of course was the "Branchoffice=Dropdown1.Selected.Result || Dropdown1.Selected.Result="All Branches"),"
    Following what you said, I changed the .Result on both to .Value, and refreshed everything.  The data shows up and everything, but what is happening is I believe I need to TrimEnds() somewhere else. This is doing it for the CollectAll, which controls the dropdown, but doesn't actually control the Thisitem.branchoffice in the gallery, so I need to figure out how to Trim that. Otherwise those assets just won't show up if they have extra spaces.  I added TrimEnds() to Branchoffice=Dropdown1.Selected.Result in the gallery code above and that didn't fix it. I also added it to the Branch display text on the gallery items and that didn't help either.  I'm kind of at a loss, I should probably just get the SQL View edited to trim there instead.
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    I have an alternate structure that you may want to consider - you can mix this with what you have, but a couple of things first. You do not need the collection for the Drop-down Items - you can put this straight in there
    Table(
       {Value: "All Branches"},
       Sort(
          Distinct(
             'View.PowerApps.ServiceLaptops',
             Branchoffice
          ),
          Value
       )
    );
    Now the other thing which is good practice is getting any calculations/formulas out of the Filter - also you do not need the nested filters, so try this
    With(
       {
          _OL: Office365Users.MyProfile().OfficeLocation,
          _Branch: TrimEnds(Dropdown1.Selected.Value)
       },
       Sort(
          Filter(
             RenameColumns(
                'View.PowerApps.ServiceLaptops',
                Custom1,Allison,
                Custom2,AllisonExp,
                Custom3,Bendix,
                Custom4,BendixExp,
                Custom5,CatET,
                Custom6,CatETExp,
                Custom7,Insite,
                Custom8,InsiteExp,
                Custom9,Davie,
                Custom10,ESA,
                Custom11,JPRO,
                Custom12,JPROExp,
                Custom13,WabcoExp,
                Custom14,Wabco,
                Custom16,WabcoOrder,
                Custom17,WabcoSN,
                Custom18,SR4Order,
                Custom19,SR4,
                Custom20,SR4Exp
             ),
             (Branchoffice = _OL || IsO365GroupMember) &&
             (Branchoffice = _Branch || _Branch = "All Branches") &&
             (
                (varSoftwareSelected = "Allison" && !IsBlank(Allison)) ||
                (varSoftwareSelected = "Bendix" && !IsBlank(Bendix)) ||
                (varSoftwareSelected = "CatET" && !IsBlank(CatET)) ||
                (varSoftwareSelected = "Davie" && !IsBlank(Davie)) ||
                (varSoftwareSelected = "ESA" && !IsBlank(ESA)) ||
                (varSoftwareSelected = "Insite" && !IsBlank(Insite)) ||
                (varSoftwareSelected = "JPRO" && !IsBlank(JPRO)) ||
                (varSoftwareSelected = "Wabco" && !IsBlank(Wabco)) ||
                (varSoftwareSelected = "SR4" && IsBlank(SR4))
             ) &&
             IsBlank(varSoftwareSelected)
          )
       ), 
       AssetName,
       SortOrder.Ascending
    )
    The important part for your problem is where I have put the TrimEnds statement.
     
    I am not exactly sure of the outcome you require in the branch office (the and/or result), however (I am guessing here), if you want the resrict users where IsO365GroupMember is false, then put this on the Items of the drop-down.
    If(
       IsO365GroupMember,
       Table(
          {Value: "All Branches"},
          Sort(
             Distinct(
                'View.PowerApps.ServiceLaptops', 
                Branchoffice
             ),
             Value
          )
       ),
       [Office365Users.MyProfile().OfficeLocation]
    )
    and then you can delete these two lines from the Gallery Filter
    _OL: Office365Users.MyProfile().OfficeLocation
    
    (Branchoffice = _OL || IsO365GroupMember)
    This way, a false result will only get one office to choose from in the drop-down.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like
    Visit my blog
    Practical Power Apps    LinkedIn  
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    A quick follow-up to see if you received the answer you were looking for. Happy to assist further if not.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn   

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard