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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
aeriksen
Frequent Visitor

Fetch previous and next row based on column value

Hi

I have a table where I want to create two new columns with the time stamp of the next and previous status change of fk_order_id. It's easily solvable in DAX, but I would like to do this in M to use the data ready to go in a dataflow.

 

I have solved this in M also, but the solutions I've come up with so far are not optimal as there are 100000+ rows and daily refreshes.

Solutions so far in M:
Sorting on Order ID and time stamp and adding two index columns starting with 0 and 1. Merging based on index columns and using conditinal column to fetch previous row if fk_order_id are the same. 

Merging the status change ID with itself, expanding the table keeping only the previous status change ID, then mergin the previous status change ID with the table again to fetch names, time stamps etc. 

I figure there must be an easier way to do this, an equivalent to the EALIER-function in DAX?

DAX expression for fetching next status change:
MINX(
FILTER('FACT Statusendringer',
'FACT Statusendringer'[fk_order_id] = EARLIER('FACT Statusendringer'[fk_order_id])
&& 'FACT Statusendringer'[created] > EARLIER('FACT Statusendringer'[created])),
'FACT Statusendringer'[created])

DAX expression for fetching previous status change:
MAXX(
FILTER('FACT Statusendringer',
'FACT Statusendringer'[fk_order_id] = EARLIER('FACT Statusendringer'[fk_order_id])
&& 'FACT Statusendringer'[created] < EARLIER('FACT Statusendringer'[created])),
'FACT Statusendringer'[created])

Here is an example of the data:
Skjermbilde 2021-07-09 kl. 13.58.53.png

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @aeriksen ,

 

According to your DAX, I think you want to group by the ID column to get the value of the previous or next row. The following is the method in Power Query.

1.Refer to this article to get a ranking by category. And copy the original table to get the second table, the first table sorting starts from 2, and the second table sorting starts from 1.

1.png2.png

 

 

 

2.Then merge two tables.

3.png

 

 

3.Expand the table

4.png

 

What the example shows is to get the next row. If you want to get the previous row, just change the rank order.

You can check more details from the attachment.

 

 

Best Regards,

Stephen Tao

 

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

View solution in original post

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @aeriksen ,

 

According to your DAX, I think you want to group by the ID column to get the value of the previous or next row. The following is the method in Power Query.

1.Refer to this article to get a ranking by category. And copy the original table to get the second table, the first table sorting starts from 2, and the second table sorting starts from 1.

1.png2.png

 

 

 

2.Then merge two tables.

3.png

 

 

3.Expand the table

4.png

 

What the example shows is to get the next row. If you want to get the previous row, just change the rank order.

You can check more details from the attachment.

 

 

Best Regards,

Stephen Tao

 

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

This is really great! Thank you! 🙂 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors