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
Pedro77000
Frequent Visitor

Calculate an average with date filter

Hi Everyone,

 

I have a problem of averaging according to a context. I have a data model with a fact table and a "Date" dimension table

Indeed, I want to calculate the average based on the month selected to calculate the average over the last 12 months (12 rolling months). Look at the following table:

 

13/10/20196
21/10/20193
01/09/201915
14/09/20191
20/11/20193
27/11/20193
29/11/20196
30/11/20191


Par exemple :
- For example, the average I want to have when I select September  is: (15 + 1)/ 1= 16

- For example, the average I want to have when I select October is: (16 + 9)/2 = 12,5

- For example, the average I want to have when I select November  is: (16 + 9+13)/3 = 12,66

 

For information, the relationship between my fact table and the "Date" dimension is based on a "Date" field.

 

Thank you very much in advance !!
2 ACCEPTED SOLUTIONS
v-frfei-msft
Community Support
Community Support

Hi @Pedro77000 ,

 

We can create a measure as below.

Measure =
VAR SUMA =
    CALCULATE (
        SUM ( 'Table'[value] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] <= MAX ( 'date'[Date] ) )
    )
VAR COUNTM =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[YearMOnth] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] <= MAX ( 'date'[Date] ) )
    )
RETURN
    DIVIDE ( SUMA, COUNTM )

Capture.PNG

 

Also you can find the pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

Hi @Pedro77000 ,

 

To use ALLEXCEPT instead of ALL should work.

 

FILTER ( ALLEXCEPT ( 'Table','Table'[region] ), 'Table'[date] <= MAX ( 'date'[Date] ) )

 

If it doesn't meet your requirement,  Kindly share your sample data and excepted result to me if you don't have any Confidential Information. Please upload your files to One Drive and share the link here.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

5 REPLIES 5
v-frfei-msft
Community Support
Community Support

Hi @Pedro77000 ,

 

We can create a measure as below.

Measure =
VAR SUMA =
    CALCULATE (
        SUM ( 'Table'[value] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] <= MAX ( 'date'[Date] ) )
    )
VAR COUNTM =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[YearMOnth] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] <= MAX ( 'date'[Date] ) )
    )
RETURN
    DIVIDE ( SUMA, COUNTM )

Capture.PNG

 

Also you can find the pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Good morning v-frfei-msft !!

 

Thank you very much for this reply.

The solution works well. But this average I cannot have it by region, I believe because of the filter ALL.

The average for the total is good but when I split by region, it's the same overall average that appears in front of each region.

 

Hi @Pedro77000 ,

 

To use ALLEXCEPT instead of ALL should work.

 

FILTER ( ALLEXCEPT ( 'Table','Table'[region] ), 'Table'[date] <= MAX ( 'date'[Date] ) )

 

If it doesn't meet your requirement,  Kindly share your sample data and excepted result to me if you don't have any Confidential Information. Please upload your files to One Drive and share the link here.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Thank you so much Franck ! that works well ! even if the disadvantage of ALLEXCEPT is having to enter the fields of all dimension tables.

Anonymous
Not applicable

Hi @Pedro77000 

 

Try changing ALL to ALLSELECTED and then it should carry your filters through.

 

---
Please hit the "Accept as Solution" button if my post answered your question! If my post was helpful please consider giving it a "Thumbs Up."

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.

Top Solution Authors