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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
bernate
Helper III
Helper III

Get Previous Value in Column

Hello, I have a Date column with the following values:

bernate_0-1713544979012.png

 

I want to create a new column named "Start Date" that would return the following. The 12/1/2010 value would be a Min Date from another table.

bernate_1-1713545167196.png

 

My current attempt is the M code below, but it does it on a row-by-row basis. I'm guessing I need to group by the Date field?

 

#"SortedRows" = Table.Sort(#"Changed Type1",{{"Date", Order.Ascending}}),
#"ShiftedDates" = {null} & List.RemoveLastN( Table.Column( SortedRows, "Date" ), 1 ),
#"ListOfLists" = Table.ToColumns( SortedRows ) & { ShiftedDates },
#"LastStep"= Table.FromColumns(
ListOfLists,
Table.ColumnNames( SortedRows ) & { "Previous Date" }
)
in
LastStep

 

1 ACCEPTED SOLUTION
dufoq3
Community Champion
Community Champion

I'm not sure if you want this and what is your source, but if you only want to shift 3 rows, you can do that this way.

 

dufoq3_0-1713620237014.png

 

let
    fnShift = (tbl as table, col as text, shift as nullable number, optional newColName as text, optional _type as type) as table =>
        //v 3. parametri zadaj zaporne cislo ak chces posunut riadky hore, kladne ak dole, 4. je nepovinny (novy nazov stlpca), 5. je nepovinny typ
        let
            a = Table.Column(tbl, col),
            b = if shift = 0 or shift = null then a else if shift > 0
                then List.Repeat({null}, shift) & List.RemoveLastN(a, shift)
                else List.RemoveFirstN(a, shift * -1) & List.Repeat({null}, shift * -1),    
            c = Table.FromColumns(Table.ToColumns(tbl) & {b}, Table.ColumnNames(tbl) &
                ( if newColName <> null then {newColName} else
                    if shift = 0 then {col & "_Duplicate"} else
                    if shift > 0 then {col & "_PrevValue"} 
                    else              {col & "_NextValue"} )),
            d = Table.TransformColumnTypes(c, {List.Last(Table.ColumnNames(c)), if _type <> null then _type else type any})
        in
            d,
            
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNzLUN7RUitVB51jqAxFpHENDfQMTwhwDoCWm+kYGVODEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    GroupedRows = Table.Group(Source, {"Date"}, {{"All", each _, type table}}),
    Ad_DatePrevValue = fnShift(GroupedRows, "Date", 1),
    Ad_Data = Table.AddColumn(Ad_DatePrevValue, "Data", each Table.AddColumn([All], "Start Date", (x)=> [Date_PrevValue]), type table),
    Data = Table.Combine(Ad_Data[Data])
in
    Data

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

7 REPLIES 7
dufoq3
Community Champion
Community Champion

Hi @bernate, I'm not sure why are you trying to shift rows when you need min date from another table. You can add this code as custom column:

List.Min(Another_Table[ColumnName_WithDateYouWant])

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

I will need the min date eventually but for now it can be left as null.

bernate_0-1713546701471.png

 

dufoq3
Community Champion
Community Champion

I'm not sure if you want this and what is your source, but if you only want to shift 3 rows, you can do that this way.

 

dufoq3_0-1713620237014.png

 

let
    fnShift = (tbl as table, col as text, shift as nullable number, optional newColName as text, optional _type as type) as table =>
        //v 3. parametri zadaj zaporne cislo ak chces posunut riadky hore, kladne ak dole, 4. je nepovinny (novy nazov stlpca), 5. je nepovinny typ
        let
            a = Table.Column(tbl, col),
            b = if shift = 0 or shift = null then a else if shift > 0
                then List.Repeat({null}, shift) & List.RemoveLastN(a, shift)
                else List.RemoveFirstN(a, shift * -1) & List.Repeat({null}, shift * -1),    
            c = Table.FromColumns(Table.ToColumns(tbl) & {b}, Table.ColumnNames(tbl) &
                ( if newColName <> null then {newColName} else
                    if shift = 0 then {col & "_Duplicate"} else
                    if shift > 0 then {col & "_PrevValue"} 
                    else              {col & "_NextValue"} )),
            d = Table.TransformColumnTypes(c, {List.Last(Table.ColumnNames(c)), if _type <> null then _type else type any})
        in
            d,
            
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNzLUN7RUitVB51jqAxFpHENDfQMTwhwDoCWm+kYGVODEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    GroupedRows = Table.Group(Source, {"Date"}, {{"All", each _, type table}}),
    Ad_DatePrevValue = fnShift(GroupedRows, "Date", 1),
    Ad_Data = Table.AddColumn(Ad_DatePrevValue, "Data", each Table.AddColumn([All], "Start Date", (x)=> [Date_PrevValue]), type table),
    Data = Table.Combine(Ad_Data[Data])
in
    Data

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Sorry I should have been more clear, the 3 rows was for example purposes. 02/21/19 has 48 rows, 09/09/19 has 51, etc. The number of rows varies by the date value, which is why I thought doing something to group by Date would let me reference the previous Date value and add it to the new column.

dufoq3
Community Champion
Community Champion

Hi, I've edited my previous post. Check it.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you for your help!

dufoq3
Community Champion
Community Champion

You're welcome.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.