Want to pull a random result from a subset of a table

I know a post a similar topic related to meal planning, but I wasn’t able to follow enough of the setup to duplicated it in the fashion I need.

What I need is the ability to filter a table by a specific value in a specific column and then pull a random item only from the filtered table results.

Take this table on the shared page, for example

I’m trying to figure out how to pull a random item from this table where the “Role = Minion”, “Creature Type = Elemental”, or even “Role = Minion AND Creature Type =! Elemental”. Really advanced would be if I could do something with the logic of “give me a random row where Creature Type contains Elemental, Human, or Construct”

Any guidance would be most appreciated.

Hi @C.R_Rowenson ,
good to have you back, it has been a while

You are almost there. What about this formula?

thisTable.Filter([Creature Type].Contains(Elemental,Human,Construct)).RandomItem()

Cheers, Christiaan

2 Likes

In order to keep your formula as simple as possible and for it to respect the filter bar, start by making a view of your table. Use your view of this table to get your random item:

Place something like the following formula on the canvas:

[View of Hostile NPCs - D&D 2].RandomItem().WithName(RI,Concatenate(RI.Name," ",RI.Role))

image

The part following RandomItem() is only to show the result respecting the Role filter.

1 Like

That helps a great deal! Is there any way I can set up a filter using the filter bar and then see the formula version of what the filter bar is doing? THat would be incredibly helpful for reverse-engineering formulas and replicating results in different ways. For example, I know how to use the bar for something like “Creature type is not Elemental” but I’m not sure how to do that with just a formula

You can build those formulas yourself, but I don’t think you can retrieve the formula that is used by the filter band.
So, in order to reverse-engineer what is going on, you have to build the formula yourself, like so:
[View of Hostile NPCs - D&D 2].Filter([Creature Type].Contains(Elemental).not()).RandomItem()

But perhaps I am misunderstand your question?

I am glad i could be of service, but like @joost_mineur , I don’t understand your question.

I think you got my meaning. Is there documentation or a walkthrough somewhere that goes over the advanced components of the formula like the “.not” and so on? What I’ve found so far has just been general formula information… but I may be going about it the wrong way.

Even if there’s not. Thank you. THis has been very helpful