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

AVG of a measure based on dates pulled in

I would like to achieve a measure to calculate an average of employee count based on the dates that are pulled in. For example, using the screen shot below, for Dec 22 the calculation would be =AVG(SUM(2285+2328),2).....2 in the equation is the count if current period and previous period.

 

the Jan 2023 denominator would be 3 and so forth.

 

I need the formula to be adaptable for any range of periods.

 

Trying2Excel_0-1679501461482.png

 

1 ACCEPTED SOLUTION
MohammadLoran25
Super User
Super User

Hi @Trying2Excel 

 

1-In your DateTable, Create a calculated column as below:

 

YearMonthIndex=YEAR(DateTable[Date])*12+MONTH(DateTable[Date])
 
It gives you the index for combinations of year and month.
 
2-Then Create your measure as this:
VAR _MINIMUMMONTHYEARINDEX=CALCULATE(MIN(DateTable[YearMonthIndex]),ALLSELECTED ( DateTable ))
 RETURN
CALCULATE (
    AVERAGEX(VALUES(DateTable[YearMonthIndex]),[salesss]),
    FILTER (
        ALL ( DateTable ),
        DateTable[YearMonthIndex]
             
                <= MAX ( DateTable[YearMonthIndex] )
                &&
                DateTable[YearMonthIndex]>=_MINIMUMMONTHYEARINDEX
               
               
    )
)
 
 
If this answer solves your problem, please mark it as an accepted solution so the others would find what they need easier.
Regards,
Loran

View solution in original post

10 REPLIES 10
MohammadLoran25
Super User
Super User

Hi @Trying2Excel 

 

1-In your DateTable, Create a calculated column as below:

 

YearMonthIndex=YEAR(DateTable[Date])*12+MONTH(DateTable[Date])
 
It gives you the index for combinations of year and month.
 
2-Then Create your measure as this:
VAR _MINIMUMMONTHYEARINDEX=CALCULATE(MIN(DateTable[YearMonthIndex]),ALLSELECTED ( DateTable ))
 RETURN
CALCULATE (
    AVERAGEX(VALUES(DateTable[YearMonthIndex]),[salesss]),
    FILTER (
        ALL ( DateTable ),
        DateTable[YearMonthIndex]
             
                <= MAX ( DateTable[YearMonthIndex] )
                &&
                DateTable[YearMonthIndex]>=_MINIMUMMONTHYEARINDEX
               
               
    )
)
 
 
If this answer solves your problem, please mark it as an accepted solution so the others would find what they need easier.
Regards,
Loran

Hi! Thank you for the quick response, the YEAR & MONTH functions are not allowing me to select a table[column], only measures. Am I doing something wrong?

@Trying2Excel 

I think your are putting it in a measure so it is the reason.

Create it as a calculated column. Then it would be OKay.

Okay figure that out. These are my results. The second image are the numbers I am trying to get to...

Trying2Excel_0-1679508673969.png

Trying2Excel_1-1679508733676.pngTrying2Excel_2-1679508741367.png

 

Did you put latest version of my measure?

It includes variables.

Yes! I have updated the measure and I am getting all sorts of errors...

Trying2Excel_0-1679509630571.png

VAR _MINIMUMMONTHYEARINDEX = CALCULATE(MIN(Periods[MonthYearIndex]),ALLSELECTED ( Periods ))
 RETURN
CALCULATE (
    AVERAGEX(VALUES(Periods[MonthYearIndex]), 'Worker Type'[Worker Count]),
    FILTER (
        ALL ( Periods ),
        Periods[MonthYearIndex]
             
                <= MAX (Periods[MonthYearIndex]) && Periods[MonthYearIndex] >=_MINIMUMMONTHYEARINDEX
               
               
    ))

It is because you do not have set any name for your measure.

MeasureName=

My Formula 

Yikes! Rookie here. 🙂 It worked! That is awesome! One last thing do you know why the Index would be duplicating like this:

 

Trying2Excel_0-1679510588750.png

 

Should not be like that.

Send your formula. Is it set on "Don't be summarized" option?

Discard figured it out! Thank you so much for your help!

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.