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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

Measure with average of difference between value for max date and value for min date (within groups)

Hi, 

I'd like to know how to get with DAX the average difference between the value corresponding to a max date within a group and the value corresponding to a min date within a group. I'll explain this with an example: 

Suppose my data is the following: 

Row_ID  Market  DateHourValue
1A01-09-2022 12:02:12  23
2A01-09-2022 12:03:1425
3A01-09-2022 12:05:5228
4B01-09-2022 12:00:0312
5B01-09-2022 12:12:3215
6B01-09-2022 12:23:0914

 

 

What I'd like to obtain is the difference between Value for Max DateHour and Value for Min DateHour within Market (which is my grouping criteria) and, then, get the average of this difference. 

 

For Market A, Value for Max DateHour is 28, and Value for Min DateHour is 23. Thus, the difference is 5 (Row_ID=2 is discarded because it's not Max DateHour nor Min DateHour)

For Market B, Value for Max DateHour is 14, and Value for Min DateHour is 12. Thus, the difference is 2  (Row_ID=5 is discarded because it's not Max DateHour nor Min DateHour)

 

And the average would be AVERAGE(5,2)= 3.5. 

 

How can I get  a measure (not a calculated column) for this in DAX?

 

Thanks for your help 🙂

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 

if you are using a card visual or slicing by market only then use

 

Average Difference =
AVERAGEX (
    VALUES ( TableName[Market] ),
    VAR MaxDate =
        CALCULATE ( MAX ( TableName[DateHour] ) )
    VAR MinDate =
        CALCULATE ( MIN ( TableName[DateHour] ) )
    VAR MaxDateValue =
        CALCULATE ( MAX ( TableName[Value] ), TableName[DateHour] = MaxDate )
    VAR MinDateValue =
        CALCULATE ( MIN ( TableName[Value] ), TableName[DateHour] = MinDate )
    RETURN
        MaxDateValue - MinDateValue
)

 

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi @Anonymous 

if you are using a card visual or slicing by market only then use

 

Average Difference =
AVERAGEX (
    VALUES ( TableName[Market] ),
    VAR MaxDate =
        CALCULATE ( MAX ( TableName[DateHour] ) )
    VAR MinDate =
        CALCULATE ( MIN ( TableName[DateHour] ) )
    VAR MaxDateValue =
        CALCULATE ( MAX ( TableName[Value] ), TableName[DateHour] = MaxDate )
    VAR MinDateValue =
        CALCULATE ( MIN ( TableName[Value] ), TableName[DateHour] = MinDate )
    RETURN
        MaxDateValue - MinDateValue
)

 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

October NL Carousel

Fabric Community Update - October 2024

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