Forum Discussion

Joey2022's avatar
Joey2022
Regular Visitor
4 years ago
Solved

Calculated column to show difference value btw rows

Hi there! 

I need a favor from you guys, experts. I stucked in the matter for weeks. Pls kindly help me out.

The matter is:

I want to find the difference value of sale btw 2 days as:

DateSaleAmountDifferenceSA
D1Sum1 
D2Sum2Sum2-Sum1
D3Sum3Sum3-Sum2

 

The input Data looked like:

DateSaleAmount
D1S1
D1S2
D2S3
D2S4
D2S5
D3S6

 

Joey

  • Hi,

    Try this

    1. Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of the Calendar Table
    2. To your visual, drag Date from the Calendar Table.
    3. Write these measures:

    Sales = sum(Data[SaleAmount])

    Previous day sales = calculate([Sales],previousday(Calendar[Date]))

    Delta = [Sales]-[Previous day sales]

    Hope this helps.

9 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjFU0lEyNDBQitWBcoxMoRwjkIwxEsfIAJkDU2YM5JiAZGIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, SaleAmount = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}, {"SaleAmount", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Date"}, {{"SaleAmount", each List.Sum([SaleAmount]), type nullable text}}),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "DifferenceSA", each try [SaleAmount]-#"Added Index"[SaleAmount]{[Index]-1} otherwise null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
    in
        #"Removed Columns"

     

    • Joey2022's avatar
      Joey2022
      Regular Visitor

      Thank you Verma!

      Got your idea with power querry. But seem my data set is quite large for the group row. I would prefer some solution go with DAX

       

      Joey

  • Hi,

    Try this

    1. Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of the Calendar Table
    2. To your visual, drag Date from the Calendar Table.
    3. Write these measures:

    Sales = sum(Data[SaleAmount])

    Previous day sales = calculate([Sales],previousday(Calendar[Date]))

    Delta = [Sales]-[Previous day sales]

    Hope this helps.

    • Joey2022's avatar
      Joey2022
      Regular Visitor

      Thank you Ashish Mathur.

      It really worked the way I expect. I am really appreciate your guide. Very gentle help.

       

      Joey

    • Joey2022's avatar
      Joey2022
      Regular Visitor

      hi Ashish_Mathur,

       

      I still have a issue regarding to the day without data e.g Sunday. I want to find the different btw Monday and last Saturday. So the "previousday" don't work properly in the case. 

      Thank you

      Joey