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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

v-lionel-msft

Json Transformations

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:

v-lionel-msft_0-1621847633542.png

Output in power query:

v-lionel-msft_1-1621847645634.png

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:

v-lionel-msft_2-1621847660260.png

Expected output:

v-lionel-msft_3-1621847671457.png

Preface

If we connect to this .json file and expand all columns directly, it will get this table which is not we wanted:

v-lionel-msft_4-1621847682860.png

Operations:

  1. From the .json file we can get one list column with a single structure ‘[ID]’ and another list column with a composite ‘[Contents]’

v-lionel-msft_6-1621847767143.png

  1. Expand the [Contents] column, we will get two rows with Records. In addition, for transforming data more conveniently, we can rename the columns.

v-lionel-msft_7-1621847767148.png

  1. Add a custom column to extract the ‘values’ list from the .json file:

#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Value", each [Contents][Price]{0}[values])

v-lionel-msft_8-1621847767153.png

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.

v-lionel-msft_9-1621847767154.png

v-lionel-msft_10-1621847767154.png

  1. Add the second column to extract [Products] records’ values, here returns a new record.

#"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.

v-lionel-msft_11-1621847767155.png

v-lionel-msft_12-1621847767155.png 

v-lionel-msft_13-1621847767155.png

  1. Expand the new Record column [Products] to get the list columns.

v-lionel-msft_14-1621847767157.png

  1. Add the third column to ‘pack and compress’ the [ID] column and [Value] column as new lists, also use List.Zip()

#"Added Custom2" = Table.AddColumn(#"Expanded Products", "Custom", each List.Zip({[ID],[Value]}))

v-lionel-msft_15-1621847767158.png

v-lionel-msft_16-1621847767158.png 

v-lionel-msft_17-1621847767158.png

v-lionel-msft_18-1621847767159.png

v-lionel-msft_19-1621847767159.png

  1. After the above steps, we have basically completed the data conversion, and the rest is to organize the data. Only keep the two product columns and the Custom column, remove other columns and expand them.

v-lionel-msft_20-1621847767161.png

  1. Compare with the expected output, we need to filter the two Products column

v-lionel-msft_21-1621847767162.png

  1. Convert the list column to record column, expand the record column to get the ID column and Value column. If we expand the list directly, we will make the rows expanded down. So need to use Table.TransformColumns() and Record.FromList() to convert.

#"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"})

v-lionel-msft_22-1621847767164.png

  1. Reorder the columns, rename and change the data type through the GUI to get the expected result.

v-lionel-msft_23-1621847767165.png

 

Main Functions reference:

  1. List.Zip()
  2. Record.ToList()
  3. Record.FromList()
  4. Table.Transformcolumns()

 

Author: Yingjie Li

Reviewer: Ula Huang, Kerry Wang