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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Conditional Dynamic DAX to calculate Avg of recent 2 weeks or 1 week before &after from audit week

Dynamic DAX to calculate average of recent 2 weeks before and after from audit dates and if only  1 week data is available than consider in the calculation AND FLAG all conditions in the FINAL OUT WITH THE CONDITIONS COMMENTS mentioned below

 

Audit data        (Table 1)

 

 

Emp idAudit Week start
15/31/2020
25/31/2020
35/31/2020
45/31/2020
55/31/2020
65/31/2020
75/31/2020

 

 

Production Score     ( Table 2)

Emp idScore Week start
15/17/2020
15/24/2020
15/31/2020
16/7/2020
16/14/2020
25/3/2020
25/24/2020
25/31/2020
26/14/2020
26/21/2020
35/31/2020
36/14/2020
36/21/2020
45/17/2020
45/24/2020
45/31/2020
55/17/2020
55/24/2020
55/31/2020
56/14/2020
65/17/2020
65/31/2020
66/14/2020
66/21/2020
75/24/2020
75/31/2020
76/14/2020

 

Conditions :-

Condition -1 ->In orginal data  both  tables are  NOT sorted, hence please CONSIDER this in the calculation if required & EXCLUDE Audit weeks while calculating before and after
Condition-2 (Idle Situation)-> (Employee-1)  -> From feedback date it should take average values of recent 2 weeks/ # Flag -> "Include- Recent 2 Weeks"
Condition-3 (Employee-2)  -> Incase last data is missing for last 2 weeks or 1 week, formula should search untill it finds values for 2 weeks for both before & after /# Flag   -> "Include- Others 2 Week"
Condition-4 (Employee-3&4)  ->  Incase data is missing for  before ,after or Both it should be excluded from the calculation/# Flag- Exclude- Missing weeks before" / # Flag- Exclude- Missing weeks after"/ # Flag- Exclude- Missing weeks"
Condition-5 (Employee-5&6)  ->  Incase data is only available for 1 week than consider it as before and after/# Flag- "Include -1 Week After" / # Flag- "Include -1 Week Before"
Condition-6 (Employee-7)  ->  Incase data is only available for 1 week for both than consider it as before and after/# Flag- "Include -Both 1 Week Before & After"

 

Final Output required columns

 

Reference COLUMN for formulaEmp idAudit week startBefore datesAfter datesAvg Before ScoreAvg After ScoreConditions Flags (
Condition-2

1

  5/31/20205/24/2020 & 5/17/2020 6/14/2020 & 6/07/2020200200Include- Recent 2 Weeks
Condition-325/31/20205/3/2020 & 5/24/2020 6/21/2020 & 6/14/2020200200Include- Others 2 Weeks
Condition-435/31/2020- 6/21/2020 & 6/14/20200200 Exclude- Missing weeks Before
Condition-44 5/31/2020 5/24/2020 & 5/17/2020-2000Exclude- Missing weeks After
Condition-55 5/31/20205/24/2020 & 5/17/20206/14/2020200200Include -1 Week After
Condition-565/31/20205/17/2020 6/21/2020 & 6/14/2020200200Include -1 Week Before
Condition-67 5/31/20205/24/20206/14/2020200200 Include -Both 1 Week Before & After 

 

Thanks in advance.

3 REPLIES 3
amitchandak
Super User
Super User

@Anonymous , refer these two can help

Power BI — WTD Questions— Time Intelligence 4–5
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandak Thanks these are super helpful

Now my new challenge is 

I need before and after average scores of THOSE EMPLOYEES WHO WERE AUDITED not for all

The DAX should try to find values of 2 weeks before 6 weeks & after 6 weeks of audit week.Please note there is possibiity that only 1 week data is available for some employees.
And provides a measure Avg score BEFORE & Avg score AFTER ...

Audit data                     (Table 1) 
emp_idAudit Week start
15/31/2020
Production Score     ( Table 2)  
emp_idScore Week startScore
15/17/202059
15/24/20200
25/24/202091
16/7/202031
16/14/202061

 

Please assist 🙂 

Greg_Deckler
Community Champion
Community Champion

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...



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

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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