Forum Discussion

Aj_BI's avatar
Aj_BI
Frequent Visitor
6 years ago
Solved

DAX Query to Find Next Value

Hi All,

I am new in PowerBI and need your help!

I have a table like below having the Date Time Stamp as first column with Sales in second column. What I need is a formula to compute a third column(Sales-1) which can give me values from Sales column but first record as blank and 2nd record should show 1st record of Sales column. Similarly, I need another column as Sales-2 and so on. 

Point to be noted: I am using Direct Query so I can not use any Index column from M Query/Query Editor Page. Thanks in advance for your help.

Date TimeSalesSales-1Sales-2Sales-3Sales-4Sales-5
201901015000     
2019010241005000    
20190103340041005000   
201901042300340041005000  
2019010588002300340041005000 
20190106250088002300340041005000
  • Hi Aj_BI ,

    Try this:

    Rank =
    RANKX (
        ALL ( TableTest ),
        CALCULATE ( MAX ( TableTest[Date_Time] ) ),
        ,
        ASC,
        DENSE
    )
    Sales-1 =
    VAR index = [Rank] - 1
    RETURN
        CALCULATE (
            SUM ( TableTest[Sales] ),
            FILTER ( ALL ( TableTest ), index = [Rank] )
        )
    Sales-2 =
    VAR index = [Rank] - 2
    RETURN
        CALCULATE (
            SUM ( TableTest[Sales] ),
            FILTER ( ALL ( TableTest ), index = [Rank] )
        )

     

     

    Best Regards,
    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

8 Replies

  • Icey's avatar
    Icey
    Community Support

    Hi Aj_BI ,

    Try this:

    Rank =
    RANKX (
        ALL ( TableTest ),
        CALCULATE ( MAX ( TableTest[Date_Time] ) ),
        ,
        ASC,
        DENSE
    )
    Sales-1 =
    VAR index = [Rank] - 1
    RETURN
        CALCULATE (
            SUM ( TableTest[Sales] ),
            FILTER ( ALL ( TableTest ), index = [Rank] )
        )
    Sales-2 =
    VAR index = [Rank] - 2
    RETURN
        CALCULATE (
            SUM ( TableTest[Sales] ),
            FILTER ( ALL ( TableTest ), index = [Rank] )
        )

     

     

    Best Regards,
    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Aj_BI's avatar
      Aj_BI
      Frequent Visitor

      Hi Icey Icey and AlB AIB, first thanks a lot for your help and time. I really appreciate that.

      @Icey: you solution looks very precise and easy to undertand but somehow, I am not able to get the RANK. I also tried to add an index column from Query Editor but not workign for me due to Direct Query. 

      When I try to use CALCULATE to get the RANK in a measure(I hope you are too creating a measure as RANK, and I can not use calcualted column due to error to use CALCULATE), I get weired error. Next I tried applying some work arounds but those are giving me same RANK as 1. Not sure what is different in my real data.

      • Icey's avatar
        Icey
        Community Support

        Hi Aj_BI ,

        All the three expressions above are Measure.

        And, what's this?

         

         

        Best Regards,

        Icey

  • AlB's avatar
    AlB
    Community Champion

    Hi Aj_BI 

    Try this for Sales-1

    Sales-1 =
    VAR PreviousDate_ =
        CALCULATE (
            MAX ( Table1[Date] ),
            Table1[Date] < EARLIER ( Table1[Date] ),
            ALL ( Table1 )
        )
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Sales] ),
            Table1[Date] = PreviousDate_,
            ALL ( Table1 )
        )

    For the others you can follow exactly the same pattern, using the previous column. For instance, for Sales-2 you'd use Sales-1 in the second CALCULATE, and so on:

    Sales-2 =
    VAR PreviousDate_ =
        CALCULATE (
            MAX ( Table1[Date] ),
            Table1[Date] < EARLIER ( Table1[Date] ),
            ALL ( Table1 )
        )
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Sales-1] ),
            Table1[Date] = PreviousDate_,
            ALL ( Table1 )
        )

     

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Cheers  Datanaut

    • Aj_BI's avatar
      Aj_BI
      Frequent Visitor

      Thank you so much AIB for your help. I tried the same but got the error at EARLIER function.

       

      Please advice.

       
      • AlB's avatar
        AlB
        Community Champion

        Hi Aj_BI 

        The code above is for creating calculated columns in your original table. It looks like you are using the code in a visual. You can create the calculated columns and then create a table visual simply showing the columns (all in Don't summarize). See it all at work in the attached file.

        Please mark the question solved when done and consider giving kudos if posts are helpful.

        Cheers Datanaut