Excel: Power Query - how to repeat block of code for each row in a table
It appears you want to have a result with two columns date
and data
. If that is the case, all you need to do is expand the table that results from Step 3.
Merely click the double headed arrow at the top of the column
M Code
let
Source = Json.Document(File.Contents("C:\Users\ron\Desktop\new 3.json")),
data = Source[data],
#"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"date", "data"}, {"date", "data"})
in
#"Expanded Column1"
Result
You should also set the data types after you have expanded the columns
Lets start from beginning
let
Source = Json.Document(...),
data = Source[data],
#"ToTable1" = Record.ToTable(Column1)
Now we have List
of Record
s stored in #"ToTable1"[Column1]
. (Because table Columns are List
s.)
So we can use List.Transform
function to manipulate with each Item
of this List
.
Syntax is
List.Transform(list as list, transform as function) as list
So we need some transform function which works with each Item of the List separately. Let modify a Code Block from the Original question.
MyFunc = (ColumnRow) =>
let
#"ToTableX" = Record.ToTable(ColumnRow),
#"TransposeX" = Table.Transpose(#"ToTableX"),
#"HeadersX" = Table.PromoteHeaders(#"TransposeX", [PromoteAllScalars=true]),
#"ChangeTypeX" = Table.TransformColumnTypes(#"HeadersX",{{"date", type date}, {"value", Int64.Type}, type any}})
in
#"ChangeTypeX"
So whole List.Tranform
function code looks like
List.Transform(#"ToTable1"[Column1], MyFunc)
This returns a List
of Table
s which could be used in Table.Combine
function.
MyResultTable = Table.Combine(List.Transform(#"ToTable1"[Column1], MyFunc))
Put it all together
let
Source = Json.Document(...),
data = Source[data],
#"ToTable1" = Record.ToTable(Column1)
// End of original code used here
MyList = #"ToTable1"[Column1], //For better readability
MyFunc = (ColumnRow) =>
let
#"ToTableX" = Record.ToTable(ColumnRow),
#"TransposeX" = Table.Transpose(#"ToTableX"),
#"HeadersX" = Table.PromoteHeaders(#"TransposeX", [PromoteAllScalars=true]),
#"ChangeTypeX" = Table.TransformColumnTypes(#"HeadersX",{{"date", type date}, {"value", Int64.Type}, type any}})
in
#"ChangeTypeX",
MyResultTable = Table.Combine(List.Transform(MyList, MyFunc))
in
MyResultTable