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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Anonymous
Not applicable

Expanding Records from a Single List

Hello all!

I tried and fail several times. I need help on expanding these records from a list. Kindly see the image below:

 

LuciannaFG_0-1645031279046.png

as you can see, I have one List with three Records (indicator_value, country_name,indicator_name), but I am able to expand just just one record in this query. I would like to have a single table that merges the columns from the first 2 records. Example:

 

Record: indicator_value:

 

LuciannaFG_1-1645031553287.png

 

Record: country_name:

LuciannaFG_2-1645031656239.png

 

So, I would like to have a final table looking like that:

 

Country CodeCoutry NameValueYear
DEUGermany0.9472019
DEUGermany0.946

2018

BRABrazil0.765

2019

 

Instead of (when I expand only indicator_value):

Country CodeValueYear
DEU0.9472019
DEU0.946

2018

BRA0.765

2019

 

Here is the API from I am getting the data:

http://ec2-54-174-131-205.compute-1.amazonaws.com/API/HDRO_API.php/indicator_id=137506/year=2018,201...

 

Thank you!

Kind regards,

Lucianna

 

1 ACCEPTED SOLUTION
KNP
Super User
Super User

Hi @Anonymous,

 

Please see attached PBIX. I think it achieves what you're looking for.

Let me know if you have any questions.

 

Here's the M code if you prefer...

let
  Source = Json.Document(
    Web.Contents(
      "http://ec2-54-174-131-205.compute-1.amazonaws.com/API/HDRO_API.php/indicator_id=137506/year=2018,2019"
    )
  ),
  #"Converted to Table" = Table.FromRecords({Source}),
  country_name = #"Converted to Table"{0}[country_name],
  CountryTable = Record.ToTable(country_name),
  Custom1 = #"Converted to Table"{0}[indicator_value],
  #"Converted to Table1" = Record.ToTable(Custom1),
  #"Expanded Value" = Table.ExpandRecordColumn(
    #"Converted to Table1",
    "Value",
    {"137506"},
    {"137506"}
  ),
  #"Expanded 137506" = Table.ExpandRecordColumn(
    #"Expanded Value",
    "137506",
    {"2018", "2019"},
    {"2018", "2019"}
  ),
  #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(
    #"Expanded 137506",
    {"Name"},
    "Year",
    "Value"
  ),
  IndicatorTable = Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"Value", type number}}),
  Custom2 = Table.NestedJoin(
    CountryTable,
    "Name",
    IndicatorTable,
    "Name",
    "Custom",
    JoinKind.LeftOuter
  ),
  #"Expanded Custom" = Table.ExpandTableColumn(
    Custom2,
    "Custom",
    {"Year", "Value"},
    {"Indicator.Year", "Indicator.Value"}
  )
in
  #"Expanded Custom"

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

View solution in original post

4 REPLIES 4
KNP
Super User
Super User

Hi @Anonymous,

 

Please see attached PBIX. I think it achieves what you're looking for.

Let me know if you have any questions.

 

Here's the M code if you prefer...

let
  Source = Json.Document(
    Web.Contents(
      "http://ec2-54-174-131-205.compute-1.amazonaws.com/API/HDRO_API.php/indicator_id=137506/year=2018,2019"
    )
  ),
  #"Converted to Table" = Table.FromRecords({Source}),
  country_name = #"Converted to Table"{0}[country_name],
  CountryTable = Record.ToTable(country_name),
  Custom1 = #"Converted to Table"{0}[indicator_value],
  #"Converted to Table1" = Record.ToTable(Custom1),
  #"Expanded Value" = Table.ExpandRecordColumn(
    #"Converted to Table1",
    "Value",
    {"137506"},
    {"137506"}
  ),
  #"Expanded 137506" = Table.ExpandRecordColumn(
    #"Expanded Value",
    "137506",
    {"2018", "2019"},
    {"2018", "2019"}
  ),
  #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(
    #"Expanded 137506",
    {"Name"},
    "Year",
    "Value"
  ),
  IndicatorTable = Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"Value", type number}}),
  Custom2 = Table.NestedJoin(
    CountryTable,
    "Name",
    IndicatorTable,
    "Name",
    "Custom",
    JoinKind.LeftOuter
  ),
  #"Expanded Custom" = Table.ExpandTableColumn(
    Custom2,
    "Custom",
    {"Year", "Value"},
    {"Indicator.Year", "Indicator.Value"}
  )
in
  #"Expanded Custom"

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x
Anonymous
Not applicable

@KNP that's incredible! Thank you so much!!

 

I have a question about this part of the M code. Could you please explain what it is doing? The "empty" symbol. 

 

LuciannaFG_1-1645088297574.png

 

Thank you!

So the #"Converted to Table" is obviously referencing the earlier step, and the [indicator_value] the column. The {0} is referring to the first row. Power Query is 0 indexed, so {1} would be the second row. In your case it doesn't matter as there is only one row. If there were more rows, the code would need to be adjusted accordingly. 

I hope this clarifies.

 

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x
Anonymous
Not applicable

Ok! Thank you, @KNP !

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 FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors