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
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
Fabcon_Europe_Social_Bogo

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.