Smartsheet data in Excel via Power Query

1.1k Views Asked by At

The API output from Smartsheet returns rows and columns as separate objects,that are independent of each other. This results in separate records for the columns(A list of field names) and another set of records for the rows(records with a single field of values from various fields)

Is there a way to return a single list of JSON (with rows and columns resulting in a single list of records)?

This is the code I'm using in the Query Editor that returns separate Rows and Columns

= Web.Contents(
    "https://api.smartsheet.com/1.1/sheet/[SHEET_ID]",
    [
        Headers = 
        [
            #"Authorization" = "Bearer YOUR_API_TOKEN"
        ]
    ]
)
2

There are 2 best solutions below

0
On

I used the sample data on their site to come up with this set of transformations:

let
    Source = Json.Document(File.Contents("D:\testdata\foo.json")),
    ColumnIds = List.Transform(Source[columns], each Text.From([id])),
    ColumnNames = List.Transform(Source[columns], each [title]),
    Table = Table.FromList(Source[rows], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Expanded = Table.ExpandRecordColumn(Table, "Column1", {"rowNumber", "cells"}, {"rowNumber", "cells"}),
    Mapped = Table.TransformColumns(Expanded, {"cells",
        each Record.Combine(List.Transform(_, each Record.AddField([], Text.From([columnId]), [value])))}),
    Result = Table.ExpandRecordColumn(Mapped, "cells", ColumnIds, ColumnNames)
in
    Result
0
On

The Curt Hagenlocher's answer was exactly I was looking for

I did some changes to get rowIDs and solve some issue if the sheet or some column is empty

    let
    Source = Json.Document(File.Contents("C:\0\0\0\0\test.json")),
    ColumnIds = List.Transform(Source[columns], each Text.From([id])),
    ColumnNames = List.Transform(Source[columns], each [title]),
    Table = Table.FromList(Source[rows], Splitter.SplitByNothing(), 1, {"id", "rowNumber", "cells"}, ExtraValues.Error),
    Expanded = Table.ExpandRecordColumn(Table, "Column1", {"id", "rowNumber", "cells"}, {"id", "rowNumber", "cells"}),
    Mapped = Table.TransformColumns(Expanded, {"cells",
        each Record.Combine(List.Transform(_, each Record.AddField([], Text.From([columnId]), try [value] otherwise null)))}),
    Result = Table.ExpandRecordColumn(Mapped, "cells", ColumnIds, ColumnNames),
    #"Changed Type" = Table.TransformColumnTypes(Result,{{"id", type text}})
in
    #"Changed Type"