samedi 22 août 2020

Amazon QuickSight extract a string from a comma separated unsorted string using calculated field

I am working on a QuickSight table graph, where the Dataset has a field that is a set of words separated by ',' (commas). These words are not sorted in any order, but for each row of the dataset, there might be 1 or more words and they can be repeated across the rows, but not for the same data row.

giraffe, lion, apple, mango, dog, frog, banana
lion, apple
banana, apple, giraffe, lion, mango, dog
giraffe, mango, dog, banana, lion, apple
mango, dog, frog, giraffe, banana, apple
frog
giraffe
lion, giraffe, dog

I want to separate these into individual 'calculated fields', where I want just that word if it is in the row to be shown and a NULL, if the word is not in that complete row. I am using the following construct for it:

calculated field name: 
"giraffe"

value to calculate the field:

ifelse(
    (
        (split({AnimalFruit}, ',', 1) = "giraffe") OR 
        (split({AnimalFruit}, ',', 2) = "giraffe") OR  
        (split({AnimalFruit}, ',', 3) = "giraffe") OR  
        (split({AnimalFruit}, ',', 4) = "giraffe") OR  
        (split({AnimalFruit}, ',', 5) = "giraffe") OR  
        (split({AnimalFruit}, ',', 6) = "giraffe") OR  
        (split({AnimalFruit}, ',', 7) = "giraffe") OR  
        (split({AnimalFruit}, ',', 8) = "giraffe") OR  
        (split({AnimalFruit}, ',', 9) = "giraffe") OR  
        (split({AnimalFruit}, ',', 10) = "giraffe")
    ),
    "giraffe",
    NULL
)

But the above just gives me the rows where the 'giraffe' is the first word, and not the lines where it might be the 2nd or 3rd or later in the row.

I am not sure what I am doing wrong here, as the logic seems to be correct to me. Is there something else I can do to get this result?

I want to create 7 fields, one for each word:

giraffe
lion
apple
mango
dog
frog
banana

I have also tried using the following construct as given in the AWS page for 'ifelse':

https://docs.aws.amazon.com/quicksight/latest/user/ifelse-function.html

ifelse(
    (split({Flags}, ',', 1)) = "giraffe", "giraffe",
    (split({Flags}, ',', 2)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 3)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 4)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 5)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 6)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 7)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 8)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 9)) = "giraffe", "giraffe", 
    (split({Flags}, ',', 10)) = "giraffe", "giraffe", 
    NULL
)

Aucun commentaire:

Enregistrer un commentaire