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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

DAX dynamic rownumber and lag partition by

Hello, could you please help me with PBI DAX formulas:

 

I have such data which I pulled out from SQL database using rownumber() and lag() partition by function

 

ROW_NUMBER() OVER(PARTITION BY outlet, product ORDER BY outlet, product, date ASC) AS rownumber,
LAG(date, 1, 0) OVER(PARTITION BY outlet, product ORDER BY outletproductdate ASC) AS prevdate

 

periodoutletproductdateamountrownumberprevdatediff between dates
2018P082531900510000898/3/2018311/1/19000
2018P092531900510000898/17/20181028/3/201813
2018P092531900510000899/7/2018438/17/201822
2018P102531900510000899/28/2018549/7/201820

 

And now I want to use this data in Power BI, but rownumber and prevdate columns is static. When I use filters for example date > 8/3/2018 rownumber starts from 2. For example, after filtering I want table be like the following:

 

periodoutletproductdateamountrownumberprevdatediff between dates
2018P092531900510000898/17/20181011/1/19000
2018P092531900510000899/7/2018428/17/201822
2018P102531900510000899/28/2018539/7/201820

 

How to reach it using DAX in Power BI?

1 REPLY 1
v-lili6-msft
Community Support
Community Support

hi, @Anonymous

If you want to get a dynamic result, you need to create a measure.

For ROW_NUMBER() OVER(PARTITION BY outlet, product ORDER BY outlet, product, date ASC) AS rownumber

You could use RANKX Function to create a measure:

RANK = RANKX(FILTER(ALLSELECTED('Table'),'Table'[outlet]=SELECTEDVALUE('Table'[outlet])&&'Table'[product]=SELECTEDVALUE('Table'[product])),CALCULATE(SELECTEDVALUE('Table'[date])),,ASC)

But for LAG(date, 1, 0) OVER(PARTITION BY outlet, product ORDER BY outlet, product, date ASC) AS prevdate

there is no function can be used directly, and It is a little difficult to achieve. So let's have a try by the logic above it.

 

 

Best Regards,

Lin

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

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors