Forum Discussion

Grella's avatar
Grella
Frequent Visitor
3 years ago

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

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    = #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})
    • Grella's avatar
      Grella
      Frequent Visitor

      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

      • wdx223_Daniel's avatar
        wdx223_Daniel
        Community Champion
        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