Forum Discussion

Juarente's avatar
Juarente
Regular Visitor
4 years ago
Solved

Help getting units sold per day from cummulative units sold.

Hello there,

 

My data looks like this:

 

Product_IDDateCummulative_Sold_UnitsPrevious_Date
8906722-may-2211021-may-22
8656722-may-2212321-may-22
4567822-may-227821-may-22
8906723-may-2211522-may-22
8656723-may-2212922-may-22
4567823-may-228322-may-22
8906724-may-2211523-may-22
8656724-may-2212923-may-22
4567824-may-228323-may-22

 

As you can see, I have dayly data for each product sales in units, however, those are cummulative units, meaning the qty of units sold since the product begun selling. 

 

I added a new column (Prevous Date) in the hope that I could get de cummulative qty of units sold the day before, then substract both quantyties and get something like this:

 

Product_IDDateCummulative_Sold_UnitsPrevious_DateCumm_on_Previous_DateUnits_Sold
8906722-may-2211021-may-22nullnull
8656722-may-2212321-may-22nullnull
4567822-may-227821-may-22nullnull
8906723-may-2211522-may-221105
8656723-may-2212922-may-221236
4567823-may-228922-may-227811
8906724-may-2212023-may-221155
8656724-may-2212923-may-221290
4567824-may-229723-may-22898

 

I haven't been able to acomplish this with a DAX formula for a new column in Power Query. Hope someone can help me here. Thanks.

 

Regards,

 

Juan

  • Juarente ,

     

    Another way to sort out the problem

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5RCsAgCIDhu/hcUGalZwkfOsAOsNtPgoE12Jv44Y9jAEtqHQIgxmveEdHmnBNoMGv1a1iWkREfZot19iaLT9Y96Q1lTzpj2ZPkz44v6SfpTDqoPg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product_ID = _t, Date = _t, Cummulative_Sold_Units = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Cummulative_Sold_Units", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Product_ID"}, {{"Row", each _, type table [Product_ID=nullable text, Date=nullable date, Cummulative_Sold_Units=nullable number]}}),
        #"Sort by date" = Table.TransformColumns( #"Grouped Rows",{ "Row", each Table.Sort(_, {{"Date", Order.Ascending}})} ),
        #"Added Index" = Table.TransformColumns( #"Sort by date",{ "Row", each Table.AddIndexColumn(_, "Index",0)} ),
        #"Added Day quantity" = Table.AddColumn(#"Added Index", "Custom", 
            each let
            Group = [Row],
            DayQty = 
            Table.AddColumn(
                Group, "Qty",
                each try Group[Cummulative_Sold_Units]{[Index]} - Group[Cummulative_Sold_Units]{[Index]-1} otherwise 0
            )
        in
            DayQty),
        Expand = Table.Combine( #"Added Day quantity"[Custom], {"Product_ID", "Date", "Cummulative_Sold_Units","Qty"}),
        #"Changed Type1" = Table.TransformColumnTypes(Expand,{{"Product_ID", Int64.Type}, {"Date", type date}, {"Qty", Int64.Type}, {"Cummulative_Sold_Units", Int64.Type}}),
        #"Sorted Rows1" = Table.Sort(#"Changed Type1",{{"Date", Order.Ascending}, {"Product_ID", Order.Descending}})
    in
        #"Sorted Rows1"

2 Replies

  • latimeria's avatar
    latimeria
    Icon for Solution Specialist rankSolution Specialist

    Juarente ,

     

    Another way to sort out the problem

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5RCsAgCIDhu/hcUGalZwkfOsAOsNtPgoE12Jv44Y9jAEtqHQIgxmveEdHmnBNoMGv1a1iWkREfZot19iaLT9Y96Q1lTzpj2ZPkz44v6SfpTDqoPg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product_ID = _t, Date = _t, Cummulative_Sold_Units = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Cummulative_Sold_Units", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Product_ID"}, {{"Row", each _, type table [Product_ID=nullable text, Date=nullable date, Cummulative_Sold_Units=nullable number]}}),
        #"Sort by date" = Table.TransformColumns( #"Grouped Rows",{ "Row", each Table.Sort(_, {{"Date", Order.Ascending}})} ),
        #"Added Index" = Table.TransformColumns( #"Sort by date",{ "Row", each Table.AddIndexColumn(_, "Index",0)} ),
        #"Added Day quantity" = Table.AddColumn(#"Added Index", "Custom", 
            each let
            Group = [Row],
            DayQty = 
            Table.AddColumn(
                Group, "Qty",
                each try Group[Cummulative_Sold_Units]{[Index]} - Group[Cummulative_Sold_Units]{[Index]-1} otherwise 0
            )
        in
            DayQty),
        Expand = Table.Combine( #"Added Day quantity"[Custom], {"Product_ID", "Date", "Cummulative_Sold_Units","Qty"}),
        #"Changed Type1" = Table.TransformColumnTypes(Expand,{{"Product_ID", Int64.Type}, {"Date", type date}, {"Qty", Int64.Type}, {"Cummulative_Sold_Units", Int64.Type}}),
        #"Sorted Rows1" = Table.Sort(#"Changed Type1",{{"Date", Order.Ascending}, {"Product_ID", Order.Descending}})
    in
        #"Sorted Rows1"
  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Icon for Most Valuable Professional rankMost Valuable Professional

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5RCsAgCIDhu/hcUGalh9gJwoeOsdtPgoE12Jv44Y9jAEtqHQIgxmveEdHmnBNoMGv1a1iWkREfZot19iaLT9Y96Q1lTzpj2ZPkz44v6SfpTDqoPg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product_ID = _t, Date = _t, Cummulative_Sold_Units = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product_ID", Int64.Type}, {"Date", type date}, {"Cummulative_Sold_Units", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Previous Date", each Date.AddDays([Date],-1)),
        #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Product_ID", "Previous Date"}, #"Added Custom", {"Product_ID", "Date"}, "Added Custom", JoinKind.LeftOuter),
        #"Added Custom1" = Table.AddColumn(#"Merged Queries", "Previous **bleep** Units Sold", each try [Added Custom]{0}[Cummulative_Sold_Units] otherwise null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Added Custom"}),
        #"Added Custom2" = Table.AddColumn(#"Removed Columns", "Units_Sold", each [Cummulative_Sold_Units]-[Previous **bleep** Units Sold])
    in
        #"Added Custom2"