Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi,
I am trying to tackle in a semi-dynamic manner the retreival of data from JSON files (with several levels of nesting for the data).
When 1st level of data is not a lateral I get columns which are Lists and Records.
I saw a cool tip on YouTube on combining list columns into a single added column and then expanding them all at one (Expand Multiple Lists Simultaneously in Power Query on Goodly Channel).
This row of code does exactly what I want but the column names here are "hard coded"
addListColumnsAsColumn = Table.AddColumn(relevantFirstLevel, "FirstLevelListsColumn", each Table.FromColumns({[name], [description], [externalId], [price], [overrideOption]})),
I have in my file a helper function which will return the list of relevant column name (in this case it will return this list of strings {"name", "description", "externalId", "price", "overrideOption"})
Any recommendation how I can, within the function statetement of adding a column (each), convert the list of column names to the list of values within those columns?
Solved! Go to Solution.
It took me some thought, time and some trial-and-error but eventually I managed to find a solution.
I created a helper function called listsToTableOfRecords which accepts the record and the list of names which we want the record items as a table or records (instead of Lists).
(inRecord as any, attributeNames as any) =>
let
Source = Record.SelectFields(inRecord, attributeNames),
numberOfAttributes = List.Count(attributeNames),
listOfColumns = List.Generate(
()=>0,
each _<numberOfAttributes, each _+1,
each Record.Field(Source, attributeNames{_})
),
Response = Table.FromColumns(listOfColumns, attributeNames)
in
Response
Instead of adding the column with the hard-coded columns, I called the new function.
= Table.AddColumn(relevantFirstLevel, "FirstLevelListsColumn", each listsToTableOfRecords(_, relevantListAttributes))
Hope it is useful to someone else in the future ...
Know of a simpler way, I'd love to learn.
It took me some thought, time and some trial-and-error but eventually I managed to find a solution.
I created a helper function called listsToTableOfRecords which accepts the record and the list of names which we want the record items as a table or records (instead of Lists).
(inRecord as any, attributeNames as any) =>
let
Source = Record.SelectFields(inRecord, attributeNames),
numberOfAttributes = List.Count(attributeNames),
listOfColumns = List.Generate(
()=>0,
each _<numberOfAttributes, each _+1,
each Record.Field(Source, attributeNames{_})
),
Response = Table.FromColumns(listOfColumns, attributeNames)
in
Response
Instead of adding the column with the hard-coded columns, I called the new function.
= Table.AddColumn(relevantFirstLevel, "FirstLevelListsColumn", each listsToTableOfRecords(_, relevantListAttributes))
Hope it is useful to someone else in the future ...
Know of a simpler way, I'd love to learn.
User | Count |
---|---|
5 | |
4 | |
3 | |
2 | |
2 |
User | Count |
---|---|
8 | |
6 | |
4 | |
4 | |
4 |