When the Aggregate functions are getting you down, try RefFunctions.
Install the RefFunctions plugin. This adds a variety of useful custom functions to the expression builder, including
Retrieve target field value from the nearest target feature in target layer. Syntax:
targetLayer → the name of a currently loaded layer, for example 'myLayer'.
targetField → a field in target layer we want as result when source feature is within target feature, for example 'myField'.
If targetField is equal to '$geometry' The WKT geometry of targetFeature willbe retrieved. If otherwise is equal to '$distance' the calculated distance between source and target features will be returned
For the default field value, use
geomnearest to auto-fill the field with the targetField of the nearest point, no matter how far away that point is. (note: I tested this expression, and it worked)
I tried to find a solution using
geomdistance to auto-fill the field with the targetField of the nearest point, as long as that point is within a specified distance. However, I couldn't quite make it work. I'm including my best attempt; feel free to use it as a starting point. Comment if you find a solution, or feel free to post as a separate answer.
if( distance( start_point($geometry), geomnearest('Poles',$geometry))<5, geomnearest('Poles','Pole Id'), NULL)
The error seems to come from using
geomnearest to get a feature's geometry. I can get other RefFunctions to work with
$geometry as a parameter, but not
geomnearest. When I use the recommended syntax from the function description,
geomnearest('Poles','$geometry'), I get this error:
Eval Error: 'QgsGeometry' object has no attribute 'exportToWkt'
But if I use the syntax that seems right to me,
geomnearest('Poles',$geometry), I get this error:
Eval Error: error: targetFieldName not present.
You can probably get around this by adding the WKT geometry as a text field, using the field calculator and
geom_to_WKT($geometry). Then substitute for the second parameter in the distance function above: