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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
janssenfrank
Regular Visitor

How to get an Weighted Average

I have a table with three colums: year, target and value. So that lookes like this:

 

YEARTARGETVALUE
2022Profit5%
2023Profit6%
2024Profit7%
2022Scrap10%
2023Scrap9%
2024Scrap8%

 

I want to know the Average Value if the Target is "Profit".

I need that value (Average Value ) to be shown in a card-visual. 

 

For example: In my report I select date range December 1st 2023 to December 1st 2024

 

That is 31 days of 365 in 2023 and 335 of 366 days in 2024.

So, the Average Value should be:

(31/365) * 6% + 355/366) * 7% = 6,92%.

 

It is like a Weighted Average. But how can I get this one value?

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi All,
Firstly  Greg_Deckler thank you for your solution!
And @janssenfrank ,I'm glad to tell you that your needs can be achieved, the following is the implementation of some ideas for your reference, we first selectvalue to get the filter to select the largest and smallest date, and then determine whether their year is a leap year, and then calculate the number of specific days, you can complete your needs, in accordance with your needs should be +335 so the results will be a little off!

Measure = 
VAR StartDate=SELECTEDVALUE('Date Table'[Date],MIN('Date Table'[Date]))
VAR EndDate=SELECTEDVALUE('Date Table'[Date],MAX('Date Table'[Date]))
VAR IsLeapYearA=IF(MOD(YEAR(StartDate),4)=0&&(MOD(YEAR(StartDate),100)<>0||MOD(YEAR(StartDate),400)=0),
    366,
    365)
VAR IsLeapYearB=IF(MOD(YEAR(EndDate),4)=0&&(MOD(YEAR(EndDate),100)<>0||MOD(YEAR(EndDate),400)=0),
366,
365)
VAR DaysInStartYear = DATEDIFF(StartDate, DATE(YEAR(StartDate), 12, 31), DAY) + 1
VAR DaysInEndYear = DATEDIFF(DATE(YEAR(EndDate), 1, 1), EndDate, DAY)
VAR WeightedStartYear = CALCULATE(MAX('Table'[VALUE]), 'Table'[YEAR] = YEAR(StartDate), 'Table'[TARGET] = "Profit") * (DaysInStartYear / IsLeapYearA)

RETURN
(WeightedStartYear+(DaysInEndYear/IsLeapYearB))*CALCULATE(MAX('Table'[VALUE]),'Table'[YEAR]=YEAR(EndDate),'Table'[TARGET]="Profit")

 

vxingshenmsft_0-1725353745741.png

I would be honoured if my solution solves your problem!

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi All,
Firstly  Greg_Deckler thank you for your solution!
And @janssenfrank ,I'm glad to tell you that your needs can be achieved, the following is the implementation of some ideas for your reference, we first selectvalue to get the filter to select the largest and smallest date, and then determine whether their year is a leap year, and then calculate the number of specific days, you can complete your needs, in accordance with your needs should be +335 so the results will be a little off!

Measure = 
VAR StartDate=SELECTEDVALUE('Date Table'[Date],MIN('Date Table'[Date]))
VAR EndDate=SELECTEDVALUE('Date Table'[Date],MAX('Date Table'[Date]))
VAR IsLeapYearA=IF(MOD(YEAR(StartDate),4)=0&&(MOD(YEAR(StartDate),100)<>0||MOD(YEAR(StartDate),400)=0),
    366,
    365)
VAR IsLeapYearB=IF(MOD(YEAR(EndDate),4)=0&&(MOD(YEAR(EndDate),100)<>0||MOD(YEAR(EndDate),400)=0),
366,
365)
VAR DaysInStartYear = DATEDIFF(StartDate, DATE(YEAR(StartDate), 12, 31), DAY) + 1
VAR DaysInEndYear = DATEDIFF(DATE(YEAR(EndDate), 1, 1), EndDate, DAY)
VAR WeightedStartYear = CALCULATE(MAX('Table'[VALUE]), 'Table'[YEAR] = YEAR(StartDate), 'Table'[TARGET] = "Profit") * (DaysInStartYear / IsLeapYearA)

RETURN
(WeightedStartYear+(DaysInEndYear/IsLeapYearB))*CALCULATE(MAX('Table'[VALUE]),'Table'[YEAR]=YEAR(EndDate),'Table'[TARGET]="Profit")

 

vxingshenmsft_0-1725353745741.png

I would be honoured if my solution solves your problem!

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Greg_Deckler
Community Champion
Community Champion

@janssenfrank See if this gets you there: Better Weighted Average per Category - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.