Forum Discussion
Power Query Formulas
- Feb 11, 2021
It's not necessary and better not to repeat Excel formulas literally. Group records by names without aggregation and within it work with each name as with separate table. Add Index as Test Number, with that for second row take difference in Score between it and Score in first row; in next column same for the third row. Please note in Power Query numbering starts from zero.
You may do latest transformations on separate query with only one name, after that copy/paste code under the grouping, here is only to change table name. Entire script looks like
let Source = Excel.CurrentWorkbook(){[Name="Tests"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}), #"Sorted Rows" = Table.Sort( #"Changed Type",{{"Youth", Order.Ascending}, {"Date", Order.Ascending}} ), #"Grouped Rows" = Table.Group( #"Sorted Rows", {"Youth"}, {{ "Group", each [ TestNumber = Table.AddIndexColumn( _, "Test Number", 1, 1, Int64.Type ), Diff2 = Table.AddColumn( TestNumber, "Diff2-1", each if [Test Number] = 2 then TestNumber[Score]{1}-TestNumber[Score]{0} else null ), Diff3 = Table.AddColumn( Diff2, "Diff3-1", each if [Test Number] = 3 then Diff2[Score]{2}-Diff2[Score]{0} else null ) ][Diff3] }} ), #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Youth"}), #"Expanded Group" = Table.ExpandTableColumn( #"Removed Columns", "Group", {"Youth", "Date", "Score", "Test Number", "Diff2-1", "Diff3-1"}, {"Youth", "Date", "Score", "Test Number", "Diff2-1", "Diff3-1"} ) in #"Expanded Group"
It's not necessary and better not to repeat Excel formulas literally. Group records by names without aggregation and within it work with each name as with separate table. Add Index as Test Number, with that for second row take difference in Score between it and Score in first row; in next column same for the third row. Please note in Power Query numbering starts from zero.
You may do latest transformations on separate query with only one name, after that copy/paste code under the grouping, here is only to change table name. Entire script looks like
let
Source = Excel.CurrentWorkbook(){[Name="Tests"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Sorted Rows" = Table.Sort(
#"Changed Type",{{"Youth", Order.Ascending}, {"Date", Order.Ascending}}
),
#"Grouped Rows" = Table.Group(
#"Sorted Rows",
{"Youth"},
{{
"Group",
each [
TestNumber = Table.AddIndexColumn(
_,
"Test Number", 1, 1, Int64.Type
),
Diff2 = Table.AddColumn(
TestNumber,
"Diff2-1",
each
if [Test Number] = 2
then TestNumber[Score]{1}-TestNumber[Score]{0}
else null
),
Diff3 = Table.AddColumn(
Diff2,
"Diff3-1",
each
if [Test Number] = 3
then Diff2[Score]{2}-Diff2[Score]{0}
else null
) ][Diff3]
}}
),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Youth"}),
#"Expanded Group" = Table.ExpandTableColumn(
#"Removed Columns",
"Group",
{"Youth", "Date", "Score", "Test Number", "Diff2-1", "Diff3-1"},
{"Youth", "Date", "Score", "Test Number", "Diff2-1", "Diff3-1"}
)
in
#"Expanded Group"
- SergeiBaklanFeb 14, 2021Diamond Contributor
AFFCF_Grants_Admin , you are welcome