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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Unknown
Frequent Visitor

Calculate difference between rows by group

Hello,

 

I have a table with four columns: index, company, number, date (in the format DD/MM/YYYY).

The table looks like the one in the picture bellow.

I want to calculate the difference between the dates where the column number is not empty for each company.
I managed to create the column datediff (in yellow) with the following dax code:

     IFTable[number]<>"" && CALCULATEMAX(Table[date]), FILTER(Table, Table[company]=EARLIER(Table[company]) &&                                Table[number]<>"" && Table[date]<EARLIER(Table[date])))<> BLANK(),
        Table[date] - CALCULATE(MAX(Table[date]), FILTER(Jobs, Table[company]=EARLIER(Table[company]) &&                                                   Table[number]<>"" && Table[date]<EARLIER(Table[date]))))
 

However, I want to get the results in the column datediff_correct (in green). Which means, when for two consecutive indexes where number is not empty, if the date is the same it should return either blank or 0 (it will work the same for the problem).

Do you have any idea how to solve this? Thank you

 

Unknown_1-1657097747426.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Unknown ,

 

Thank you for the sample data, I created the same data as yours to test it, and finally succeeded in calculating the result you want to achieve. As the following shows, it is my testing result:

vmengzhumsft_0-1657699137527.png

 

The measure I use is as follows, you can try it:

_date =

if(SELECTEDVALUE('Table'[number])="", BLANK(), DATEDIFF(

CALCULATE(MAX('Table'[date]),FILTER(ALL('Table'),'Table'[company]=MAX('Table'[company])&&'Table'[Index]<MAX('Table'[Index])&&'Table'[number]<>"")),SELECTEDVALUE('Table'[date]),DAY)

)

Best regards,

Community Support Team Selina zhu

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

3 REPLIES 3
Anonymous
Not applicable

Hi @Unknown ,

 

Thank you for the sample data, I created the same data as yours to test it, and finally succeeded in calculating the result you want to achieve. As the following shows, it is my testing result:

vmengzhumsft_0-1657699137527.png

 

The measure I use is as follows, you can try it:

_date =

if(SELECTEDVALUE('Table'[number])="", BLANK(), DATEDIFF(

CALCULATE(MAX('Table'[date]),FILTER(ALL('Table'),'Table'[company]=MAX('Table'[company])&&'Table'[Index]<MAX('Table'[Index])&&'Table'[number]<>"")),SELECTEDVALUE('Table'[date]),DAY)

)

Best regards,

Community Support Team Selina zhu

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

amitchandak
Super User
Super User

@Unknown , In place of date use the index in your formula

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

I've tried that, but for some reason it returns this message: "There's not enough memory to complete this operation. Please try again later when there may be more memory available."
I've tried calculating in different occasions but it always returns the same message

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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