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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

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
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.