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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Simplyjustdavid
New Member

match column and subtract value from previous day

Hello all,

 

Kinda been racking my brain and my google search with this one.

We have a csv datadump into power query, each row in that dump has date/unit #/ accumulated total. 

Each day sales accumulated upward:

  Date             Loc       Unit#     NA             NA            NA                   Sales

6/28/2021551109763020005230
6/29/2021551109763020005295
2/5/2022551109763030003790
2/6/2022551109763030003790

 

is theres a way to create a custom column that match the unit # (so that it doesnt subtract once the data goes to a new unit) and substract the previous day sales?

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Simplyjustdavid ,

 

Sort your query in PQ by [Unit#] ascending, then by [Date] ascending.

Add an index column that starts from 0 and call it "index0".

Add another index column that starts from 1 and call it "index1".

Go to the Home tab > Merge Queries and left outer merge the table on itself on [Unit#][index0] (top window) = [Unit#][index1] (bottom window).

Expand the sales column from the resulting nested table column and, voila, previous days' sales.

 

Paste this over the default code in Advanced Editor to see the steps in action:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ1MNM1slDSUTI1NTQ0sAQyzM2MDYxAAkbGBkqxOkjKLLErszSFKTPSNQAiU3RlxkDa2NzSAFWZGW5lsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Loc = _t, Unit = _t, Sales = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Loc", Int64.Type}, {"Unit", Int64.Type}, {"Sales", Int64.Type}}),
    sortUnitDate = Table.Sort(chgTypes,{{"Unit", Order.Ascending}, {"Date", Order.Ascending}}),
    addIndex0 = Table.AddIndexColumn(sortUnitDate, "Index0", 0, 1, Int64.Type),
    addIndex1 = Table.AddIndexColumn(addIndex0, "Index1", 1, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex1, {"Unit", "Index0"}, addIndex1, {"Unit", "Index1"}, "addIndex1", JoinKind.LeftOuter),
    expandSalesCol = Table.ExpandTableColumn(mergeOnSelf, "addIndex1", {"Sales"}, {"prevSales"})
in
    expandSalesCol

 

 

BA_Pete_0-1655375835725.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

1 REPLY 1
BA_Pete
Super User
Super User

Hi @Simplyjustdavid ,

 

Sort your query in PQ by [Unit#] ascending, then by [Date] ascending.

Add an index column that starts from 0 and call it "index0".

Add another index column that starts from 1 and call it "index1".

Go to the Home tab > Merge Queries and left outer merge the table on itself on [Unit#][index0] (top window) = [Unit#][index1] (bottom window).

Expand the sales column from the resulting nested table column and, voila, previous days' sales.

 

Paste this over the default code in Advanced Editor to see the steps in action:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ1MNM1slDSUTI1NTQ0sAQyzM2MDYxAAkbGBkqxOkjKLLErszSFKTPSNQAiU3RlxkDa2NzSAFWZGW5lsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Loc = _t, Unit = _t, Sales = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Loc", Int64.Type}, {"Unit", Int64.Type}, {"Sales", Int64.Type}}),
    sortUnitDate = Table.Sort(chgTypes,{{"Unit", Order.Ascending}, {"Date", Order.Ascending}}),
    addIndex0 = Table.AddIndexColumn(sortUnitDate, "Index0", 0, 1, Int64.Type),
    addIndex1 = Table.AddIndexColumn(addIndex0, "Index1", 1, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex1, {"Unit", "Index0"}, addIndex1, {"Unit", "Index1"}, "addIndex1", JoinKind.LeftOuter),
    expandSalesCol = Table.ExpandTableColumn(mergeOnSelf, "addIndex1", {"Sales"}, {"prevSales"})
in
    expandSalesCol

 

 

BA_Pete_0-1655375835725.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors