hey everyone in this video we will take a look at using variables in dbt now just like most
other programming languages or tools the ability to store information in a variable and reuse it
is pretty critical and dbt is no different in the fact that they offer ways to do this
it's pretty straightforward so i'll just walk you through how to do it
and then from there it's really up to you on how you want to use it and the different ways
you want to implement this on your end so as it says here variables can be defined in two ways
in the dbt project yaml file or on the command line so the most common way you'll likely see
this is in the dbt project itself simply add this vars section here give the name of the variable
and its value and just like many other things in dbt you can dictate its level of accessibility
depending on you know how you want to traverse down your project so in this case you know it's
only accessible to resources in this project in this package etc so let's go ahead and add one
let's see how this looks so if we hop into our project here let's add let's just add it up here
and what we'll do here is stick with the same data that we've been working with for most of these
videos which is the nba team list and in this case we'll add a variable for current champion and at
the time of this recording in the great year of 2020 the champions are the los angeles lakers
shout out to lebron james what we'll do here is we'll try to use this variable in our actual
sql file to create some sort of column that says you know is champion and it'll be based on this
variable let's go back into team details here's our current model
syntax here we'll create a new column and we'll call this if teams.team
equals and then we'll say the champion and let's look back at how we reference this like this
equals current champion so it's going to bring in that value here lakers
if the team is the current champion true else false as is champion now a way we could actually
simplify this is to just say this because what this will do is say it'll evaluate this
expression if it's true it'll say true if it's not true it'll return false a little cleaner
way to write that so let's so now let's save and run this vt run model flag example team details
we'll run this and then we'll take a look at the compiled code to show you how that was passed
through so now this actually failed because of the quotations that we have around here
so we actually need to update update this here so we can get rid of quotations here instead of
leaving it just like this what we'll actually need to do is switch these quotations around
so that it recognizes it as a string so let's save now let's let's let's rerun this
okay great so now we can see this ran successfully if we look at compiled code we can see it passed
in this value of lakers from our variables so that worked correctly and we can see that
this compiled code looks correct so let's take a look at our snowflake environment
we'll refresh and look at the actual table we see that the column is here and when we preview
this looks correct so we have lakers marked as champion meaning it recognized the variable
it compiled the code correctly and deployed it correctly so now the other way that you
can do this as it mentioned is actually through the command line
and we can set these variables here through this vars flag and it mentions that this will be
in order of precedence this is number one so even if we've set something in the project
if we set this flag this will actually override it so let's try that let's take this flag and
we're going to run this again but this time let's change this value so the key here is going to be
the name of our variable so that's current champion and instead of lakers here let's change
this to my favorite team the 76ers let's run that we would expect this to override this value so
that in snowflake now this this record would be marked as is champion not lakers okay so this ran
successfully this is our original one is champion let's refresh this preview and we can see now
the 76ers are marked as a champion so that's how you would use this in the command line and i think
this is more useful if you're doing something more automated and maybe you have some automated
workflows where you can hard code and you want to set this variable differently depending on
certain scenarios you could do that here a little bit easier than you could if you were to just
hard code it in the project itself and where it stays pretty static so i hope
you found this helpful thank you again for watching and i'll see you at the next video