If you have everything on one layer (as it seems to be the case), you should use aggregate functions and filter the aggregated city_names
by conty_name
to create an array with all cities in a county using array_agg()
function. From this array, loop through each element (city) to get the respective attribute values for the fiels people_ok
and people_not_ok
.
Edit: I tested your sample project and had to make only small adjustments. Use a text layout element, not an attribute table layout element, to introduce this text, as you don't really need an attribute table element for this, you use it to create a text, containing different attribute values concatenated. HOwever, the expression also works if you intreroduce it in the attribute table layout element.
Use the expression below:
with_variable(
'county',
county_nam,
array_to_string(
array_foreach(
array_agg (
city_name,
filter:="county_nam"=@county
),
@element
|| ': ' ||
attribute(
get_feature('counties_cities','city_name', 'Messine'@element),
'people_ok'
)
|| ' % of people are OK and'and ' ||
attribute(
get_feature('counties_cities','city_name', 'Messine'@element),
'people_not'
)
|| ' % are not'
),
delimiter:= '\n'
)
)