SOLVED

Power Query Formulas

%3CLINGO-SUB%20id%3D%22lingo-sub-2122306%22%20slang%3D%22en-US%22%3EPower%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2122306%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20table%20that%20shows%20students%20Col%20A%2C%20test%20date%20Col%20B%2C%20and%20test%20score%20Col%20C.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20I%20sort%20by%20student-asc%2C%20then%20test%20date-asc%2C%20the%20formula%20%3DIF(A2%3DA1%2C%2BD1%2B1%2C1)%20in%20column%20D%20tells%20me%20what%20how%20many%20tests%20the%20student%20has%20taken.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EColumn%20E%20shows%20the%20difference%20between%20test%202%20and%20test%201%20if%20the%20row%20is%20the%20second%20test%20for%20the%20student.%26nbsp%3B%20%3DIF(D3%3D2%2C%2BC3-C2%2C%22%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EColumn%20F%20shows%20the%20difference%20between%20test%203%20and%20test%201%20if%20the%20row%20is%20the%20third%20test%20for%20the%20student.%26nbsp%3B%20%3DIF(D3%3D3%2C%2BD3-D1%2C%22%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20copy%20the%20formulas%2C%20the%20work%20as%20expected%20because%20the%20cell%20references%20are%20not%20anchored.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%E2%80%99m%20trying%20build%20this%20in%20power%20query%2C%20and%20I%20can%20get%20the%20first%20three%20columns%20without%20a%20problem%2C%20but%20I%E2%80%99m%20having%20a%20devil%20of%20a%20time%20trying%20to%20figure%20out%20how%20to%20add%20the%20formulas.%20I%20know%20it's%20Add%20Column%2C%20but%20how%20to%20reference%20the%20row%20above%20the%20current%20row%20to%20evaluate%20if%20the%20student%20is%20the%20same%20or%20not.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELikewise%2C%20I%20can%E2%80%99t%20figure%20out%20how%20to%20reference%201%20row%20above%20if%20it%E2%80%99s%20test%202%20and%202%20rows%20above%20if%20it%E2%80%99s%20test%203.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20on%20this%20would%20be%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2122306%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2123003%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2123003%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%26nbsp%3B%20I%20will%20check%20this%20out%20tonight.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2122351%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2122351%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964062%22%20target%3D%22_blank%22%3E%40AFFCF_Grants_Admin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20not%20necessary%20and%20better%20not%20to%20repeat%20Excel%20formulas%20literally.%20Group%20records%20by%20names%20without%20aggregation%20and%20within%20it%20work%20with%20each%20name%20as%20with%20separate%20table.%20Add%20Index%20as%20Test%20Number%2C%20with%20that%20for%20second%20row%20take%20difference%20in%20Score%20between%20it%20and%20Score%20in%20first%20row%3B%20in%20next%20column%20same%20for%20the%20third%20row.%20Please%20note%20in%20Power%20Query%20numbering%20starts%20from%20zero.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20may%20do%20latest%20transformations%20on%20separate%20query%20with%20only%20one%20name%2C%20after%20that%20copy%2Fpaste%20code%20under%20the%20grouping%2C%20here%20is%20only%20to%20change%20table%20name.%20Entire%20script%20looks%20like%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Tests%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20%23%22Changed%20Type%22%20%3D%20Table.TransformColumnTypes(Source%2C%7B%7B%22Date%22%2C%20type%20date%7D%7D)%2C%0A%20%20%20%20%23%22Sorted%20Rows%22%20%3D%20Table.Sort(%0A%20%20%20%20%20%20%20%20%23%22Changed%20Type%22%2C%7B%7B%22Youth%22%2C%20Order.Ascending%7D%2C%20%7B%22Date%22%2C%20Order.Ascending%7D%7D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Grouped%20Rows%22%20%3D%20Table.Group(%0A%20%20%20%20%20%20%20%20%23%22Sorted%20Rows%22%2C%0A%20%20%20%20%20%20%20%20%7B%22Youth%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Group%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20each%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TestNumber%20%3D%20Table.AddIndexColumn(%0A%20%20%20%20%20%20%20%20_%2C%0A%20%20%20%20%20%20%20%20%22Test%20Number%22%2C%201%2C%201%2C%20Int64.Type%0A%20%20%20%20)%2C%0A%20%20%20%20Diff2%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20TestNumber%2C%0A%20%20%20%20%20%20%20%20%22Diff2-1%22%2C%0A%20%20%20%20%20%20%20%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%5BTest%20Number%5D%20%3D%202%0A%20%20%20%20%20%20%20%20%20%20%20%20then%20TestNumber%5BScore%5D%7B1%7D-TestNumber%5BScore%5D%7B0%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20null%0A%20%20%20%20)%2C%0A%20%20%20%20Diff3%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20Diff2%2C%0A%20%20%20%20%20%20%20%20%22Diff3-1%22%2C%0A%20%20%20%20%20%20%20%20each%0A%20%20%20%20%20%20%20%20if%20%5BTest%20Number%5D%20%3D%203%0A%20%20%20%20%20%20%20%20then%20Diff2%5BScore%5D%7B2%7D-Diff2%5BScore%5D%7B0%7D%0A%20%20%20%20%20%20%20%20else%20null%0A%20%20%20%20)%20%5D%5BDiff3%5D%0A%20%20%20%20%20%20%20%20%7D%7D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Removed%20Columns%22%20%3D%20Table.RemoveColumns(%23%22Grouped%20Rows%22%2C%7B%22Youth%22%7D)%2C%0A%20%20%20%20%23%22Expanded%20Group%22%20%3D%20Table.ExpandTableColumn(%0A%20%20%20%20%20%20%20%20%23%22Removed%20Columns%22%2C%0A%20%20%20%20%20%20%20%20%22Group%22%2C%0A%20%20%20%20%20%20%20%20%7B%22Youth%22%2C%20%22Date%22%2C%20%22Score%22%2C%20%22Test%20Number%22%2C%20%22Diff2-1%22%2C%20%22Diff3-1%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22Youth%22%2C%20%22Date%22%2C%20%22Score%22%2C%20%22Test%20Number%22%2C%20%22Diff2-1%22%2C%20%22Diff3-1%22%7D%0A%20%20%20%20)%0Ain%0A%20%20%20%20%23%22Expanded%20Group%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2122323%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2122323%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F964062%22%20target%3D%22_blank%22%3E%40AFFCF_Grants_Admin%3C%2FA%3E%26nbsp%3BPerhaps%20easiest%20to%20refer%20to%20one%20of%20many%20on-line%20resources%20that%20address%20your%20question.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.goodly.co.in%2Frefer-previous-row-next-row-power-query%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.goodly.co.in%2Frefer-previous-row-next-row-power-query%2F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2123025%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2123025%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%26nbsp%3B%20I%20will%20check%20this%20out%20tonight.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2132618%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Formulas%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2132618%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20works%20perfectly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20very%20much!!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have a table that shows students Col A, test date Col B, and test score Col C.

 

After I sort by student-asc, then test date-asc, the formula =IF(A2=A1,+D1+1,1) in column D tells me what how many tests the student has taken.

 

Column E shows the difference between test 2 and test 1 if the row is the second test for the student.  =IF(D3=2,+C3-C2,"")

 

Column F shows the difference between test 3 and test 1 if the row is the third test for the student.  =IF(D3=3,+D3-D1,"")

 

When I copy the formulas, the work as expected because the cell references are not anchored.

 

I’m trying build this in power query, and I can get the first three columns without a problem, but I’m having a devil of a time trying to figure out how to add the formulas. I know it's Add Column, but how to reference the row above the current row to evaluate if the student is the same or not.

 

Likewise, I can’t figure out how to reference 1 row above if it’s test 2 and 2 rows above if it’s test 3.

 

Any help on this would be appreciated.

6 Replies

@AFFCF_Grants_Admin Perhaps easiest to refer to one of many on-line resources that address your question. 

https://www.goodly.co.in/refer-previous-row-next-row-power-query/ 

best response confirmed by AFFCF_Grants_Admin (New Contributor)
Solution

@AFFCF_Grants_Admin 

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"

 

@Riny_van_Eekelen 

 

Thank you.  I will check this out tonight.

@Sergei Baklan 

 

Thank you.  I will check this out tonight.

@Sergei Baklan 

 

This works perfectly.

 

Thank you very much!!!

 

@AFFCF_Grants_Admin , you are welcome