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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Expand all the columns in JSON dynamically using Power Query (M language)

I am having a JSON as follows:

 

{
"Team": [
    {
        "TeamId": "1",
        "TeamName": "Chicago Bulls",
        "TeamPlayers": [
            {"PlayerId": "1", "PlayerName": "Pau Gasol"},
            {"PlayerId": "2", "PlayerName": "Derrick Rose"},
            {"PlayerId": "3", "PlayerName": "Joakim Noah"},
            {"PlayerId": "4", "PlayerName": "Jimmy Butler"},
            {"PlayerId": "5", "PlayerName": "Taj Gibson"}
        ]
    },
    {
        "TeamId": "2",
        "TeamName": "Cleveland Cavaliers",
        "TeamPlayers": [
            {"PlayerId": "1", "PlayerName": "Lebron James"},
            {"PlayerId": "2", "PlayerName": "Kyrie Irving"},
            {"PlayerId": "3", "PlayerName": "Anderson Varejao"},
            {"PlayerId": "4", "PlayerName": "Dion Waiters"},
            {"PlayerId": "5", "PlayerName": "Shawn Marion"}
        ]
    },
    {
        "TeamId": "3",
        "TeamName": "Los Angeles Clippers",
        "TeamPlayers": [
            {"PlayerId": "1", "PlayerName": "Chris Paul"},
            {"PlayerId": "2", "PlayerName": "Blake Griffin"},
            {"PlayerId": "3", "PlayerName": "DeAndre Jordan"},
            {"PlayerId": "4", "PlayerName": "Jamal Crawford"},
            {"PlayerId": "5", "PlayerName": "Matt Barnes"}
        ]
    }
]
}

The number of columns in this JSON file can be changed along with the content it holds.

 

For example: There may be an addition to new columns containing lists or records in them, or there may be a decrease in number of the columns in many JSON files required further.

 

I want to write a code in M language in such a way that, I could be able to expand all the columns in the JSON file at once, including the lists and records, irrespective of the number/name/content of the column.

This has to happen dynamically where column names are unknown (i.e., the next file may have 10 number of columns with lists and records in any/all of the columns).

 

So the above JSON file should be something as follows, but dynamically: 

 

Reference.JPG

 

Any help is greatly appreciated.

Thanks in advance!

3 REPLIES 3
v-yuezhe-msft
Employee
Employee

@Anonymous,

Create a function in Power BI Desktop as described in this blog.

Then invoke this function as below.

let
    Source = FunctionName("{
""Team"": [
    {
        ""TeamId"": ""1"",
        ""TeamName"": ""Chicago Bulls"",
        ""TeamPlayers"": [
            {""PlayerId"": ""1"", ""PlayerName"": ""Pau Gasol""},
            {""PlayerId"": ""2"", ""PlayerName"": ""Derrick Rose""},
            {""PlayerId"": ""3"", ""PlayerName"": ""Joakim Noah""},
            {""PlayerId"": ""4"", ""PlayerName"": ""Jimmy Butler""},
            {""PlayerId"": ""5"", ""PlayerName"": ""Taj Gibson""}
        ]
    },
    {
        ""TeamId"": ""2"",
        ""TeamName"": ""Cleveland Cavaliers"",
        ""TeamPlayers"": [
            {""PlayerId"": ""1"", ""PlayerName"": ""Lebron James""},
            {""PlayerId"": ""2"", ""PlayerName"": ""Kyrie Irving""},
            {""PlayerId"": ""3"", ""PlayerName"": ""Anderson Varejao""},
            {""PlayerId"": ""4"", ""PlayerName"": ""Dion Waiters""},
            {""PlayerId"": ""5"", ""PlayerName"": ""Shawn Marion""}
        ]
    },
    {
        ""TeamId"": ""3"",
        ""TeamName"": ""Los Angeles Clippers"",
        ""TeamPlayers"": [
            {""PlayerId"": ""1"", ""PlayerName"": ""Chris Paul""},
            {""PlayerId"": ""2"", ""PlayerName"": ""Blake Griffin""},
            {""PlayerId"": ""3"", ""PlayerName"": ""DeAndre Jordan""},
            {""PlayerId"": ""4"", ""PlayerName"": ""Jamal Crawford""},
            {""PlayerId"": ""5"", ""PlayerName"": ""Matt Barnes""}
        ]
    }
]
}")
in
    Source




Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

I have gone through the blog referred by you earlier, but it did not help much, as the need was to expand the columns dynamically.

Also, when I had tried out the same blog practically, it gave me an expanded table as follows:

 

 UnwantedExpansion.JPG


Hence, I am not able to refer to any help online, that can do dynamic expansion of JSON without knowing the names of the columns, count of the columns or the content in it (which can be list/records/simple text). 

 

Also, I am new to Power Query and it is difficult to tweak the code as per the necessity.

 

Any help is appreciated.

Anonymous
Not applicable

Hi Sim,

 

Have you got any solution for expanding Lists and Records from JSON file dynamically?

 

Any help is greatly appreciated.

Thanks in advance!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors