1

This might be simple, but I can't wrap my head around it... I need a Custom Column in Power Query to return data from a specific column in another row

Currently I have location data for all employee ID numbers, but for some, the location is blank. In this data, in any given employee's row, there is also their manager's ID#.

What I need is a custom row that returns the employee's manager's location IF the employee's location is blank. For now, I am not looking to fix manager's that also do not have a location, if the manager's location is blank, I am ok with the Employee's pulling blanks in these cases only.

Any help would be greatly appreciated.

2 Answers 2

5

Merge the table on top of itself using the manager ID column on top matched to the employee ID column on bottom.

enter image description here

Expand [x] location using the arrows atop the new column.

Add column ... custom column ... with formula

=if [location] =null then [putnameofnewcolumnyouexpandedhere] else [location]

or

= [location] ?? [putnameofnewcolumnyouexpandedhere]

Right click remove extra columns

enter image description here

let  Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Merged Queries" = Table.NestedJoin(Source, {"ManagerID"}, Source, {"ID"}, "Source", JoinKind.LeftOuter),
#"Expanded Source" = Table.ExpandTableColumn(#"Merged Queries", "Source", {"Location"}, {"Manager.Location"}),
#"Added Custom" = Table.AddColumn(#"Expanded Source", "CombinedLocation", each if [Location]=null then [Manager.Location] else [Location]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Location", "Manager.Location"})
in  #"Removed Columns"
3

You can do something like this:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    AddLocation 
        = Table.AddColumn(
            Source,
            "LocationCleaned",
            (a) => 
                if  a[location] = null then 
                    Table.SelectRows(Source, each [emp ID] = a[manager] )[location]{0}
                else 
                    a[location]
        )
in
    AddLocation

enter image description here

1
  • Thank you horseyride and FlexYourData, both of these worked! but with the vast amount of rows i have (from merging multiple files into 1), the solution from horseyride runs faster. happy holidays!
    – Olendris63
    Commented Dec 22, 2022 at 21:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.