Forum Discussion

John343467's avatar
John343467
Icon for Helper I rankHelper I
5 years ago
Solved

Expanding Json list

Hi,    I'm not sure how to do this. I'm currently accessing this web api in powerbi.  https://www.tomtom.com/en_gb/traffic-index/page-data/stuttgart-traffic/page-data.json   I'm trying to list t...
  • CNENFRNL's avatar
    CNENFRNL
    5 years ago

    Hi, John343467 , the solution is really verbose as requested fields lie in different layers of hierarchy. you might want to try this solution,

    let
        Source = Json.Document(Web.Contents("https://www.tomtom.com/en_gb/traffic-index/page-data/stuttgart-traffic/page-data.json")),
    
        tbl = Table.FromRecords({Source[result][data][citiesJson]}),
        #"Removed Columns" = Table.SelectColumns(tbl, {"name"} & List.Select(Table.ColumnNames(tbl), each Text.Contains(_,"stats"))),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"name"}, "Year", "Record"),
        #"Extracted Yr" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Year", each Number.From(Text.Select(_,{"0".."9"})), type number}}),
        #"Expanded Record" = Table.ExpandRecordColumn(#"Extracted Yr", "Record", {"rank", "congestion", "results"}, {"rank", "congestion", "results"}),
        results = Table.TransformColumns(#"Expanded Record", 
            {
                {"results",
                each
                    [
                        worst = [worstDay],
                        best = [bestDay],
                        res = [
                                #"worst Day/Month" = Text.From(worst[day])&"/"&Text.From(worst[month]),
                                #"worst Congestion" = Number.Round(worst[congestion]),
                                #"best Day/Month" = Text.From(best[day])&"/"&Text.From(best[month]),
                                #"best Congestion" = Number.Round(best[congestion])
                        ]
                    ][res]
                }
            }
        ),
        #"Expanded results" = Table.ExpandRecordColumn(results, "results", {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"}, {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"})
    in
        #"Expanded results"