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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
FPP
Helper I
Helper I

Subtract previous days using M-Query

Hi, I have a table that contains (Date, Category and Sales)

 

DateCategorySales
2024-01-01A300
2024-01-01B400
2024-01-02A1000
2024-01-02B500
2024-01-03A1600
2024-01-03B800

 

In the above Table (within a category)
1) the Jan 2 Sales numbers are Jan 1 and Jan 2 added together.

2) the Jan 3 Sales numbers are Jan 1, Jan 2, Jan 3 added together.


I want a table that has the Actual Sales

DateCategorySalesActual Sales
2024-01-01A300300
2024-01-01B400400
2024-01-02A1000700
2024-01-02B500100
2024-01-03A1600600
2024-01-03B800300
1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

Hi @FPP 
use this formula

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MAQiJR0lRyA2NjBQitVBk3ACYhN0CSOoDkMDbDIgLaboEsYwLWbYZEBaLEASsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Category = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Category", type text}, {"Sales", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [Sales]-(List.Sum(Table.SelectRows(#"Changed Type", (x)=>x[Date]<[Date] and x[Category]=[Category])[Sales])??0))
in
    #"Added Custom"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

6 REPLIES 6
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1737341037429.png

 

v-saisrao-msft
Community Support
Community Support

Hi @FPP 

May I ask if you have resolved this issue? If so, please mark their reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

Thank you.

v-saisrao-msft
Community Support
Community Support

Hi @FPP,

Could you please confirm if your query have been resolved the solution provided by @Omid_Motamedise & @p45cal ? If they have, kindly mark the helpful response and accept it as the solution. This will assist other community members in resolving similar issues more efficiently.

Thank you

Omid_Motamedise
Super User
Super User

Hi @FPP 
use this formula

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MAQiJR0lRyA2NjBQitVBk3ACYhN0CSOoDkMDbDIgLaboEsYwLWbYZEBaLEASsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Category = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Category", type text}, {"Sales", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [Sales]-(List.Sum(Table.SelectRows(#"Changed Type", (x)=>x[Date]<[Date] and x[Category]=[Category])[Sales])??0))
in
    #"Added Custom"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
slorin
Super User
Super User

Hi @FPP

 

let
Source = Your_Source,
Sort = Table.Sort(Source,{{"Category", Order.Ascending}, {"Date", Order.Ascending}}),
Group = Table.Group(Sort, {"Category"},
{{"Data",
each Table.FromColumns(
{[Date],
[Sales],
List.Transform(List.Zip({[Sales], {0} & List.RemoveLastN([Sales],1)}), each _{0}-_{1})},
{"Date", "Sales", "Actual Sales"}),
type table [Date=nullable date, Sales=nullable number, Actual Sales=nullable number]}}),
Expand = Table.ExpandTableColumn(Group, "Data", {"Date", "Sales", "Actual Sales"}, {"Date", "Sales", "Actual Sales"})
in
Expand 

 Stéphane

p45cal
Super User
Super User

(In haste) not especially efficient:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMTJR0lRyA2NjBQitVBk3ACYhOYhBGaDkMDbDIgLaYwCWN0LWbYZEBaLEASsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Category = _t, Sales = _t]),
    fnMonthlyActualSales = (tbl)=>
    let
        SortedRows = Table.Sort(tbl,{{"Date", Order.Ascending}}),
        AddedIndex = Table.AddIndexColumn(SortedRows, "Index.1", -1, 1, Int64.Type),
        AddedCustom = Table.AddColumn(AddedIndex, "Actual Sales", each [Sales] - (if [Index.1]<0 then 0 else AddedIndex[Sales]{[Index.1]})),
        RemovedOtherColumns = Table.SelectColumns(AddedCustom,{"Date", "Category", "Sales", "Actual Sales", "Index"})
    in
        RemovedOtherColumns,
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Category", type text}, {"Sales", type number}}),
    AddedIndex = Table.AddIndexColumn(ChangedType, "Index", 0, 1, Int64.Type),
    InsertedEOM = Table.AddColumn(AddedIndex, "End of Month", each Date.EndOfMonth([Date]), type date),
    GroupedRows = Table.Group(InsertedEOM, {"Category", "End of Month"}, {{"grp", each _, type table [Date=nullable date, Category=text, Sales=number, End of Month=date]}}),
    InvokedCustomFunction = Table.AddColumn(GroupedRows, "MonthlyActualSales", each fnMonthlyActualSales([grp])),
    RemovedColumns = Table.RemoveColumns(InvokedCustomFunction,{"grp", "End of Month"}),
    ExpandedMonthlyActualSales = Table.ExpandTableColumn(RemovedColumns, "MonthlyActualSales", {"Date", "Sales", "Actual Sales", "Index"}, {"Date", "Sales", "Actual Sales", "Index"}),
    SortedRows = Table.Sort(ExpandedMonthlyActualSales,{{"Index", Order.Ascending}}),
    ReorderedColumns = Table.ReorderColumns(SortedRows,{"Date", "Category", "Sales", "Actual Sales", "Index"}),
    RemovedColumns1 = Table.RemoveColumns(ReorderedColumns,{"Index"}),
    ChangedType1 = Table.TransformColumnTypes(RemovedColumns1,{{"Date", type date}})
in
    ChangedType1

It treats the data on a monthly basis, so assumes first sale per month is not a cumulative figure.

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors