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
Grella
Frequent Visitor

Replace null values with conditional average

Help please!

I'm still fairly new to Power BI and am trying to replace null values in my data with the average of the previous 3 months with some conditions. 

I have simplified the 2 tables to demonstrate my scenario: 

- Units Sold table: shows the number of units sold by a particular employee in a particular month

- Employee Details table: shows if the employee is still employed with the company or has left (and the date their employment ended)

 

In my Units Sold table, i want to replace all the null values with the average from the previous 3 months data for that particular person, even if one month in the previous 3 is also a calculated average (see desired value for Jessica in 1/05/2021). Before calculating this average, i would need to be able to check the Employee Details table to ensure the person is still employed - any null values after the Emp. End date should remain null. 

 

EmployeeDateUnits SoldExpected Value
Jessica1/01/20212875 
Jessica1/02/20213452 
Jessica1/03/20211217 
Jessica1/04/2021null2515
Jessica1/05/2021null2395
Jessica1/06/20215816 
Jessica1/07/20211056 
Jessica1/08/2021null3089
Jessica1/09/20214956 
Jessica1/10/20212534 
Jessica1/11/20217512 
Jessica1/12/20213185 
Abigail1/01/20215672 
Abigail1/02/20218495 
Abigail1/03/20212356 
Abigail1/04/2021null5508
Abigail1/05/20214675 
Abigail1/06/20211523 
Abigail1/07/2021nullnull
Abigail1/08/2021nullnull
Abigail1/09/2021nullnull
Abigail1/10/2021nullnull
Abigail1/11/2021nullnull
Abigail1/12/2021nullnull

 

EmployeeEmployment StatusEmp. End date
JessicaEmployed 
AbigailResigned29/06/2021

 

Any help on the best way to tackle this in power query would be greatly appreciated!

5 REPLIES 5
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1667203187330.png

= #table(Table.ColumnNames(UnitsSold),List.Accumulate(Table.ToRows(UnitsSold),{{},{},""},(x,y)=>let a=EmployeeDetails{[Employee=y{0},Employment Status="Resigned"]}?[#"Emp. End date"]? ??DateTime.LocalNow(),b=List.Average(List.LastN(x{1},3)) in if y{1}>=a then {x{0}&{y},{},y{0}} else if y{0}<>x{2} then {x{0}&{y},{y{2}},y{0}} else if y{2}<> null then {x{0}&{y},x{1}&{y{2}},y{0}} else {x{0}&{List.FirstN(y,2)&{b}},x{1}&{b},y{0}}){0})

Thanks for your response @wdx223_Daniel . Would you mind sharing the code for your first 2 steps (UnitsSold & EmployeeDetails)? I have pulled these two tables into power query as 2 separate tables, so not sure how you are referencing EmployeeDetails within the same table

let
    UnitsSold = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    EmployeeDetails = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table2"]}[Content]),
    Custom1 = #table(
                     Table.ColumnNames(UnitsSold),
                     List.Accumulate(
                                     Table.ToRows(UnitsSold),
                                     {{},{},""},
                                     (x,y)=>let
                                               a=EmployeeDetails{[Employee=y{0},Employment Status="Resigned"]}?[#"Emp. End date"]? ??DateTime.LocalNow(),
                                               b=List.Average(List.LastN(x{1},3))
                                            in
                                               if y{1}>=a then {x{0}&{y},{},y{0}} else
                                               if y{0}<>x{2} then {x{0}&{y},{y{2}},y{0}} else
                                               if y{2}<> null then {x{0}&{y},x{1}&{y{2}},y{0}}
                                               else {x{0}&{List.FirstN(y,2)&{b}},x{1}&{b},y{0}}
                                    ){0}
                    )
in
    Custom1

Would you still happen to have this file and can attach? I still can't seem to replicate the result, and i also don't fully understand the code in laymans terms to be able to troubleshoot exactly where i'm going wrong

wrote this code in a temp workbook without saving. sorry for that

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
Top Kudoed Authors