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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

How to calculate a value with dynamic count

Hi I would like to as the following advice:

 ItemSales
2019A5
2020A10
2020B20
2021A15
2021B30

So when i select the timeline 2020, the calculation will give me an average (5+15)/2, when select 2021, the calculation will be (5+10+15)/3 for item A

 

While the same for item B, when I select 2020, the average will be 20 and for 2021, the calculation will be (20+30)/2

 

Overall the equation will sum up the total base on time range selected and divide the total base on how many count found within that period and provide me an average for the item.

 

Thank you.

 

1 ACCEPTED SOLUTION
Adescrit
Impactful Individual
Impactful Individual

Thanks for the explanation. In this case I think the answer might be simpler:

 

Average2 = 
VAR __Calc = SUMX( VALUES( 'Table'[Item] ), CALCULATE( AVERAGE( 'Table'[Sales] )))
RETURN
    __Calc

Did I answer your question? Mark my post as a solution!
My LinkedIn

View solution in original post

10 REPLIES 10
Adescrit
Impactful Individual
Impactful Individual

Hi  @Anonymous

Does this work for you:

 

 

Average = 
VAR __Year =
    SELECTEDVALUE ( 'Table'[Year] )
VAR __YearCheck = IF( ISBLANK( __Year ), MAX( 'Table'[Year] ), __Year )
RETURN
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        ALLSELECTED ( 'Table'[Item] ),
        'Table'[Year] <= __YearCheck
    )

 


P.S. if you don't filter on a year it will take an average over all years.

 


Did I answer your question? Mark my post as a solution!
My LinkedIn
Anonymous
Not applicable

Hi, 

Sorry I forgot to mention the year also contains date and the expression is like "01 Jun 2019", is it possible to adjust the formula to suit that?

 

Thank you.

Adescrit
Impactful Individual
Impactful Individual

If you replace the Year column with a reference to the Date column in the same formula, does that work?


Did I answer your question? Mark my post as a solution!
My LinkedIn
Anonymous
Not applicable

The formula is working but unfortunately the result is not as the same as expected.

Jason123_0-1662037665462.png

The formula averaged both A & B item together and come up with 1 total average instead of 2 separated average for item A & B.

 

The above result is item A (5+10) and B (20) , the average under this formula become (5+10+20)/3 = 11.67 instead of 10 for item A and 20 for item B.

 

Furthermore if it is possible can the outcome be summing up the average from both A and B? (in this case my hope is to have the sum of the average (10+20) = 30

 

Thanks a lot.

Adescrit
Impactful Individual
Impactful Individual

Hi @Anonymous 

 

How about this:

 

Average2 = 
VAR __Date = SELECTEDVALUE( 'Table'[Date])
VAR __Calc = SUMX( VALUES( 'Table'[Item] ), CALCULATE( AVERAGE( 'Table'[Sales] ),  'Table'[Date] <= __Date ) )
RETURN
    __Calc

Did I answer your question? Mark my post as a solution!
My LinkedIn
Anonymous
Not applicable

Hi Adescrit,

 

Thanks for coming back, the formula is not returning any value if I pull and select more than 1 date in the timeline slicer.

 

But if i select a specific date, it does calculate the average of for all the value before the selected date, any chance we can adjust the date filter?

 

Thanks.

Adescrit
Impactful Individual
Impactful Individual

Hi @Anonymous 

 

Can you please confirm for me how you expect it to work? Should it provide the average for the exact date(s) you have selected?

 

Based on your examples I assumed it was an average of all dates up to and including the one selected.

 

Adrian


Did I answer your question? Mark my post as a solution!
My LinkedIn
Anonymous
Not applicable

Hi Adrian,

 

Sorry for the confusion, let me rephase my question below.

 

Data Example:

YearItemSales
01/01/2019A5
01/05/2019B20
01/06/2020A15
01/06/2020B30

 

I would like to have a caluclation that can provide me a dynamic caluclation on the sum of average.

 

Jason123_0-1662115541355.png

For example, if I pull the timeline slicer to May 2019 it gives me a sum of average A(5) + average B(25) = 25

Jason123_1-1662115633523.png

And when I adjust the timeline from May 2019 to cover Jun 2020, the result should be total of average A(15) + average B((20+30)/2) with these period which is 40. In this case Jan 2019 data will be excluded.

 

Appreciate your help!

Adescrit
Impactful Individual
Impactful Individual

Thanks for the explanation. In this case I think the answer might be simpler:

 

Average2 = 
VAR __Calc = SUMX( VALUES( 'Table'[Item] ), CALCULATE( AVERAGE( 'Table'[Sales] )))
RETURN
    __Calc

Did I answer your question? Mark my post as a solution!
My LinkedIn
Anonymous
Not applicable

Hi Adrian,

 

You are amazing, didn't know this formula can be done in this way.

 

Thanks a lot!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Kudoed Authors