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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
villa1980
Resolver I
Resolver I

Replicating Power Query Formula in DAX

Simple one I imagine.
I have the seen the Power Query formula which will help with my issue I have with rturning previous and next row of data.

Problem is when doing this in Power Query it slows down the refresh down to a crawl.

 Previous Row formula = Name {Index -1}

Name     Index    Previous Row Formula 

Jeff            1          0

Bob           2          Jeff

Ian             3         Bob
Just trying to replicate that in DAX
Then I can use another formula to find the next row and hopefullly go from there.

 

1 ACCEPTED SOLUTION
anmolmalviya05
Solution Sage
Solution Sage

Hi @villa1980,

To replicate the previous and next row functionality in DAX, you can use DAX functions like EARLIER, and FILTER to create calculated columns for both the previous and next rows.

Here’s how you can do it in DAX:

Step 1: Create a Calculated Column for the Previous Row

This column will get the name of the previous row based on the Index column.

 

PreviousRow = VAR CurrentIndex = YourTable[Index] RETURN IF (     CurrentIndex = 1,     BLANK(),     CALCULATE(         MAX(YourTable[Name]),         FILTER(             YourTable,             YourTable[Index] = CurrentIndex - 1         )     ) )

 

Step 2: Create a Calculated Column for the Next Row

Similarly, you can create a calculated column to get the name of the next row.

 

NextRow = VAR CurrentIndex = YourTable[Index] RETURN CALCULATE(     MAX(YourTable[Name]),     FILTER(         YourTable,         YourTable[Index] = CurrentIndex + 1     ) )

 

Example Output

With this setup, your table might look like this:

 

Name Index PreviousRow NextRow

Jeff1BLANK()Bob
Bob2JeffIan
Ian3BobBLANK()

 



View solution in original post

9 REPLIES 9
villa1980
Resolver I
Resolver I

Sorry all, was being too complicated again. Didn't need anything else, just needed the end time against the next row and have what I need.

Thank-you again.

 

villa1980
Resolver I
Resolver I

you guys amaze me honestly wow....let's see if you can solve this issue then using a similar line of thinking 🙂

So this is the table I have below, what I would like is do find the time difference between the End Time and the next row start time (which us where asking for Previous row and next row DAX).

This needs to be for the Appointment Owner, BAYID and StartDate

villa1980_0-1731070520777.png

 

FreemanZ
Super User
Super User

hi @villa1980 ,

 

try like:

column = 

VAR _result =

MAXX(

    FILTER(

        data, 

        data[index]=EARLIER(data[index]) - 1

    ),

    data[Name]

 )

RETURN 

IF(_result=BLANK(), 0, _result)

quantumudit
Skilled Sharer
Skilled Sharer

Hello @villa1980 

You can use the following formula to create a new calculated column in the table to get the previous row value:

Previous Name = 
VAR _idx = NamesTbl[Index]
RETURN
    CALCULATE (
        SELECTEDVALUE ( NamesTbl[Names] ),
        FILTER (
            ALL ( NamesTbl ),
            NamesTbl[Index] = _idx - 1
        )
    )

Here is the screenshot of the same:

quantumudit_0-1731068554145.png

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

 

anmolmalviya05
Solution Sage
Solution Sage

Hi @villa1980,

To replicate the previous and next row functionality in DAX, you can use DAX functions like EARLIER, and FILTER to create calculated columns for both the previous and next rows.

Here’s how you can do it in DAX:

Step 1: Create a Calculated Column for the Previous Row

This column will get the name of the previous row based on the Index column.

 

PreviousRow = VAR CurrentIndex = YourTable[Index] RETURN IF (     CurrentIndex = 1,     BLANK(),     CALCULATE(         MAX(YourTable[Name]),         FILTER(             YourTable,             YourTable[Index] = CurrentIndex - 1         )     ) )

 

Step 2: Create a Calculated Column for the Next Row

Similarly, you can create a calculated column to get the name of the next row.

 

NextRow = VAR CurrentIndex = YourTable[Index] RETURN CALCULATE(     MAX(YourTable[Name]),     FILTER(         YourTable,         YourTable[Index] = CurrentIndex + 1     ) )

 

Example Output

With this setup, your table might look like this:

 

Name Index PreviousRow NextRow

Jeff1BLANK()Bob
Bob2JeffIan
Ian3BobBLANK()

 



danextian
Super User
Super User

Hi @villa1980 

 

Try this:

Prev Row = 
IF (
    'Table'[Index] = 1,
    "0",
    CALCULATE (
        MAX ( 'Table'[Name] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 )
    )
)

 

danextian_0-1731067938658.png

DAX is more  optimized at scanning a table that Power Query but this will still depend on the table size. If your applying this on a very large table, this can be very slow as well. If so, I would use a combination of Power Query and DAX with two tables. The first table with index. The second with with index + or -1  (depending on which table does the lookup) and a LOOKUPVALUE or RELATED column.

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Blimey, that was quick and worked really well, thank-you.

I have now tried this with end time, however, the outcome when looking at time comes back as
0.56,0.60
rather than 13:30:00, 14:30:00
I think a format would have to be placed in the filter, but not sure where I place it

 

The newly created column is type number.  You can just change the data type to time as adding a FORMAT formula in the calc coumn returns a text. Take note, time  format cannot be 24 hours and more  which means 1.5 will be returned as 12pm and not one day and 12 hours.

danextian_0-1731069575295.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

If this solves, please mark this as the solution.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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