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

Be 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

Reply
John343467
Helper I
Helper I

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 the values per year (available years). 

 

In the advanced editor, I've the following: 

let
Source = Json.Document(Web.Contents("https://www.tomtom.com/en_gb/traffic-index/page-data/" & CityCode & "-traffic/page-data.json")),
result = Source[result],
data = result[data],
citiesJson = data[citiesJson],
Custom1 = Table.FromRecords( { Source} ),
#"Expanded result" = Table.ExpandRecordColumn(Custom1, "result", {"data", "pageContext"}, {"result.data", "result.pageContext"}),
#"Expanded result.data" = Table.ExpandRecordColumn(#"Expanded result", "result.data", {"citiesJson"}, {"result.data.citiesJson"}),
#"Expanded result.data.citiesJson" = Table.ExpandRecordColumn(#"Expanded result.data", "result.data.citiesJson", {"key", "circleKey", "name", "country", "countryName", "position", "circle", "timezone", "availableYears", "roadNetwork", "stats2019", "stats2018", "stats2017"}, {"result.data.citiesJson.key", "result.data.citiesJson.circleKey", "result.data.citiesJson.name", "result.data.citiesJson.country", "result.data.citiesJson.countryName", "result.data.citiesJson.position", "result.data.citiesJson.circle", "result.data.citiesJson.timezone", "result.data.citiesJson.availableYears", "result.data.citiesJson.roadNetwork", "result.data.citiesJson.stats2019", "result.data.citiesJson.stats2018", "result.data.citiesJson.stats2017"}),
#"Expanded result.data.citiesJson.stats2017" = Table.ExpandRecordColumn(#"Expanded result.data.citiesJson", "result.data.citiesJson.stats2017", {"rank", "congestion", "results"}, {"result.data.citiesJson.stats2017.rank", "result.data.citiesJson.stats2017.congestion", "result.data.citiesJson.stats2017.results"}),
#"Expanded result.data.citiesJson.stats2018" = Table.ExpandRecordColumn(#"Expanded result.data.citiesJson.stats2017", "result.data.citiesJson.stats2018", {"rank", "congestion", "delta", "results"}, {"result.data.citiesJson.stats2018.rank", "result.data.citiesJson.stats2018.congestion", "result.data.citiesJson.stats2018.delta", "result.data.citiesJson.stats2018.results"}),
#"Expanded result.data.citiesJson.stats2019" = Table.ExpandRecordColumn(#"Expanded result.data.citiesJson.stats2018", "result.data.citiesJson.stats2019", {"rank", "congestion", "delta", "results"}, {"result.data.citiesJson.stats2019.rank", "result.data.citiesJson.stats2019.congestion", "result.data.citiesJson.stats2019.delta", "result.data.citiesJson.stats2019.results"})
in
#"Expanded result.data.citiesJson.stats2019"

 

I cannot however expand the list or when I do, it just creates new rows. 

 

Does anyone know how I can get the results per year. Thanks for any help. 

 

 

1 ACCEPTED SOLUTION

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"

Screenshot 2020-12-30 231856.png

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

7 REPLIES 7
parry2k
Super User
Super User

@John343467 unpivoting was the critical step in this to bring the years data on rows and then it is just a matter of getting the column values from the JSON which is pretty straightforward.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@John343467 I can help you to get the data if you tell me which data you want to pull in, there is a lot of data in that JSON, if you specify what columns you want, it will help.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k Thanks. I'm looking for the following columns. 

City,Year,Congestion,Rank,worstDay/month,bestDay/month,worstDay Congestion,Best Day Congestion

The result for Dublin (https://www.tomtom.com/en_gb/traffic-index/page-data/dublin-traffic/page-data.json) would then be as below. Thanks again. 🙂

 

Pic68.JPG

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"

Screenshot 2020-12-30 231856.png

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL Great, thank you. Just wondering how you worked that out? I was stuck on it for quite a while. Any good sources I could investigate? Thanks again. 

Hi, @John343467 , glad that the solution is of help.

 

When I began to learn M language, a guru suggested me spending time to gain a firm grasp of the very fundamental data containers in M, namely Record, List, Table. So I spent quite some hours gaining acquaintance of transformations among them by using functions, such as Table.FromColumns, Table.FromRows, Table.FromRecords, Record.ToList, etc. It seems now such efforts pay off for me. You might want to give it a shot.

Enjoy M language, enjoy Power Query!


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

amitchandak
Super User
Super User

@John343467 , have you tried these options ?

 

https://www.youtube.com/watch?v=ipI6mrWLQKA
https://www.mssqltips.com/sqlservertip/4621/using-power-bi-with-json-data-sources-and-files/
https://zappysys.com/blog/howto-import-json-rest-api-power-bi/?gclid=EAIaIQobChMI7Za92YSi6wIVFSQrCh0...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.