Displaying Lookup Table Fields in "Label" Question

I am wondering if there's a way to display the contents of the a lookup table as a "label" type question. For instance, I have a table that lists the address and phone number of a clinic in each county in California. If the user is interviewing someone who lives in a certain county, I'd like to display the address and phone number of the clinic in that county later on in the same form.

It seems to me that lookup tables are primarily used for multiple choice questions and I'm not seeing an easy way to use them for other question types, like label questions. For context, I'm relatively new to programming in CommCare and am not a software developer by any means.

Thank you!
Liz

Hi Liz

yes, you can, but it's just a question of whether or not you want to.

if you want to show a list of addresses for example, you can use something like a join or a concat to a value that you query the lookup table for.

for example, let's say you want to use it in a multiple choice, you'd put something in the query definition right?

take that, add a filter between brackets [column = value] to get a single value if you want to use it as a single value, or use a concat or join function to combine all the nodes you'll be returning. then you can stick that in a label.

but if you want to show something like an address and contact details and a bunch of other information, then it get a little trickier but you can also show them line by line sort of.
you would have to put a repeat group, whose size depends on a count(column from the query you've defined to your lookup table), then use an index each time you're returning a "row" by using position() + 1 to return something like
instance('lookuptable')/lookuptable_list/lookuptabl[column = value][1]/column_im_interested_in
where the position() function would have to be used to replace the "1" value in between the brackets [].
what you're looking at above will return all the rows in the lookup table where the filter column = value is true. the column it will return is the last bit at the end after the "/".

this was copied/pasted from one of my apps that returns the first name of a service it finds, because i'm using it in a label. I just know that the label i'm interested in is going to be duplicated with this filter and it will return many rows. i'm also sure that that value is the same. so picking up the first one is fine for my purpose.

so, imagine your repeat group "repeat_labels" now.

the size is count(instance('lookuptable')/lookuptable_list/lookuptabl[column = value][1]/column_im_interested_in).
this will return how many rows fit my criteria that have a value in that column i'm after.

now, imagine inside your repeat you had a label called label_placeholder.
you also have a hidden calculation that's called "my_label"
the calculate condition for which should be
instance('lookuptable')/lookuptable_list/lookuptabl[column = value][position(..) + 1]/column_im_interested_in
now for your label, you would put your hidden calculation in the question label

this would create a repeat group with the size automatically calculated through the count function,
a hidden calculation that returns 1 value from the query expression you have created
with that value shown in the label. when the user hits next, it'll be the next item in the repeat, so the position will change, so it'll be the next value, and so on.

so, yes, it's mostly used for multiple choice question, but this workaround enables you to display information from your lookup tables in labels too. just note that if your table is large, or you get many repeats, this might be slow to run on your device depending on how powerful it is

Good luck!

Hi Mazz,
Thank you so much for your response. This was really helpful. It looks like what you're describing is how to display multiple rows in a lookup table if there are multiple entries for the value I'm looking for. I actually ended up just using one row per county because it suited my purpose, but this looks really helpful for other use cases where I have multiple rows.

Much appreciated,
Liz

1 Like