How can I show the child category and not it's parent - but other times show it's parent? I have a categories field on an entry that can have one or many children. I am able to output everything - but would like to restrict the field a bit more.

If a child has been selected, I do not want to show the parent. In my example, I only want to show "child".

It is possible, that I might only choose "parent" and I only want that to show. Or, is there a different way I should go about doing this? Thank you for any suggestions!



I've tried something like this:

{% for service in entry.servies %}
   {% set subCategories = craft.categories.descendantOf(service) %}

   {% if subCategories|length %}
       ...
   {% endif %}
{% endfor %}


But that will show the child category if it's selected or not. I would only like to show the "Child" category if it was actually selected. Otherwise, show the "parent" category. 

UPDATE/SOLUTION

First, I want to thank Robin for your help. I tried different variations and was unsuccessful. I ended up taking my existing category and making it a structure. This gave me the flexibility that I needed.
You can change the sort order in your CategoryQuery

{% set child = entry.getFieldValue('servies').orderBy('level DESC').one() %}


returns the category of the deepest level -> the child. If no child is selected you'll receive the parent