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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jerryr125
Helper III
Helper III

populate a column from a previous value

Hi - 

Not sure if this can be done.  

I have the following table in which I would like to create a column from previous values.

 

Example :

table_abc

ProductIDYear-MonthQuantity 
A01/202410 
A02/202430 
A03/202450 
A01/202520 
A02/202527 
A03/20257 
B01/202410 
B02/202415 
B01/202517 
B02/202532 

 

 

Output (new column prior-year-month-quantity):

ProductIDYear-MonthQuantityprior-year-month-quantity
A01/202410 
A02/202430 
A03/202450 
A01/20252010
A02/20252730
A03/2025750
B01/202410 
B02/202415 
B01/20251710
B02/20253215



Any thoughts ? Thanks - Jerry

6 REPLIES 6
Abhilash_P
Responsive Resident
Responsive Resident

Hi @jerryr125 ,

You can create a calculated column in power bi desktop with below DAX code

prior-year-month-quantity =
VAR CurrentProduct = table_abc[ProductID]
VAR CurrentDate = DATEVALUE("01-" & table_abc[Year-Month])
VAR PriorYearDate = EDATE(CurrentDate, -12)

RETURN
CALCULATE(
MAX(table_abc[Quantity]),
FILTER(
table_abc,
table_abc[ProductID] = CurrentProduct &&
DATEVALUE("01-" & table_abc[Year-Month]) = PriorYearDate
)
)

PwerQueryKees
Super User
Super User

And another possible solution..

 

Staring with: (note that the dates are provided as a string).

PwerQueryKees_0-1751118431686.png

 

Using this:

let
    Source = ProductQuantityByMonth,  // replace with your table
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProductID", type text}, {"Year-Month", type date}, {"Quantity", Int64.Type}}),  // The change type interprets the date string as an actual date
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Last-Year-Month Quantity", each  // we add a new column
            let 
                last_year = Table.SelectRows(#"Changed Type",     // get the previous month's row
                    (row) => // a funtion with the current row as a parameter
                        row[ProductID] = [ProductID] // for the same product
                        and row[#"Year-Month"] = Date.AddYears([#"Year-Month"],-1))  // for the previous year
                        [Quantity]  // but only the column [Quantity]
                in 
                    List.SingleOrDefault(last_year)  //  returns the first and only element of the list, null when empty or generarate an error if there are more. You can change this to a List.Sum() if the same month can occur more than once for the same product.
        )
in
    #"Added Custom"

 

Producing this:

PwerQueryKees_0-1751119072286.png

 

Nasif_Azam
Solution Sage
Solution Sage

Hey @jerryr125 ,

Calculated column that retrieves the quantity from the same ProductID and same Month but in the previous Year.

 

prior-year-month-quantity =
VAR CurrentProduct = table_abc[ProductID]
VAR CurrentDate = table_abc[Year-Month]
VAR PriorYearDate =
    EOMONTH(CurrentDate, -12)
RETURN
CALCULATE(
    MAX(table_abc[Quantity]),
    FILTER(
        table_abc,
        table_abc[ProductID] = CurrentProduct &&
        table_abc[Year-Month] = PriorYearDate
    )
)

 

 

If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.


Best Regards,
Nasif Azam

AlienSx
Super User
Super User

let
    fx_prior_year = (txt) => ((split) => split{0} & "/" & Text.From(Number.From(split{1}) - 1))(Text.Split(txt, "/")),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    data_rec = Function.Invoke(
        Record.FromList, 
        List.Reverse(
            Table.ToColumns(
                Table.Group(Source, "Year-Month", {"x", (x) => Record.FromList(x[Quantity], x[ProductID])})
            )
        )
    ), 
    prior_year = Table.AddColumn(
        Source, 
        "prior_year", 
        (x) => Record.FieldOrDefault(
            Record.FieldOrDefault(data_rec, fx_prior_year(x[#"Year-Month"]), []),
            x[ProductID]), 
            null
    )
in
    prior_year
Cookistador
Memorable Member
Memorable Member

Hi @jerryr125 

 

I had à similar issue ar à customer, this is how I solved it 

 

1) transform your year month on sat/year/month,

 

2) create a column previous year 

Previousyear= Date.AddYears([#"Year-Month"], -1)

 

3) make a self right join of your table, for your right table select product id and date & for the left one, select productid and preciousyear (the column de added at the previous step)

 

4) expand the quantity from the merge table and renamme it 

 

And that's ho you can solve  this issue 🙂

SamsonTruong
Impactful Individual
Impactful Individual

Hi @jerryr125 ,

To do this in Power Query, please try the following code. Make sure to replace the Source with your actual table source:

let
    Source = PlaceYourSourceHere,
    SplitDate = Table.SplitColumn(Source, "Year-Month", Splitter.SplitTextByDelimiter("/", QuoteStyle.Csv), {"Month", "Year"}),
    ChangedTypes = Table.TransformColumnTypes(SplitDate, {{"Month", Int64.Type}, {"Year", Int64.Type}, {"Quantity", Int64.Type}}),
    AddDate = Table.AddColumn(ChangedTypes, "Date", each #date([Year], [Month], 1), type date),
    PreviousTable = Table.SelectColumns(AddDate, {"ProductID", "Date", "Quantity"}),
    RenamedPrev = Table.RenameColumns(PreviousTable, {{"Date", "PriorDate"}, {"Quantity", "PriorQuantity"}}),
    AddShiftedDate = Table.AddColumn(AddDate, "PriorDate", each Date.AddMonths([Date], -1), type date),
    MergedTables = Table.NestedJoin(AddShiftedDate, {"ProductID", "Date"}, RenamedPrev, {"ProductID", "PriorDate"}, "PriorData", JoinKind.LeftOuter),
    Expanded = Table.ExpandTableColumn(MergedTables, "PriorData", {"PriorQuantity"}, {"prior-year-month-month-quantity"})
in
    Expanded

 

If this helped, please mark it as the solution so others can benefit too. And if you found it useful, kudos are always appreciated.

Thanks,

Samson

 

Connect with me on LinkedIn

Check out my Blog

Going to the European Microsoft Fabric Community Conference? Check out my Session



Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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