Forum Discussion

Alexandra_B's avatar
Alexandra_B
Helper III
2 years ago
Solved

Expand Record from a column

Hello.

 

I am using an API to get the data from a Shelly device:

The script is:

let
    // Define the URL
    url = "https://shelly-<xx>-eu.shelly.cloud/device/status",

    // Define the request body
    requestBody = Text.ToBinary("id=<device_ID>&auth_key=<API_Key>"),

    // Define the headers (if needed)
    headers = [
        #"Content-Type" = "application/x-www-form-urlencoded"
    ],

    // Make the POST request
    response = Web.Contents(url, [
        Headers = headers,
        Content = requestBody
    ]),

    // Parse the JSON response
    jsonResponse = Json.Document(response),
    
    // Access the 'data' field
    data = jsonResponse[data],

    // Access the 'device_status' field
    device_status = data[device_status],

    // Convert the record to a table for easier expansion
    device_status_table = Record.ToTable(device_status)
in
    device_status_table

 

Then, my result is this:


I am trying to get the Record values in each column, but I don`t know how to do it quickly enough, and not one at a time, as the results of the API are like this:

 

"device_status":

{"mqtt":{"connected":false},
"switch:0":{"id":0,"aenergy":{"by_minute":[0,0,0],"minute_ts":1717071958,"total":0},"source":"init","output":false,"apower":0,"voltage":249.4,"current":0,"pf":0,"temperature":{"tC":31.3,"tF":88.4}},

"ble":[],

"id":"<device_id>",

"_updated":"2024-05-30 12:26:28",

"input:0":{"id":0,"state":false},

"v_eve:0":{"ev":"","ttl":-1,"id":0},

"eth":{"ip":null},

"ts":1717071960.41,

"code":"codeU",

"v_eve:1":{"ev":"","ttl":-1,"id":1},

"input:1":{"id":1,"state":false},

"serial":940,

"wifi":{"sta_ip":"192.x.x.x","status":"got ip","ssid":"<WiFi_name>","rssi":-65},

"cloud":{"connected":true},

"sys":{"mac":"<mac_address>","restart_required":false,"time":"11:14","unixtime":1717064091,"uptime":20691,"ram_size":238928,"ram_free":142096,"fs_size":524288,"fs_free":282624,"cfg_rev":9,"available_updates":{"stable":{"version":"1.3.2"}}}}}}

 

Please, could you share your experience?

 

Thank you.

  • You won't have much of a choice as you have nested records too.  You will need to decide which records to keep, which to throw away, and how to flatten the hierarchies.  Not really something you can do automatically.

3 Replies

  • You won't have much of a choice as you have nested records too.  You will need to decide which records to keep, which to throw away, and how to flatten the hierarchies.  Not really something you can do automatically.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Alexandra_B 

     

    You can explore Record functions in Power Query from the following documentation. As all records don't share the same fields, you need to first filter the required fields and analyze which ones need to be organized into the same column. Then we can explore how to expand it more efficiently. 

    Record functions - PowerQuery M | Microsoft Learn

     

    If you only want to expand all records into Text format in a column, you can try the solution from below thread:

    Solved: Convert record to text in power BI - Microsoft Fabric Community

     

    Best Regards,
    Jing
    If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

  • Thank you, everyone.
    I ended up adding Custom Columns based on the following formula (adapted for each column):

    if 
       Value.Is ([Value], type record) 
       then 
            Record.Field([Value], "connected") 
    else 
        ""

     

    Resulting in:

     

      


     

     

    Thank you for your support.