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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
Anonymous
Not applicable

For Each Formula - Calculated Column

Hi

 

I am trying to count the days between two stages for each MR No. What is the best way to do this in Power BI? In excel I would use =IF(B3=B2, (DAYS(A3,A2)),(0)).

Many thanks!For Each.JPG

 

 

2 ACCEPTED SOLUTIONS

Hi @Anonymous

 

How about this one?

 

Date_Diff =
VAR NextAuditDate =
    CALCULATE (
        VALUES ( TableName[Audit Date] ),
        FILTER (
            ALL ( TableName ),
            TableName[MR No.] = EARLIER ( TableName[MR No.] )
                && TableName[Date RANK]
                    = EARLIER ( TableName[Date RANK] ) + 1
        )
    )
RETURN
    DATEDIFF ( TableName[Audit Date], NextAuditDate, DAY )

Photo1.png

View solution in original post

Ashish_Mathur
Super User
Super User

Hi @Anonymous,

 

This calculated column formula works

 

=if(ISBLANK(CALCULATE(MIN(Data[Audit Date]),FILTER(Data,Data[MR No.]=EARLIER(Data[MR No.])&&Data[Audit Date]>EARLIER(Data[Audit Date])))),BLANK(),CALCULATE(MIN(Data[Audit Date]),FILTER(Data,Data[MR No.]=EARLIER(Data[MR No.])&&Data[Audit Date]>EARLIER(Data[Audit Date])))-[Audit Date])

 

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi @Anonymous,

 

This calculated column formula works

 

=if(ISBLANK(CALCULATE(MIN(Data[Audit Date]),FILTER(Data,Data[MR No.]=EARLIER(Data[MR No.])&&Data[Audit Date]>EARLIER(Data[Audit Date])))),BLANK(),CALCULATE(MIN(Data[Audit Date]),FILTER(Data,Data[MR No.]=EARLIER(Data[MR No.])&&Data[Audit Date]>EARLIER(Data[Audit Date])))-[Audit Date])

 

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Zubair_Muhammad
Community Champion
Community Champion

Hi @Anonymous

 

Try this solution

 

First add a calculated column "DATE RANK"

Date RANK=
RANKX (
    FILTER ( TableName, TableName[MR No.] = EARLIER ( TableName[MR No.] ) ),
    TableName[Audit Date],
    TableName[Audit Date],
    ASC,
    DENSE
)

 

 

Then you can compute the Days Difference between each successive audit dates for each MR No using this formula

 

Date Difference=
VAR PreviousAuditDate =
    CALCULATE (
        VALUES ( TableName[Audit Date] ),
        FILTER (
            ALL ( TableName ),
            TableName[MR No.] = EARLIER ( TableName[MR No.] )
                && TableName[Date RANK]
                    = EARLIER ( TableName[Date RANK] ) - 1
        )
    )
RETURN
    DATEDIFF ( PreviousAuditDate, TableName[Audit Date], DAY )

 

 

DateDiff.png

 

Anonymous
Not applicable

Hello and many thanks for this :)! Just one more thing - I need to show the "date difference" count on the preceeding audit date to show how long each stage took before it moved to the next one. So, the 5 days difference on line 2 should move to line 1 and so on.  

 

Thank you for your help.

Hi @Anonymous

 

How about this one?

 

Date_Diff =
VAR NextAuditDate =
    CALCULATE (
        VALUES ( TableName[Audit Date] ),
        FILTER (
            ALL ( TableName ),
            TableName[MR No.] = EARLIER ( TableName[MR No.] )
                && TableName[Date RANK]
                    = EARLIER ( TableName[Date RANK] ) + 1
        )
    )
RETURN
    DATEDIFF ( TableName[Audit Date], NextAuditDate, DAY )

Photo1.png

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.