The IN operator
The new IN operator finally lets you bin those endless OR statements and even check values against a set.
- The IN operator lets you replace long OR-based calculations with simpler, more maintainable syntax by checking a value against a bracketed list of expressions
- You can use a double forward slash to comment out lines within a Tableau calculation
- Changing a calculation's data type to true/false may require removing and re-adding the field on a shelf to refresh it
- The IN operator can check a value against a set, not just a list of literals
- Combining IN with set actions lets dashboard users dynamically build their own selection list, with parameters usable on either side
0:00Hey, it's Tim here. And in today's video, I
0:04'm going to be showing you the new in
0:06operator
0:07in 2020.3. This is a new function that
0:10essentially allows you to get around some
0:13very long calculations
0:14that we use to write using the OR function.
0:17It doesn't replace the OR function, just
0:18adds
0:18a little bit of capability. It also allows
0:21us to work in a slightly different way with
0:23sets. So let's hop into this and I'll show
0:25you how it works. Just also upfront here,
0:28this is a pre-release version of Tableau
0:29that I'm using. And I've noticed a few bugs
0:31, especially
0:32with this function whilst using it. So we
0:34might have to fix some of those as we're
0:36building
0:36out this function, but otherwise it should
0:39work pretty much as the final release is
0:41going
0:41to work. I'm going to hit Super Source
0:43Sales here and open up the default data set
0:45inside
0:46of Tableau. Okay. So first thing I'm going
0:48to do is just build a very basic chart so
0:50we can see what's going on. I'm going to
0:52drag sales onto columns and then I'm going
0:54to drag
0:55category onto rows and subcategory just
0:57after that. I could have also hit the plus
1:01icon
1:01just here inside a category to do the same
1:03thing because it's actually a hierarchy. So
1:06that's why you get these sort of plus icons
1:08and minus icons to sort of collapse and
1:11expand
1:11the hierarchy. Okay. We've pretty much
1:13created the chart we needed to create. I'm
1:15just going
1:16to create a calculated field. And by the
1:17way, if you're watching this and you're
1:19wondering,
1:19"Oh, I know some of this already," this
1:21video is targeted at people who are
1:23discovering
1:23Tableau for the first time and just want to
1:25know what's new. So it's going to be a very
1:27,
1:27very simple guide. I'm sure there'll be
1:29lots of great blog posts showing some of
1:30the more
1:31advanced features. But okay, let's dive
1:33into a calculation we would have written
1:35before.
1:35So something you would have written before
1:38possibly, if I just type if and then I hold
1:41control and scroll my mouse wheel up and
1:43down, it makes this a little bit larger so
1:44you can
1:45see. So if I type in category and equals,
1:51let's say, furniture or the category, just
1:58do autocompletion there, equals, I always
2:02type my speech marks first, then type the
2:06end properly. I could also use the autocom
2:08plete, but I'm not going to do that. Hello,
2:10world,
2:10because we can be programmers for a few
2:13seconds. Else, goodbye, world. Why not?
2:18Just say that.
2:19And so we make sure we end our calculation
2:22appropriately. And I can see here at the
2:24bottom
2:25that the calculation is valid. Okay, great.
2:28This is set up. I'm just going to give it
2:30a name. I'm going to call it in operator.
2:32So we have a name for the field, hit apply.
2:35And then there we have it. The in operator
2:37is available to us. I can drop that in. And
2:39you can see that it's actually working. It
2:41would help if I can spell goodbye properly.
2:43You're probably screaming at the screen
2:45right now. But there we go. We have the
2:46operator
2:47is working. And you can see here that hello
2:49, world is associated with furniture and
2:51technology.
2:52I'll drag this up so we can see this change
2:54a little bit clearer when we change this
2:56calculation.
2:56Okay, the next step is to show you how the
2:58in operator can make this calculation a lot
3:01easier to write. I'm just going to hit
3:03double forward slash. A little quick tip.
3:05This allows
3:06you to essentially comment out a particular
3:08line in a calculation. So what I'm doing
3:10now
3:10is just commenting out the old calculation,
3:13go back up to the top. And in here, I'm
3:15going
3:16to start writing the new function. So to
3:18use the in operator on the left hand side,
3:20you
3:20basically put the thing that you're
3:22checking. So category in this particular
3:25case, and then
3:26I'm going to check to see if any of the
3:28expressions I'm about to write are in the
3:31category. Okay,
3:32so I'm checking the left with the right
3:34hand side. And because I'm just going to
3:35type in
3:36some text here, I'm going to put a bracket
3:38to container. In the next step, I'll show
3:40you something a little bit different with
3:42sets, which would be really, really cool.
3:43But let's keep on doing this. Let's just, I
3:46'm a really lazy Tableau calculation author,
3:50so I tend to just actually copy and paste
3:51things I've already written before. It also
3:54guarantees that you're less likely to make
3:56mistakes. So I'm just going to leave it
3:58like
3:58that. And I might get a bug here when I hit
4:01apply. There we go. This has now gone red.
4:04The reason it's gone red is because the
4:05data type has changed in the calculation.
4:08So if
4:08you see here on the left hand side where my
4:10mouse is, this has changed to true and
4:12false.
4:13So what I need to do is just remove it, and
4:16then add it back in. And now you can see I
4:19get a true and false in that column. So now
4:22this is doing the exact same thing. And
4:24just
4:24compare these two. This is much easier to
4:27write, right? It's a lot simpler, and it's
4:31going to make it a lot nicer to maintain
4:34really long calculations that have the
4:36phrase "or"
4:37essentially. You can just go through some
4:39of your old calculations, just clean them
4:40out with this much, much neater to follow n
4:44omenclature as well.
4:46Now the next thing I'd like to do is make
4:49this a little bit more dynamic. So to do
4:52this,
4:52I'm going to create a set. The category set
4:54I'm going to create is going to have the
4:56exact
4:56same two groups of furniture and technology
4:59. I'm going to go ahead here, create a set,
5:02and then I'm going to put the office
5:05supplies, no, actually technology and
5:07furniture. I'm
5:09going to call this the category set. Hit OK
5:11, and now we have our category set. And what
5:14the in operator can actually do, if I just
5:16copy this line and put it onto a new one,
5:19is essentially, I'm just going to comment
5:21that out and put this one in here, is
5:23actually
5:23check a set to make sure that the thing I'm
5:27actually checking for is in my set. How
5:30cool
5:30is that? So now if I hit apply, you'll see
5:32the calculation still works. It's doing the
5:35exact same thing. And the cool thing with
5:38this is now you can use set actions to
5:40dynamically
5:41change what's in this set, giving the user
5:43the ability to basically build their own
5:46list
5:46of what you're checking. So how cool is
5:48that?
5:48Let's just hop into another tab. I'm going
5:51to have to build a dashboard very, very
5:53quickly
5:54here to show you this working. So I'm going
5:56to open another sheet. Apologies for not
5:58naming
5:58my sheets correctly. First thing I'm going
6:01to do is drag quantity onto size and then
6:03drag category onto color. And this builds
6:06out this really nice, cool sort of tree map
6:10.
6:10Actually I don't need to put category on
6:12color. I just need to put it on detail for
6:13now. And
6:16the next thing I'm going to do is just
6:17build a simple list. I'm just going to
6:18build a very
6:19simple list of items. And to do this, I'm
6:23just going to bring the category onto rows.
6:25I did this in there where I dragged the
6:27dimension first. And I'm just going to show
6:29the value
6:30of each category in a table. Okay. And so
6:32what we're going to do is going to use this
6:34sheet to control our main visualization. So
6:37let's go over to our dashboard. I'm just
6:39going
6:39to do the classic double click trick to get
6:42everything in place. I'll leave the sheet
6:44names up top. And this is pretty much
6:47everything that we want. Now I'm going to
6:49go back into
6:49sheet number two and I'm going to bring my
6:53category set onto color. And the reason I
6:55want to do this is so that we can see what
6:57's going on. And the last thing I'll also do
6:59is put the label of each category so we can
7:02clearly see the selections are actually
7:05working.
7:05So right now what this is telling me is
7:07that technology and furniture are in my set
7:09, hence
7:10their blue, and office supplies is not in
7:12my set. Okay. So if we go back to our
7:15dashboard,
7:15you can now see we have the resemblance of
7:17a dashboard. I'm going to have a list
7:19selector
7:20here and it's basically going to change the
7:23set and you should see the colors here
7:25changing
7:26based on my selection. So let's go ahead
7:29and actually make that happen. To make that
7:31happen,
7:31I'm going to go to dashboard and I'm going
7:33to hit this option for actions. And so if
7:35I hit actions, add an action, and I want
7:38the action to change the set value. So this
7:41is
7:42really important that you choose the right
7:44action here. The next thing is to choose
7:45your
7:46source sheet. Where is the action going to
7:48be driven from? In this case, it's actually
7:51sheet number three. And I want to do this
7:53on selection. You can see here that
7:54selection
7:55is already preselected. And the thing I
7:58want to do is assign values to the set. So
8:00you
8:00can see here that's already ticked. And
8:03when I clear the selection, I'd like it to
8:06remove
8:08all values from the set. So I'd like
8:10nothing to be in the set. So it should all
8:12turn gray
8:13if I clear the selection that I've just
8:15made. And the thing I want to change is the
8:17category
8:18set. This is really important. You must
8:20make sure you change the right thing to hit
8:22the
8:22category set. And I'm going to say this
8:26action should be called change category set
8:30. All
8:31right. So here we go. We're pretty much
8:33ready to go. I'm going to hit OK. Hit OK.
8:37And now
8:37we're on the sheet, but nothing's happened.
8:39Now, if we make a change to this, let's add
8:41office supplies. You'll see that now office
8:44supplies goes blue and everything else goes
8:47gray because it's changing the set. And it
8:50's doing that and using the coloring from
8:52our
8:53previous setup. Now, if we go back in here
8:56and you can see the coloring is actually
8:58based
8:58on our set, which is incorrect. What I need
9:03to do is actually make sure it's based on
9:05our in operator. And now you can see this
9:08is doing the exact same thing. So now if I
9:10select furniture, you can see it's kind of
9:12spreading this out. If I select everything,
9:15it all goes orange. But now this is
9:17dynamically doing this. And it's a really,
9:19really nice
9:19sort of addition. And notice when you
9:21change this, it's moving whatever select it
9:23's always
9:24to the left. So it's always really clear
9:26what's going on. Actually, it's not doing
9:28that, but
9:29I thought it was doing that. But that's
9:30just because office supplies is large. But
9:32you
9:32can see here that it's changing technology
9:34and furniture. It's kind of an interesting
9:35decision there. I wonder why it's doing
9:38this vertically. If I go to office supplies
9:40, it
9:40does it horizontally. It's an interesting,
9:44interesting habit. But I hope you've
9:46enjoyed
9:46this very brief video. It's a very, very
9:48brief sort of showcase of this. I'm
9:50actually quite
9:50interested to see what people come up with
9:52this because you can do lots of really
9:53interesting
9:54things like even parameters can go into the
9:56left and the right hand side. So it's going
9:59to be a really nice way to sort of see what
10:01creativity people in the community come up
10:03with. So definitely stay tuned into that.
10:05If you're reading this video, watching this
10:06video, sorry, be sure to just go on Google
10:09and Google what other people have written
10:11about this particular feature because I'm
10:13sure you'll find some great content. That's
10:15pretty much the video. It's a slightly
10:17longer one than usual, 10 minutes. But
10:19thank you
10:19for watching and I'll catch you in the next
10:21video. Be sure to check out some of the
10:23other
10:23videos in the playlist and also subscribe,
10:26like the video or if you don't like it,
10:28dislike.
10:28That's also fine. We're happy to get your
10:30feedback for that. Thank you very much.
10:32Thank you very much.
Tableau release note: Use the IN operator in calculations to test whether a specified value matches any value in a list of comma-separated values, a set, or combined fields like City/Region combinations. The IN operator supports string, numeric, date and boolean data types.This video is part of a larger playlist on Tableau 2020.3. Be sure to check out other videos in the playlist and subscribe for more content.https://tableautim.com -------Join my Discord Server. https://discord.gg/shBuxXr it’s a little sparse at the moment but hang in there.