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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
JanoLehocky
Helper I
Helper I

Table.AddColumn help - adding more than one previous row column

Good afternoon Power BI Community,

 

I have a case where I need to create two custom columns that shows the previou value of two different columns within each sub group.

 

i.e.: in my case I need to know the previous row for each employee to understand:

1. what the previous salary band was

2. what the previous employee subgroup was (autopaid hrly, salaried, etc,,)

 

I was super happy to find this video:

https://www.youtube.com/watch?v=IGF2-qfzDQs&t=796s

 

All went well and I was able to create my previous salary band column. However... How to create a second previous row column for EE Subgroup?

 

THIS WAS THE CODE TO CREATE THE PrevRowSB

 

= Table.AddColumn(Custom1, "Custom", each let
AllDataTable = [FullTable],
PrevRowValue =
Table.AddColumn(
AllDataTable, "PrevRowSB",
each try AllDataTable[Salary Band] {[Index] - 1}
otherwise null
)
in
PrevRowValue)

JanoLehocky_1-1634953751543.png

 

 

I just want to have an additional column called 'PrevEEsubgroupname' that for above should list:

null

Salaried

Salaried

Salaried

Salaried

Salaried

 

 

Would appreciate any assistance with this.. 

 

Thank you

Jano

1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @JanoLehocky ,

You can try this query to get the previous row value in Power Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("1ZRNT4NAEIb/yoRzD9BKhaO1msbUj7QkHpoeRpjYDQvbLLs2/HsHPChQEzTBVg6b5d152Hdn2NlsnMlk4oycmRbGYF7Cek+oC1bWKFELSnjquTzcE4fAit4EHZztqB/o8fCkVaZqIggCfr+RUihj4FpJyjBvEg8VgCUUMUoCkceasKC/ox/NjvSgwDf+PNd163TamGBJxPMra9QeRQILZbUsWfkR/rUO40YdegR3qh16Psu3WB0XIpG1oel/hMKQ5YV93cEzyRfOcqteXtCocL/4896miwVhraNOITqgaEPHMzcEdHliqH545c4WRuQQaZsQ2hZ68Xu0apdzypQRKv+4h+PqayuVsABLm+IRm5+/RZ/ojrMz2WJgX9N2Zn2/agdzQYbbwUxinvYxdXKq2/H9Drt9Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Personnel no." = _t, #"Personnel Number" = _t, #"EE subgroup name" = _t, #"Salary Band" = _t, #"Reason for Changing Master Dat" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Personnel no.", Int64.Type}, {"Personnel Number", type text}, {"EE subgroup name", type text}, {"Salary Band", type text}, {"Reason for Changing Master Dat", type text}}),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type", {"Personnel no."}, 
            {
                {
                    "Data", each 
                    let tab=Table.AddIndexColumn(_,"Index",1,1,Int64.Type) in
                        Table.AddColumn(
                            tab,"New",
                            (x)=> try Table.Max(Table.SelectRows(tab,(y)=>y[Index]=x[Index]-1),"Index")[EE subgroup name]
                                    otherwise null ), 
                type table [#"Personnel no."=nullable number, Personnel Number=nullable text, EE subgroup name=nullable text, Salary Band=nullable text, Reason for Changing Master Dat=nullable text]
                }
            }
        )
in
    #"Grouped Rows"

vyingjl_0-1635211349165.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
v-yingjl
Community Support
Community Support

Hi @JanoLehocky ,

You can try this query to get the previous row value in Power Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("1ZRNT4NAEIb/yoRzD9BKhaO1msbUj7QkHpoeRpjYDQvbLLs2/HsHPChQEzTBVg6b5d152Hdn2NlsnMlk4oycmRbGYF7Cek+oC1bWKFELSnjquTzcE4fAit4EHZztqB/o8fCkVaZqIggCfr+RUihj4FpJyjBvEg8VgCUUMUoCkceasKC/ox/NjvSgwDf+PNd163TamGBJxPMra9QeRQILZbUsWfkR/rUO40YdegR3qh16Psu3WB0XIpG1oel/hMKQ5YV93cEzyRfOcqteXtCocL/4896miwVhraNOITqgaEPHMzcEdHliqH545c4WRuQQaZsQ2hZ68Xu0apdzypQRKv+4h+PqayuVsABLm+IRm5+/RZ/ojrMz2WJgX9N2Zn2/agdzQYbbwUxinvYxdXKq2/H9Drt9Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Personnel no." = _t, #"Personnel Number" = _t, #"EE subgroup name" = _t, #"Salary Band" = _t, #"Reason for Changing Master Dat" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Personnel no.", Int64.Type}, {"Personnel Number", type text}, {"EE subgroup name", type text}, {"Salary Band", type text}, {"Reason for Changing Master Dat", type text}}),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type", {"Personnel no."}, 
            {
                {
                    "Data", each 
                    let tab=Table.AddIndexColumn(_,"Index",1,1,Int64.Type) in
                        Table.AddColumn(
                            tab,"New",
                            (x)=> try Table.Max(Table.SelectRows(tab,(y)=>y[Index]=x[Index]-1),"Index")[EE subgroup name]
                                    otherwise null ), 
                type table [#"Personnel no."=nullable number, Personnel Number=nullable text, EE subgroup name=nullable text, Salary Band=nullable text, Reason for Changing Master Dat=nullable text]
                }
            }
        )
in
    #"Grouped Rows"

vyingjl_0-1635211349165.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

mahoneypat
Employee
Employee

While this can be done within your query, it would be much easier to do with a DAX calculated column.  Please provide some sample/mock data in a copy/paste-able format (or link to data), so specific DAX expressions can be suggested.

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hello Pat,

 

Thanks for offering your knowledge here.

 

Here is some sample data:

 

https://docs.google.com/spreadsheets/d/1xk_yEhXPLVnvf7ki59lvXgYbLxqfiak1/edit?usp=sharing&ouid=10053...

 

Cheers

Jano

Your data didn't have a date or index column to know which is the previous, so I added an Index column in the query editor.  Once that is in place, you can make two columns with this pattern to get the previous values.  Replace Personnel with your actual table name.

 

Prev Band =
VAR thisperson = Personnel[Personnel no.]
VAR thisindex = Personnel[Index]
RETURN
    MINX (
        FILTER (
            Personnel,
            Personnel[Personnel no.] = thisperson
                && Personnel[Index] = thisindex - 1
        ),
        Personnel[Salary Band]
    )

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors