You can do this by adding a VirtualField to your layer (which will be calculated upon feature update/creation), using the following expression:
filter:= contains($geometry, geometry(@parent)),
In this case Polygons is the ...
You can count the number of nodes in your legend using PyQGIS like below (standalone use)
project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('myLayout')
# We consider only one legend
itemLegend = [item for item in layout.items() if isinstance(item, QgsLayoutItemLegend)]
# It's a tree, we should follow it ...
This is probably because your total_properties field is numeric and concatenate only works on string data types (why QGIS doesn't autoconvert numeric values for this is beyond me - perhaps in a future release).
Try using expression:=to_string("total_properties") in your expression and see if that works.
You can use the map variant of replace, where the map is a series of key-value, the key being the string being searched for and the value being the replacement string