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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Johannes_W
Regular Visitor

AVERAGEX over datediff events in a single collumn

Hi all!

 

I am struggeling to define a measure that calculates the average time passed between several events. I am working with averagex and datediff but somehow end up nowhere.

The data are structured as such

 

Productevent_idevent_start
A101/01/2020
A204/01/2020
A306/01/2020
B101/01/2020
B204/01/2020
C115/02/2020
C317/02/2020
D104/01/2020
D215/02/2020
D317/02/2020
D416/04/2020

 

Now the goal is to find out the average datediff in days that passes between event ID 1 and event ID 2.

The problem is, not all products necessarily see event 2, some jump to event 3 directly.

 

My expected result for this measure would be 16 days.

Product A and B have seen 3 days jumping from 1 to 2, D has seen 42 days, so on average 16.

 

Thanks for any input!

jo

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi Jo,

 

Here is how I would write such a measure

 

Average Days Event1-Event2 = 
AVERAGEX ( 
    VALUES ( Events[Product] ),
    VAR DateEvent1 = 
        CALCULATE ( MAX ( Events[event_start] ), Events[event_id] = 1 )
    VAR DateEvent2 = 
        CALCULATE ( MIN ( Events[event_start] ), Events[event_id] = 2 )
    RETURN
        IF (
            NOT ISBLANK ( DateEvent1 ) && NOT ISBLANK ( DateEvent2 ),
            DateEvent2 - DateEvent1
            -- Blank result returned if either Event 1 or 2 missing
            -- Blanks are ignored by AVERAGEX
        )
)

 

 

This measure iterates over the values of Product, and for each calculates the difference between Event 2 date and Event 1 date, averaging the result.

 

The MAX & MIN are arbitrary, as I'm assuming that event_id values are not repeated for a given Product.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

4 REPLIES 4
OwenAuger
Super User
Super User

Hi Jo,

 

Here is how I would write such a measure

 

Average Days Event1-Event2 = 
AVERAGEX ( 
    VALUES ( Events[Product] ),
    VAR DateEvent1 = 
        CALCULATE ( MAX ( Events[event_start] ), Events[event_id] = 1 )
    VAR DateEvent2 = 
        CALCULATE ( MIN ( Events[event_start] ), Events[event_id] = 2 )
    RETURN
        IF (
            NOT ISBLANK ( DateEvent1 ) && NOT ISBLANK ( DateEvent2 ),
            DateEvent2 - DateEvent1
            -- Blank result returned if either Event 1 or 2 missing
            -- Blanks are ignored by AVERAGEX
        )
)

 

 

This measure iterates over the values of Product, and for each calculates the difference between Event 2 date and Event 1 date, averaging the result.

 

The MAX & MIN are arbitrary, as I'm assuming that event_id values are not repeated for a given Product.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi Owen!

Thanks for this, it works!

Just a few clarifications:

I have not mentioned this but yes, event_id can repeat, so a product can fall back to status 2 after having reached status 3 for example.

However, I am only interested in the extremes, so earliest date of ID 1 and latest date of ID 2, so MIN MAX as below should be ok?

 

MIN ( Events[event_start] ), Events[event_id] = 1  )
MAX( Events[event_start] ), Events[event_id] = 2  )

 

 

The same should easily work for MEDIANX, right? Or are there any pitfalls.

 

best and thanks!!

Joh

Hi Joh,

Glad it works!

On your first question about earliest ID 1 & latest ID 2: Yes, if you want the extremes then the existing formula is fine.

 

On your second question: Yes you can replace AVERAGEX with MEDIANX, but MEDIANX treats BLANK values as zero, while AVERAGEX ignores them. For this reason, you would have to rewrite the measure to filter out BLANK values. For example:

 

Median Days Event1-Event2 =
VAR ProductDuration =
    FILTER (
        ADDCOLUMNS (
            VALUES ( Events[Product] ),
            "@Duration",
                VAR DateEvent1 =
                    CALCULATE (
                        MAX ( Events[event_start] ),
                        Events[event_id] = 1
                    )
                VAR DateEvent2 =
                    CALCULATE (
                        MIN ( Events[event_start] ),
                        Events[event_id] = 2
                    )
                RETURN
                    IF (
                        NOT ISBLANK ( DateEvent1 )
                            && NOT ISBLANK ( DateEvent2 ),
                        DateEvent2 - DateEvent1
                    )
        ),
        NOT ISBLANK ( [@Duration] )
    )
RETURN
    MEDIANX (
        ProductDuration,
        [@Duration]
    )

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Thanks Owen, works like a charm!

 

Joh

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.