Skip to main content

Notifications

Community site session details

Community site session details

Session Id : Fz0l0/fH2jHuZODGZYMHrD
Power Apps - Building Power Apps
Answered

Dynamic Table Access in PowerApps - MatchAll

Like (1) ShareShare
ReportReport
Posted on 5 Aug 2023 18:05:16 by Microsoft Employee

I've been trying to figure out a way to use dynamic tables in PowerApps.  I get data just fine - the problem is that PowerApps doesn't have a string accessor (e.g. table["NameofColumn"]).  Everything is strongly named at design time.  Eventually, I'm also trying to do this in a component.   The end goal is to dynamically create forms by "zipping" the data into a metadata table and displaying the form in a gallery.... whew!

 

So, the only thing I can think of is dynamic FetchXml to Json response and now I'm trying to parse and access the response.  ParseJson returns a strongly named object.  So, I'm down to trying to create a collection on the fly with the ColumnName and ColumnValue as rows like so:

 

ColumnName | ColumnValue

CN1 | CV1

CN2 | CV2

CN3 | CV3

 

and so on.  I've got a collection of names with the following:

 

Set(
    colColumnNames,
    Distinct(
        Ungroup(
            MatchAll(
                jsonRequestSummary,
                "([^""]+?)""\s*:"
            ).SubMatches,
            "SubMatches"
        ),
        Value
    )
);
 
Now, I need to get the values for each name with the following regex: 
"(?<=""title"":)""([^""]+?)"""
 
which works for value of "title".
 
The trick is to do the MatchAll within the Collect statement to lookup the value, but I'm having trouble getting a single value becuase it returns rows and that's where I'm currently stuck.  
 
By the way, I would *love* for someone to tell me that I'm going about this all the wrong way.  I realize I'm pushing the boundaries here, but that's the need. 
 
Thanks so much!
  • lionelgo Profile Picture
    Microsoft Employee on 06 Aug 2023 at 03:13:12
    Re: Dynamic Table Access in PowerApps - MatchAll

    Here's the final code for the couple of folks that "liked" the first connundrum:

     

    ClearCollect(
     colColumnIndex,
     Ungroup(
     MatchAll(
     jsonRequestSummary,
     """([^""]+?)""\s*:\s*""([^""]+?)"""
     ).FullMatch,
     "FullMatch"
     )
    );
    ClearCollect(
     gloJsonCollection,
     ForAll(
     colColumnIndex,
     {
     ColumnName: Trim(
     Substitute(
     First(
     Split(
     FullMatch,
     """:"""
     )
     ).Value,
     Char(34),
     ""
     )
     ),
     ColumnValue: Trim(
     Substitute(
     Last(
     Split(
     FullMatch,
     """:"""
     )
     ).Value,
     Char(34),
     ""
     )
     )
     }
     )
    );
  • lionelgo Profile Picture
    Microsoft Employee on 06 Aug 2023 at 03:10:45
    Re: Dynamic Table Access in PowerApps - MatchAll

    Sweet!  And a lot simpler than where I was going!  I did have some issues with extra ":" characters, so I've taken your cue and just used the RegEx to pre-filter the data into a string I could split on.  Works like a charm.  On to the next step, thank you!

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on 05 Aug 2023 at 18:23:12
    Re: Dynamic Table Access in PowerApps - MatchAll

    @lionelgo 

    if you are currently getting the right "rows" or "records" already, then simply extracting one of the columns out of it, if that's the only place you're stuck, should be straightforward.

    Say you were in a hurry and want to create a comma-separated string from a column in a collection (and say you don't have time to optimize it much by incorporating this into the data processing you already have),

     

    then you can use the Concat function in Power Apps.

     

    The Concat function iterates over a collection and concatenates the string representation of one or more records.

    Here's how you might generate a comma-separated string from the 'Title' values:

     

    PowerApps
    Set(
     gloTitleString,
     Concat(
     gloJsonCollection,
     title & ","
     )
    )

     

    In this formula, Concat is iterating over gloJsonCollection and for each record, it's appending the 'Title' value and a comma to the gloTitleString. At the end of this operation, gloTitleString should hold a comma-separated string of all 'Title' values from the collection.

     

    You may have a trailing comma at the end of the string.

     

    If you want to avoid the trailing comma, you can use the Left function to remove it:

     

    Set(
     gloTitleString,
     Left(
     Concat(
     gloJsonCollection,
     title & ","
     ),
     Len(Concat(gloJsonCollection, title & ",")) - 1
     )
    )
    

     


    Here, Len() is used to find the length of the string and Left() is used to take all characters except the last one, effectively removing the trailing comma.

     

    See if this helps as well @lionelgo 

  • Verified answer
    poweractivate Profile Picture
    11,078 Most Valuable Professional on 05 Aug 2023 at 18:15:51
    Re: Dynamic Table Access in PowerApps - MatchAll

    @lionelgo 

    You are indeed trying to achieve something quite complex. PowerApps generally doesn't support dynamic table and column names like you might see in SQL. PowerApps has a declarative language, Power Fx, which is not designed with dynamic data structures as a first class concept.

     

    In the scenario you've provided, what you're trying to achieve with regex may not be the best approach, as PowerApps doesn't natively support regex operations. Even though your example does use a MatchAll function, which can mimic some of the regex functionality, it's still limited in its scope.

     

    However, it seems that what you're essentially trying to achieve is to turn a JSON string into a table that PowerApps can interact with. PowerApps has a JSON function that can convert JSON to and from collections, which might be more suitable for your purposes.

     

    This formula example should create a collection where each record contains two properties: ColumnName and ColumnValue, from a JSON string:

    ClearCollect(
     gloJsonCollection, 
     ForAll(
     Split(
     Mid(jsonRequestSummary, Find("{", jsonRequestSummary) + 1, Find("}", jsonRequestSummary) - 2),
     ","
     ),
     {
     ColumnName: Trim(First(Split(Result, ":")).Result),
     ColumnValue: Trim(Last(Split(Result, ":")).Result)
     }
     )
    )
    

     

    This formula example should handle JSON strings formatted like {"Name": "John", "Age": "30"}, where each key-value pair is separated by a comma.

    Note that this is a simplified version and might not handle more complex JSON strings.

     

    For handling dynamic form creation, I suggest looking at PowerApps' built-in functions, such as:

    - AddColumns() to dynamically add columns to your data source,
    - UpdateContext() to update screen scope variables based on user input,
    - and Patch() to update your data source with these changes.

    Remember, you cannot use Set() here because it could cause issues later when you want to patch the data, as per Patch requiring the first argument to be a Collection, which means it also cannot be a global variable by Set even if it's a Table.

    You could also name your global variables with the prefix 'glo', as in 'gloJsonCollection' to distinguish them from say context variables.

     

    See if it helps @lionelgo 

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

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,660 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,999 Most Valuable Professional

Leaderboard
Loading started
Loading complete