March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Scenario:
In Power BI Desktop, when we connect to a .json file, power query editor will transform it automatically if its structure is single.
Sample data1:
Output in power query:
From the APPLIED STEPS we can find that power query editor expands all columns automatically to get this result table.
Extension:
If the json file’s structure is a bit complex and expand all columns which could not get the expected result, how can we transform the file in power query?
Sample data2:
Expected output:
Preface:
If we connect to this .json file and expand all columns directly, it will get this table which is not we wanted:
Operations:
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Value", each [Contents][Price]{0}[values])
Note: This step is the combination of the steps that we click ‘Record => Price[list] => Record => values[lists]’, just add a custom column to show it.
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Products", each Record.FromList(
List.Zip(
Record.ToList([Contents][Products])),
{"A_B","B_C"}
))
Note: For this step, we need to use List.Zip() function to ‘pack and compress’ a new list due to the column and row transformation, then transform it to a record for next steps.
#"Added Custom2" = Table.AddColumn(#"Expanded Products", "Custom", each List.Zip({[ID],[Value]}))
#"List to Record" = Table.TransformColumns(#"Filtered Rows",{"Custom",each Record.FromList(_,{"ID","values"})}),
#"Expanded Custom1" = Table.ExpandRecordColumn(#"List to Record", "Custom", {"ID", "values"}, {"Custom.ID", "Custom.values"})
Main Functions reference:
Author: Yingjie Li
Reviewer: Ula Huang, Kerry Wang
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.