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 / Add a conditional Grou...
Power Apps
Answered

Add a conditional GroupBy to ClearCollect.

(1) ShareShare
ReportReport
Posted on by 578

I'm trying to add a condition to GroupBy, but I get an error saying: The function GroupBy has some invalid arguments. Expected literal text. We expect literal text at this point in the formula. I really don't know what I'm missing. I'm trying to use something like this:

...
 GroupBy(
 Data,
 If(
 condition,
 "Column1",
 "Column2"
 )
 )
...

 

The code I'm using is this:

ClearCollect(
 reportsDataTypeByYear,
 AddColumns(
 SortByColumns(
 GroupBy(
 AddColumns(
 rawData,
 "ROValue",
 'Office'.Value
 ),
 If(reportTypeVar = "Count By Request Type And FY","fy", "Title"),
 "ROValue",
 "ROGroup"
 ),
 "ROValue",
 Ascending
 ),
 "TypeCount",
 CountRows(ROGroup)
 )
)

 

Thanks for any suggestion.

Categories:
I have the same question (0)
  • Verified answer
    LaurensM Profile Picture
    12,516 Moderator on at

    Hi @emfuentes27,

     

    I don't think this is currently possible within the input parameters of GroupBy. It expects a Text Literal - meaning a hardcoded string value that is not variable or conditional. Text variables, for example, lead to the exact same error.

    Another way we could get this to work is by going to the parent function and trying to apply the If() there. Instead of using it within the GroupBy, we use it around the function:

    ClearCollect(
     reportsDataTypeByYear,
     AddColumns(
     SortByColumns(
     If(reportTypeVar = "Count By Request Type And FY",
     GroupBy(
     AddColumns(
     rawData,
     "ROValue",
     'Office'.Value
     ),
     "fy",
     "ROValue",
     "ROGroup"
     ),
     GroupBy(
     AddColumns(
     rawData,
     "ROValue",
     'Office'.Value
     ),
     "Title",
     "ROValue",
     "ROGroup"
     )
     ),
     "ROValue",
     Ascending
     ),
     "TypeCount",
     CountRows(ROGroup)
     )
    )

    This unfortunately results in some code duplication. Now by using a With() function and storing the AddColumns(rawData, ...) locally, you could remove the duplication partially.

     

    If this solves your question, would you be so kind as to accept it as a solution.

    Thanks!

  • emfuentes27 Profile Picture
    578 on at

    Thanks @LaurensM . Your solution is precisely what I was trying to avoid, but I guess there is no other way for now.

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 Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 326 Most Valuable Professional

#2
11manish Profile Picture

11manish 168

#3
sannavajjala87 Profile Picture

sannavajjala87 75 Super User 2026 Season 1

Last 30 days Overall leaderboard