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