cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Frequent Visitor

## 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 Time Sales Sales-1 Sales-2 Sales-3 Sales-4 Sales-5 20190101 5000 20190102 4100 5000 20190103 3400 4100 5000 20190104 2300 3400 4100 5000 20190105 8800 2300 3400 4100 5000 20190106 2500 8800 2300 3400 4100 5000
1 ACCEPTED SOLUTION
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.

8 REPLIES 8
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.

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.

Community Support

Hi @Aj_BI ,

All the three expressions above are Measure.

And, what's this?

Best Regards,

Icey

Frequent Visitor

Hi @Icey , Thanks for the follow up, even I was not sure why that error was coming so I deleted and re cretaed a fresh report. Now I got the RANK working but when I try to get the Previous Sale, it's blank. I tried to use ALLSELECTED in FILTER and started getting only 1 record with rest as blank.

Super User

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

Frequent Visitor

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

Super User

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

Frequent Visitor

Thanks so much AIB for your time and help. Looks like things are not in my favor as I mentioned the source of the data is a Direct Query and when I tried creating a computed column, it gives me another error for COMPUTE not working:

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors