Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
TheInvoker
Frequent Visitor

How to convert all records in a nested list of records, into tables?

I have an expression which produces a list of records. Each record can have properties whose values are another list of records or a record. I want to convert it into a Table, where each list of records gets transformed into a table, and each record gets transformed into a table too. All recursively.

 

For example, if the data structure looks like this:

 

 

 

 

 

 

{
    "include": [
        {
            "relation": "ActivityCodeSet",
            "scope": {
                "limit": 2
            }
        }
    ]
}

 

 

 

 

 

Then this query:

 

 

 

 

 

= Table.FromRecords(
    List.Transform(
        Json.Document(Web.Contents("localhost:64977/ActivityCode?filter={""include"":[{""relation"":""ActivityCodeSet"",""scope"":{""limit"":2}}]}")), 
        each Record.TransformFields(_, 
            {
                {"ActivityCodeSet", (x) => Table.FromRecords({x})}
            }
        )
    )
)

 

 

 

 

 


Does the job I want, but the problem is, it is very hardcoded to that json structure. How can I do this more generically where I don't hardcode any field names? It should just work recursively with any json structure.


I got this so far, but it doesn't seem to work recursively. The nested ones remain a list of records.

 

 

 

= let
    Source = ...,
    fxFactorial = (x as list) => 
         let
            Check = Table.FromRecords(List.Transform(x, each Record.TransformFields(_, 
                let
                    Row = _,
                    Fields = Record.FieldNames(Row),
                    RFields = List.Select(Fields, each Value.Is(Record.Field(Row, _), type record)),
                    LFields = List.Select(Fields, each Value.Is(Record.Field(Row, _), type list)),
                    RFieldsTr = List.Transform(RFields, each {_, (x) => Table.FromRecords({x})}),
                    LFieldsTr = List.Transform(LFields, each {_, @fxFactorial}), 
                    Full = List.Combine({RFieldsTr, LFieldsTr})
                in
                    Full
            )))
          in
            Check,
    Custom1 = fxFactorial(Source)
in
    Custom1

 

 

1 REPLY 1
lbendlin
Super User
Super User

Keep in mind that at the end of your Power Query script needs to be a single table with  (ideally) static meta data (column names, column order, column types).  So while you may be able to recursively collect all these tables, it is for nought as you then need to squeeze them into a single result.

 

XML and JSON are by definition hierarchical.  RDBMS type solutions are not.  Converting one into the other includes making compromises and concessions. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.