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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mbl
Frequent Visitor

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 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
Super User
Super User

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


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors