Forum Discussion

Montechristos01's avatar
1 year ago

Dynamic table based on JSON column

Hi,

 

We have a data source that is a table of fixed columns, as well as an additional column with JSON of the form:
{"Column 1": "value", "Column 2": "value", ... }

We have hundreds of records, and potentially hundreds of columns, which we do not know in advance.

 

We want to display this data in a 'normal' PBI table, (including sorting, filtering, highlighting, ...) dynamically, and after a set of rows have been filtered.

 

Is this possible in PBI?

8 Replies

  • Yes.

     

    Let's say you are importing something like the below table:

     

    SourceTable:

    Fixed Col AFixed Col BFixed Col CVariable Json Col
    11121{"Column1":"A","Column2":"B1"}
    21222{"Column2":"B2","Column3":"C"}
    31323{"ColumnA":1,"ColumnB":2}
    41424{"ColumnC":3,"ColumnD":4}
    51525{"Extra 1":"abc","Extra 2":"def"}

     

    The following code will parse the JSON column, acertain all the JSON fields across all rows, then expand all of them:

     

    let
        Source = SourceTable,
    
        ParseJSON = Table.TransformColumns(Source,{{"Variable Json Col", Json.Document}}),
    
        AllJsonFields = List.Distinct( List.Combine( 
            List.Transform( ParseJSON[Variable Json Col], Record.FieldNames ) 
        ) ),
    
        ExpandAllJsonFields = Table.ExpandRecordColumn(ParseJSON, "Variable Json Col", AllJsonFields )
    in
        ExpandAllJsonFields

     

    Output:

     

    Note that all the dynamically expanded JSON fields are of type any, so any type-setting will have to be addressed separately. Although, it would be possible to try to do something like automatically setting the type based on the values.

    • Montechristos01's avatar
      Montechristos01
      Helper I

      Thanks for the answer.

      We had thought of that, but there are tow issues:

      - There may be hundreds of columns. Can PBI deal with these?

      - For every entity, you will see all the columns (not even ordered) of all other entities.

       

      Any further thoughts?

      • MarkLaf's avatar
        MarkLaf
        Super User

        Q: There may be hundreds of columns. Can PBI deal with these?

        A: Yes, although it's almost certainly not useful for analysis. If you provide more context on what kind of reports you want to build, and more specificity on the shape of your data, it would be easier to recommend something. Pasting test data into a table for easy copying into PBI is the best

         

        Q: For every entity, you will see all the columns (not even ordered) of all other entities.

        A: This is unavoidable if you want to expand them all in the same table. Are some rows of the same structure as others and it would make sense to split them into separate tables? If it's highly variable without patterns in the data to leverage, I would unpivot all the JSON parsed columns into field/value columns. You can just use unpivot through the UI after expanding. Or change up the query steps to convert it before expanding - something like:

         

         

        let
            Source = SourceTable,
        
            // parse json and then convert to field/value table
            ParseJSON = 
            Table.TransformColumns(
                Source,
                {
                    "Variable Json Col", 
                    each Record.ToTable( Json.Document(_) ), 
                    type table [Name=text,Value=any] 
                }
            ),
        
            ExpandJsonCol = 
            Table.ExpandTableColumn(
                ParseJSON, 
                "Variable Json Col", 
                {"Name", "Value"}, 
                {"Name", "Value"}
            )
        
        in
            ExpandJsonCol

         

         

        Output: