Metabase Expressions
Metabase Expressions
Joining data Custom columns. You could use = [Subtotal] / [Quantity] to create a new column, which you could name “Item price”. List of expressions
The SQL editor Filters. The expression = contains([comment], "Metabase") would filter for rows where the comment field contained the word “Metabase”.
SQL parameters Summaries. Also known as metrics or aggregations. = Share([Total] > 50) would return the percentage of orders with totals greater than 50 dollars.
Referencing models and saved
questions This page covers the basics of expressions. You can check out a full list of expressions in Metabase, or walk through a tutorial that shows you how you can use
custom expressions in the notebook editor.
SQL snippets
Actions Functions
Functions, by contrast, do something to each value in a column, like searching for a word in each value ( contains ), rounding each value up to the nearest integer (the
Organization ceil function), and so on.
People
Permissions
Basic mathematical operators
Use + , - , * (multiply), / (divide) on numeric columns with numeric values, like integers, floats, and double. You can use parentheses, ( and ) , to group parts of your
Embedding expression.
Configuration For example, you could create a new column that calculates the difference between the total and subtotal of a order: = [Total] - [Subtotal] .
To do math on timestamp columns, you can use Date functions like dateDiff.
Tools
Cloud
Conditional operators
Metabase API
AND , OR , NOT , > , >= (greater than or equal to), < , <= (less than or equal to), = , != (not equal to).
Troubleshooting For example, you could create a filter for customers from California or Vermont: = [State] = "CA" OR [State] = "VT" .
Developer guide
Filter expressions are different in that they must return a Boolean value (something that’s either true or false). For example, you could write [Subtotal] + [Tax]
< 100 . Metabase would look at each row, add its subtotal and tax, the check if that sum is greater than 100. If it is, the statement evaluates as true, and Metabase
will include the row in the result. If instead you were to (incorrectly) write [Subtotal] + [Tax] , Metabase wouldn’t know what to do, as that expression doesn’t
evaluate to true or false.
You can use functions inside of the conditional portion of the CountIf and SumIf aggregations, like so: CountIf( round([Subtotal]) > 100 OR floor([Tax])
< 10 ) .
Example:
This expression would return rows where Created At is between January 1, 2020 and March 31, 2020, or where Received At is after December 25, 2019.
List of expressions
See a full list of expressions.
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
© Metabase 2024
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Aggregations
Installation List of expressions
Functions
For an introduction to expressions, check out the overview of custom expressions.
Databases Logical functions
Aggregations
Questions Math functions
Average
String functions
Questions overview Count
Date functions
Asking questions CountIf
Limitations
CumulativeCount
Visualizing data
CumulativeSum
Custom expressions
Distinct
List of expressions
Max
Joining data
Median
The SQL editor
Min
SQL parameters
Percentile
Referencing models and saved
questions Share
SQL snippets StandardDeviation
SQL snippet folder permissions Sum
Alerts Variance
between
Dashboards
case
isnull
Actions
notnull
Organization Math functions
abs
People
ceil
Permissions exp
floor
Embedding
log
Configuration power
round
Tools
sqrt
Cloud String functions
concat
Metabase API
contains
Troubleshooting
doesNotContain
isempty
Paid features
ltrim
length
lower
notempty
regexextract
replace
rtrim
startsWith
substring
trim
upper
Date functions
convertTimezone
datetimeAdd
datetimeDiff
datetimeSubtract
day
hour
interval
minute
month
now
quarter
relativeDateTime
second
week
weekday
year
Database limitations
Aggregations
Aggregation expressions take into account all values in a field. They can only be used in the Summarize section of the query builder.
Average
Returns the average of the values in the column.
Syntax: Average(column)
Example: Average([Quantity]) would return the mean for the Quantity field.
Count
Returns the count of rows (also known as records) in the selected data.
Syntax: Count
CountIf
Only counts rows where the condition is true.
Syntax: CountIf(condition) .
Example: CountIf([Subtotal] > 100) would return the number of rows where the subtotal were greater than 100.
CumulativeCount
The additive total of rows across a breakout.
Syntax: CumulativeCount .
Example: CumulativeCount .
CumulativeSum
The rolling sum of a column across a breakout.
Syntax: CumulativeSum(column) .
Example: CumulativeSum([Subtotal]) .
Distinct
The number of distinct values in this column.
Syntax: Distinct(column) .
Distinct([Last Name]) . Returns the count of unique last names in the column. Duplicates (of the last name “Smith” for example) are not counted.
Max
Returns the largest value found in the column.
Syntax: Max(column) .
Example: Max([Age]) would return the oldest age found across all values in the Age column.
Median
Returns the median value of the specified column.
Syntax: Median(column) .
Example: Median([Age]) would find the midpoint age where half of the ages are older, and half of the ages are younger.
Databases that don’t support median : SQLite, Vertica, SQL server, MySQL. Presto only provides approximate results.
Min
Returns the smallest value found in the column.
Syntax: Min(column) .
Example: Min([Salary]) would find the lowest salary among all salaries in the Salary column.
Percentile
Returns the value of the column at the percentile value.
Example: Percentile([Score], 0.9) would return the value at the 90th percentile for all values in that column.
Databases that don’t support percentile : H2, MySQL, SQL Server, SQLite, Vertica. Presto only provides approximate results.
Share
Returns the percent of rows in the data that match the condition, as a decimal.
Syntax: Share(condition)
Example: Share([Color] = "Blue") would return the number of rows with the Color field set to Blue , divided by the total number of rows.
StandardDeviation
Calculates the standard deviation of the column, which is a measure of the variation in a set of values. Low standard deviation indicates values cluster around the
mean, whereas a high standard deviation means the values are spread out over a wide range.
Syntax: StandardDeviation(column)
Example: StandardDeviation([Population]) would return the SD for the values in the Population column.
Sum
Adds up all the values of the column.
Syntax: Sum(column)
Example: Sum([Subtotal]) would add up all the values in the Subtotal column.
SumIf
Sums up the specified column only for rows where the condition is true.
Example: SumIf([Subtotal], [Order Status] = "Valid") would add up all the subtotals for orders with a status of “Valid”.
Variance
Returns the numeric variance for a given column.
Syntax: Variance(column)
Example: Variance([Temperature]) will return a measure of the dispersion from the mean temperature for all temps in that column.
Functions
Function expressions apply to each individual value. They can be used to alter or filter values in a column, or create new, custom columns.
Logical functions
Logical functions determine if a condition is satisfied or determine what value to return based on a condition.
between
Checks a date or number column’s values to see if they’re within the specified range.
Example: between([Created At], "2019-01-01", "2020-12-31") would return rows where Created At date fell within the range of January 1, 2019 and
December 31, 2020.
Related: interval.
case
Tests an expression against a list of cases and returns the corresponding value of the first matching case, with an optional default value if nothing else is met.
Example: case([Weight] > 200, "Large", [Weight] > 150, "Medium", "Small") If a Weight is 250, the expression would return “Large”. In this case, the
default value is “Small”, so any Weight 150 or less would return “Small”.
coalesce
Looks at the values in each argument in order and returns the first non-null value for each row.
Example: coalesce([Comments], [Notes], "No comments") . If both the Comments and Notes columns are null for that row, the expression will return the string
“No comments”.
isnull
Returns true if the column is null.
Syntax: isnull(column)
Example: isnull([Tax]) would return true if no value were present in the column for that row.
notnull
Returns true if the column contains a value.
Syntax: notnull(column)
Example: notnull([Tax]) would return true if there is a value present in the column for that row.
Math functions
Math functions implement common mathematical operations.
abs
Returns the absolute (positive) value of the specified column.
Syntax: abs(column)
ceil
Rounds a decimal up (ceil as in ceiling).
Syntax: ceil(column) .
exp
Returns Euler’s number, e, raised to the power of the supplied number. (Euler sounds like “Oy-ler”).
Syntax: exp(column) .
Related: power.
floor
Rounds a decimal number down.
Syntax: floor(column)
Example: floor([Price]) . If the Price were 1.99, the expression would return 1.
log
Returns the base 10 log of the number.
Syntax: log(column) .
Example: log([Value]) .
power
Raises a number to the power of the exponent value.
Example: power([Length], 2) . If the length were 3 , the expression would return 9 (3 to the second power is 3*3).
Related: exp.
round
Rounds a decimal number either up or down to the nearest integer value.
Syntax: round(column) .
Example: round([Temperature]) . If the temp were 13.5 degrees centigrade, the expression would return 14 .
Example: round([Temperature] * 10) / 10 . If the temp were 100.75 , the expression would return 100.8 .
sqrt
Returns the square root of a value.
Syntax: sqrt(column) .
Example: sqrt([Hypotenuse]) .
Related: Power.
String functions
String functions manipulate or validate string data.
concat
Combine two or more strings together.
Example: concat([Last Name], ", ", [First Name]) would produce a string of the format “Last Name, First Name”, like “Palazzo, Enrico”.
contains
Checks to see if string1 contains string2 within it.
Performs case-sensitive match by default. You can pass an optional parameter "case-insensitive" to perform a case-insensitive match.
If Status were “Classified”, the expression would return true . If the Status were “classified”, the expression would return false , because the case does not match.
doesNotContain
Checks to see if string1 contains string2 within it.
Performs case-sensitive match by default. You can pass an optional parameter "case-insensitive" to perform a case-insensitive match.
Example: doesNotContain([Status], "Class") . If Status were “Classified”, the expression would return false .
endsWith
Returns true if the end of the text matches the comparison text.
Performs case-sensitive match by default. You can pass an optional parameter "case-insensitive" to perform a case-insensitive match.
isempty
Returns true if a string column contains an empty string or is null. Calling this function on a non-string column will cause an error. You can use isnull for non-string
columns.
Syntax: isempty(column)
Example: isempty([Feedback]) would return true if Feedback was an empty string ( '' ) or did not contain a value.
ltrim
Removes leading whitespace from a string of text.
Syntax: ltrim(text)
Example: ltrim([Comment]) . If the comment were " I'd prefer not to" , ltrim would return "I'd prefer not to" .
length
Returns the number of characters in text.
Syntax: length(text)
Example: length([Comment]) . If the comment were “wizard”, length would return 6 (“wizard” has six characters).
lower
Returns the string of text in all lower case.
Syntax: lower(text) .
Example: lower([Status]) . If the Status were “QUIET”, the expression would return “quiet”.
Related: upper.
notempty
Returns true if a string column contains a value that is not the empty string. Calling this function on a non-string column will cause an error. You can use notnull on
non-string columns.
Syntax: notempty(column)
Example: notempty([Feedback]) would return true if Feedback contains a value that isn’t the empty string ( '' ).
regexextract
Extracts matching substrings according to a regular expression.
replace
Replaces all occurrences of a search text in the input text with the replacement text.
rtrim
Removes trailing whitespace from a string of text.
Syntax: rtrim(text)
Example: rtrim([Comment]) . If the comment were “Fear is the mindkiller. “, the expression would return “Fear is the mindkiller.”
startsWith
Returns true if the beginning of the text matches the comparison text. Performs case-sensitive match by default. You can pass an optional parameter "case-
insensitive" to perform a case-insensitive match.
Example: startsWith([Course Name], "Computer Science") would return true for course names that began with “Computer Science”, like “Computer Science
101: An introduction”.
It would return false for “Computer science 201: Data structures” because the case of “science” does not match the case in the comparison text.
startsWith([Course Name], "Computer Science", "case-insensitive") would return true for both “Computer Science 101: An introduction” and “Computer
science 201: Data structures”.
substring
Returns a portion of the supplied text, specified by a starting position and a length.
Example: substring([Title], 1, 10) returns the first 10 letters of a string (the string index starts at position 1).
trim
Removes leading and trailing whitespace from a string of text.
Syntax: trim(text)
Example: trim([Comment]) will remove any whitespace characters on either side of a comment.
upper
Returns the text in all upper case.
Syntax: upper(text) .
Date functions
Date functions manipulate, extract, or create date and time values.
convertTimezone
Shifts a date or timestamp value into a specified time zone.
Example: convertTimezone("2022-12-28T12:00:00", "Canada/Pacific", "Canada/Eastern") would return the value 2022-12-28T09:00:00 , displayed as
December 28, 2022, 9:00 AM .
datetimeAdd
Adds some unit of time to a date or timestamp value.
Example: datetimeAdd("2021-03-25", 1, "month") would return the value 2021-04-25 , displayed as April 25, 2021 .
datetimeDiff
Returns the difference between two datetimes in some unit of time. For example, datetimeDiff(d1, d2, "day") will return the number of days between d1 and
d2 .
datetimeSubtract
Subtracts some unit of time from a date or timestamp value.
Example: datetimeSubtract("2021-03-25", 1, "month") would return the value 2021-02-25 , displayed as February 25, 2021 .
day
Takes a datetime and returns the day of the month as an integer.
hour
Takes a datetime and returns the hour as an integer (0-23).
interval
Checks a date column’s values to see if they’re within the relative range.
Related: between.
minute
Takes a datetime and returns the minute as an integer (0-59).
month
Takes a datetime and returns the month number (1-12) as an integer.
now
Returns the current date and time using your Metabase report timezone.
Syntax: now .
quarter
Takes a datetime and returns the number of the quarter in a year (1-4) as an integer.
relativeDateTime
Gets a timestamp relative to the current time.
Example: [Orders → Created At] < relativeDateTime(-30, "day") will filter for orders created over 30 days ago from current date.
second
Takes a datetime and returns the number of seconds in the minute (0-59) as an integer.
timeSpan
Gets a time interval of specified length.
Example: [Orders → Created At] + timeSpan(7, "day") will return the date 7 days after the Created At date.
week
Takes a datetime and returns the week as an integer.
mode: Optional.
ISO: (default) Week 1 starts on the Monday before the first Thursday of January.
Instance: Week 1 starts on Jan 1. All other weeks start on the day defined in your Metabase localization settings.
weekday
Takes a datetime and returns an integer (1-7) with the number of the day of the week.
Syntax: weekday(column)
Example:
case(
weekday([Created At]) = 1, "Sunday",
weekday([Created At]) = 2, "Monday",
weekday([Created At]) = 3, "Tuesday",
weekday([Created At]) = 4, "Wednesday",
weekday([Created At]) = 5, "Thursday",
weekday([Created At]) = 6, "Friday",
weekday([Created At]) = 7, "Saturday")
year
Takes a datetime and returns the year as an integer.
Limitations
Aggregation expressions can only be used in the Summarize section of the query builder.
Functions that return a boolean value, like isempty or contains, cannot be used to create a custom column. To create a custom column based on one of these
functions, you must combine them with another function, like case .
For example, to create a new custom column that contains true if [Title] contain 'Wallet' , you can use the custom expression
Database limitations
Limitations are noted for each aggregation and function above, and here there are in summary:
SQLite: log , Median , Percentile , power , regexextract , StandardDeviation , sqrt and Variance
Additionally, Presto only provides approximate results for Median and Percentile .
If you’re using or maintaining a third-party database driver, please refer to the wiki to see how your driver might be impacted.
Check out our tutorial on custom expressions in the query builder to learn more.
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
© Metabase 2024
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Metabase’s two core concepts are questions and their corresponding answers. To ask a question in Metabase, click the + New button in the upper right of the main The query builder
Databases
navigation bar, and select either:
Picking data
Questions Question Joining data
SQL query
Questions overview Filtering
Asking questions This page covers how to ask a question using Metabase’s graphical query builder, the “Question” option. Filter types
Custom expressions Creating a new question with the query builder Summarizing and grouping by
List of expressions From the + New dropdown, select Question, then pick your starting data: Grouping your metrics
Data modeling
The query builder Question version history
Once you select your data, Metabase will take you to the query builder. Say you selected Raw data > Sample database > Orders, then you’ll see something like this: Further reading
Actions
Organization
People
Permissions
Embedding
Configuration
Tools
Cloud
Metabase API
Troubleshooting
Developer guide
Paid features
This is the query builder’s notebook editor. It has three default steps.
Picking data
Filtering
To the right of each completed step is a Preview button (looks like a Play button - a triangle pointing to the right) that shows you the first 10 rows of the results of
your question up to that step.
Picking data
The data section is where you select the data you want to work with. Here you’ll pick a model, a table from a database, or a saved question. You can click on a table to
select which columns you want to include in your results. See also adding or removing columns in a table.
Joining data
You can also select multiple tables from the same database by joining them.
Filtering
Filtering just means narrowing things down based on certain criteria. You’re probably already familiar with filtering when looking for something online, like when
shopping. Maybe you only want to see olive-colored pants, or books where the author’s last name is “Borges,” or pictures of people wearing olive-colored pants
reading Jorge Luis Borges.
When you add a filter step, you can select one or more columns to filter on. Depending on the data type of the column you pick, you’ll get different filter types, like a
calendar for date columns.
You can add subsequent filter steps after each summarize step. This lets you do things like summarize by the count of rows per month, and then add a filter on the
count column to only include rows where the count is greater than 100. (This is basically like a SQL HAVING clause.)
Once you’re happy with your filter, click Add filter, and visualize your results. Your data will be updated with the filter applied.
If you want to edit your filter, just click the little purple filter at the top of the screen. If you click on the X, you’ll remove your filter. You can add as many filters as you
need.
Filter types
Broadly speaking, there are three types of columns, each with their own set of filtering options:
Numeric columns let you add filters to only include rows in your table where this number is between two specific values, or is greater or less than a specific value,
or is exactly equal to something.
Text or category columns let you specify that you only want to include data where this column is or isn’t a specific option, or you can exclude empty cells in that
column.
Date columns give you a lot of options to filter by specific date ranges, relative date ranges, and more.
Filter modal
When viewing a table or chart, clicking on the Filter will bring up the filter modal:
Here you can add multiple filters to your question in one go. Filter options will differ depending on the field type. Any tables linked by foreign keys will be displayed in
the left tab of the modal. When you’re done adding filters, hit Apply filters to rerun the query and update its results. To remove all the filters you’ve applied, click on
Clear all filters in the bottom left of the filter modal. Any filters you apply here will show up in the notebook editor, and vice versa.
Filtering by date
One important thing to understand when filtering on a date column is the difference between specific and relative dates:
Specific dates are things like November 1, 2010, or June 3 – July 12, 2017; they always refer to the same date(s).
Relative dates are things like “the past 30 days,” or “the current week;” as time passes, the dates these options refer to change. Relative dates are a useful way to
set up a filter on a question so that it stays up-to-date by showing you, for example, how many people visited your website in the last 7 days. You can also click on
the … to specify a Starting from option, which lets you offset the relative date range. For example, you could set the range as the “Previous 7 days, starting from 2
days ago”.
Filtering by a segment
If your Metabase administrators have created special named filters for the table you’re viewing, they’ll appear at the top of the filter dropdown in purple text with a
star next to them. These are called Segments, and they’re shortcuts to a combination of filters that are commonly used in your organization. They might be called
things like “Active Users,” or “Most Popular Products.”
Filters with OR
OR
If you have a more complex filter you’re trying to express, you can pick Custom Expression from the add filter menu to create a filter expression. You can use
comparison operators like greater than, > , or less than , < , as well as spreadsheet-like functions. For example, [Subtotal] > 100 OR median([Age]) < 40 . Learn
more about writing expressions or skip right to the list of expressions.
When we have a question like “how many people downloaded our app each day last week?”, we’re asking for a summary of the data. A summary is usually made up of
two parts: one or more numbers we care about (called a “metric” in data-speak), and how we want to see that number grouped or broken out. To answer that example
question of “How many people downloaded our app each day last week?”
The metric would be the count of people who downloaded the app (the count of rows).
There are two common ways you’ll tend to summarize your data:
And a lot of the time, you’ll then group that metric by:
Time
Place
Category
Adding a summarize step lets you choose how to aggregate the data from the previous step. You can pick one or more metrics, and optionally group those metrics by
one or more dimensions (columns). When picking your metrics you can choose from basic functions like sum , average , and count ; or you can pick a common metric
defined by an admin; or you can create a custom expression by writing a formula.
Count of rows: the total of number of rows in the table, after any filters have been applied. If you’re looking at your Orders table and want to know how many
orders were placed with a price greater than $40, you’d filter by “Price greater than 40,” and then select Count of rows , because you want Metabase to count
how many orders matched your filter.
Number of distinct values of…: the number of unique values in all the cells of a single column. This is useful when trying to find out things like how many different
types of products were sold last month (not how many were sold in total).
Cumulative sum of…: This gives you a running total for a specific column. In order for this metric to be useful you’ll need to group it by a date column to see it
across time.
Cumulative count of rows: This gives you a running total of the number of rows in the table over time. Just like Cumulative sum of… , you’ll need to group this by
a date column in order for it to be useful.
Standard deviation of …: A number which expresses how much the values of a column vary, plus or minus, from the average value of that column.
If you summarize and add a grouping you can then summarize again. You can also add steps to filter and/or join in between. For example, your first summarization
step could be to get the count of orders per month, and you could then add a second summarization step to get the average monthly order total by selecting the
Average of… your count column.
You can also add metrics and groupings on the results page in a sidebar: the top of the sidebar where you pick the number (“metric”) you want to see, and the part
below is where you pick how to group that number (or how to “break it out”).
If your admins have created any named metrics that are specific to your company or organization, they will be in this dropdown under the Common Metrics section.
These might be things like your company’s official way of calculating revenue.
When you click on a different grouping column than the one you currently have selected, the grouping will switch to use that column instead. But if you want to add
an additional grouping, just click the plus (+) icon on the right side of the column. To remove a grouping, click on the X icon.
Some grouping columns will give you the option of choosing how big or small to make the groupings. So for example, if you’ve picked a Date column to group by, you
can click on the words by month to change the grouping to day, week, hour, quarter, year, etc. If you’re grouping by a numeric column, like age, Metabase will
automatically “bin” the results, so you’ll see your metric grouped in age brackets, like 0–10, 11–20, 21–30, etc. Just like with dates, you can click on the current
binning option to change it to a specific number of bins. It’s not currently possible to choose your own ranges for bins, though.
Once you’re done setting your metrics and groupings, click Visualize to see your results in all their glory.
Drill-through menu
You can also click through questions to explored the data in greater detail.
The drill-through menu will present different options depending on what you click on. You can then optionally save that exploration as a new question. The drill-
through menu is only available for questions built using the query builder. For more on how drill-through works, check out Creating interactive charts.
For example, you could do Average(sqrt[FieldX]) + Sum([FieldY]) or Max(floor([FieldX] - [FieldY])) , where FieldX and FieldY are fields in the
currently selected table. Learn more about writing expressions.
Custom columns are helpful when you need to create a new column based on a calculation, such as subtracting the value of one column from another, or extracting a
portion of an existing text column. Custom columns that you add aren’t permanently added to your table; they’ll only be present in the given question.
You can use the following math operators in your formulas: + , – , * (multiplication), and / (division), along with a whole host of spreadsheet-like functions. You can
also use parentheses to clarify the order of operations.
Sorting results
The sorting step lets you pick one or more columns to sort your results by. For each column you pick, you can also choose whether to sort ascending or descending;
just click the arrow to change from ascending (up arrow) to descending (down arrow).
Feel free to play around with any saved question, as you won’t have any effect on the existing question. When you hit Save on the question, you can choose either to
save as a new question (the default), or you can overwrite the existing question you started from.
If you find yourself using the same saved question as a starting point for multiple questions, you may want to turn it into a model to let others know it’s a good
starting place.
See History.
Further reading
Visualize results.
Sharing answers.
Asking questions
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
© Metabase 2024
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Questions overview
Asking questions
Visualizing data
Custom expressions
List of expressions
Joining data
SQL parameters
You can join data to combine your current data with another table, or even with a saved question.
Referencing models and saved
questions
After you click on the Join Data button to add a join step, you’ll need to pick the data (from the same database) that you want to join. You can only pick tables and
SQL snippets saved questions that are from the same database as your starting data.
SQL snippet folder permissions
Sharing answers
Alerts
Public sharing
Exporting data
Dashboards
Data modeling
Actions
Organization
People
Permissions
Embedding
Next, you’ll need to pick the columns you want to join on. This means you pick a column from the first table, and a column from the second table, and the join will
stitch rows together where the value from the first column is equal to the value in the second column. A very common example is to join on an ID column in each
Configuration
table, so if you happened to pick a table to join on where there is a foreign key relationship between the tables, Metabase will automatically pick those corresponding
ID columns for you. At the end of your join step, there’s a Columns button you can click to choose which columns you want to include from the joined data.
Tools
By default, Metabase will do a left outer join, but you can click on the Venn diagram icon to select a different type of join. Not all databases support all types of joins,
Cloud so Metabase will only display the options supported by the database you’re using.
Left outer join: select all records from Table A, along with records from Table B that meet the join condition, if any.
Troubleshooting
Right outer join: select all records from Table B, along with records from Table A that meet the join condition, if any.
Developer guide Inner join: only select the records from Table A and B where the join condition is met.
Full outer join: select all records from both tables, whether or not the join condition is met.
Paid features
A left outer join example: If Table A is Orders and Table B is Customers, and assuming you do a join where the customer_id column in Orders is equal to the ID
column in Customers, when you do a left outer join your results will be a full list of all your orders, and each order row will also display the columns of the customer
who placed that order. Since a single customer can place many orders, a given customer’s information might be repeated many times for different order rows. If there
isn’t a corresponding customer for a given order, the order’s information will be shown, but the customer columns will just be blank for that row.
Further reading
Joins in Metabase
Type of joins
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation DatetimeDiff
Calculating age
datetimeDiff gets the amount of time between two datetime values, using the specified unit of time. Note that the difference is calculated in whole units (see the
Databases
example below). Accepted data types
Questions Limitations
S Y NTAX EXAMPL E
Related functions
Dashboards
datetimeDiff(datetime1, datetime2, unit) datetimeDiff("2022-02-01", "2022-03-01", Further reading
"month")
Data modeling
Actions Gets the difference between two datetimes (datetime2 minus datetime 1) using the specified unit 1
of time.
Organization
People
Permissions
Parameters
Embedding
datetime1 and datetime2 can be:
Configuration
The name of a timestamp column,
a string in the format "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SS" (as shown in the example above).
Cloud
unit can be any of:
Metabase API
“year”
Troubleshooting “quarter”
“month”
Developer guide
“week”
Paid features “day”
“hour”
“minute”
“second”
Calculating age
Let’s say you’re a cheesemaker, and you want to keep track of your ripening process:
String
Number
Timestamp
Boolean
JSON
We use “timestamp” and “datetime” to talk about any temporal data type that’s supported by Metabase. For more info about these data types in Metabase, see
Timezones.
If your timestamps are stored as strings or numbers in your database, an admin can cast them to timestamps from the Table Metadata page.
Limitations
datetimeDiff is currently unavailable for the following databases:
Druid
Google Analytics
Related functions
This section covers functions and formulas that work the same way as the Metabase datetimeDiff expression, with notes on how to choose the best option for your
use case.
SQL
Spreadsheets
Python
SQL
When you run a question using the query builder, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
Some databases, such as Snowflake and BigQuery, support functions like DATEDIFF or DATE_DIFF . For more info, check out our list of common SQL reference guides.
Spreadsheets
If our cheese sample data is in a spreadsheet where “Aging Start” is in column B and “Aging End” is in column C:
Yes, DATEDIF looks a bit wrong, but the spreadsheet function really is DATEDIF() with one “f”, not DATEDIFF() .
Python
Assuming the cheese sample data is in a pandas dataframe column called df , you can subtract the dates directly and use numpy ’s timedelta64 to convert the
difference to months:
is equivalent to
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation CountIf
Multiple conditions
CountIf counts the total number of rows in a table that match a condition. CountIf counts every row, not just unique rows.
Databases Conditional counts by group
Syntax: CountIf(condition) .
Questions Accepted data types
Example: in the table below, CountIf([Plan] = "Basic") would return 3. Related functions
Dashboards
Further reading
ID PLAN
Data modeling
1 Basic
Actions
2 Basic
Organization
People 3 Basic
Permissions
4 Business
Embedding
5 Premium
Configuration
Tools
Cloud
Metabase API Aggregations like CountIf should be added to the query builder’s Summarize menu > Custom Expression (scroll down in the menu if needed).
Troubleshooting
Developer guide
Parameters
Paid features
CountIf accepts a function or conditional statement that returns a boolean value ( true or false ).
Multiple conditions
We’ll use the following sample data to show you CountIf with required, optional, and mixed conditions.
1 Basic true
2 Basic true
3 Basic false
4 Business false
5 Premium true
Required conditions
To count the total number of rows in a table that match multiple required conditions, combine the conditions using the AND operator:
This expression will return 2 on the sample data above (the total number of Basic plans that have an active subscription).
Optional conditions
To count the total rows in a table that match multiple optional conditions, combine the conditions using the OR operator:
Returns 4 on the sample data: there are three Basic plans, plus one Premium plan has an active subscription.
Returns 2 on the sample data: there are only two Basic or Business plans that lack an active subscription.
Tip: make it a habit to put parentheses around your AND and OR groups to avoid making required conditions optional (or vice versa).
1 Basic true
2 Basic true
3 Basic false
4 Business false
5 Premium true
Alternatively, if your Active Subscription column contains null (empty) values that represent inactive plans, you could use:
To view your conditional counts by plan, set the Group by column to “Plan”.
Basic 1
Business 1
Premium 0
Tip: when sharing your work with other people, it’s helpful to use the OR filter, even though the != filter is shorter. The inclusive OR filter makes it easier to
understand which categories (e.g., plans) are included in your conditional count.
String
Number
Timestamp
Boolean
JSON
CountIf accepts a function or conditional statement that returns a boolean value ( true or false ).
Related functions
Different ways to do the same thing, because it’s fun to try new things.
Metabase
case
CumulativeCount
Other tools
SQL
Spreadsheets
Python
case
You can combine Count
Count with case
case:
CountIf([Plan] = "Basic")
The case version lets you count a different column when the condition isn’t met. For example, if you’ve got data from different sources:
1 Basic
B basic
C basic
4 Business D business
5 Premium E premium
To count the total number of Basic plans across both sources, you could create a case expression to:
CumulativeCount
CountIf doesn’t do running counts. You’ll need to combine CumulativeCount with case
case.
And we want to get the running count of active plans like this:
October 2020 2
November 2020 3
You’ll also need to set the Group by column to “Created Date: Month”.
SQL
When you run a question using the query builder, Metabase will convert your query builder settings (filters, summaries, etc.) into a SQL query, and run that query
against your database to get your results.
CountIf([Plan] = "Basic")
If you want to get conditional counts broken out by group, the SQL query:
SELECT
plan,
COUNT(CASE WHEN active_subscription = false THEN id END) AS total_inactive_subscriptions
FROM accounts
GROUP BY
plan
The SELECT part of the SQl query matches the Metabase expression:
The GROUP BY part of the SQL query matches a Metabase Group by set to the “Plan” column.
Spreadsheets
If our sample data is in a spreadsheet where “ID” is in column A, the spreadsheet formula:
=CountIf(B:B, "Basic")
CountIf([Plan] = "Basic")
Python
If our sample data is in a pandas dataframe column called df , the Python code:
len(df[df['Plan'] == "Basic"])
CountIf([Plan] = "Basic")
len(df_filtered.groupby('Plan'))
The Python code above will produce the same result as the Metabase CountIf expression (with the Group by column set to “Plan”).
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation SumIf
Multiple conditions
SumIf adds up the values in a column based on a condition.
Databases Conditional subtotals by group
Syntax: SumIf(column, condition) .
Questions Accepted data types
Example: in the table below, SumIf([Payment], [Plan] = "Basic") would return 200. Related functions
Dashboards
Further reading
PAY M E NT PLAN
Data modeling
100 Basic
Actions
100 Basic
Organization
Permissions
200 Business
Embedding
400 Premium
Configuration
Tools
Cloud
Metabase API Aggregation formulas like sumif should be added to the query builder’s Summarize menu > Custom Expression (scroll down in the menu if needed).
Troubleshooting
Developer guide
Parameters
Paid features
column can be the name of a numeric column, or a function that returns a numeric column.
condition is a function or conditional statement that returns a boolean value ( true or false ), like the conditional statement [Payment] > 100 .
Multiple conditions
We’ll use the following sample data to show you SumIf with required, optional, and mixed conditions.
Required conditions
To sum a column based on multiple required conditions, combine the conditions using the AND operator:
This expression would return 200 on the sample data above: the sum of all of the payments received for Basic Plans in October.
Optional conditions
To sum a column with multiple optional conditions, combine the conditions using the OR operator:
Tip: make it a habit to put parentheses around your AND and OR groups to avoid making required conditions optional (or vice versa).
To view those payments by month, set the Group by column to “Date Received: Month”.
October 200
November 600
Tip: when sharing your work with other people, it’s helpful to use the OR filter, even though the != filter is shorter. The inclusive OR filter makes it easier to
understand which categories (e.g., plans) are included in the sum.
String
Number
Timestamp
Boolean
JSON
See parameters.
Related functions
Different ways to do the same thing, because CSV files still make up 40% of the world’s data.
Metabase
case
CumulativeSum
Other tools
SQL
Spreadsheets
Python
case
You can combine Sum
Sum and case
case:
The case version lets you sum a different column when the condition isn’t met. For example, you could create a column called “Revenue” that:
CumulativeSum
SumIf doesn’t do running totals. You’ll need to combine the CumulativeSum aggregation with the case
case formula.
For example, to get the running total of payments for the Business and Premium plans by month (using our payment sample data):
October 200
November 800
SQL
When you run a question using the query builder, Metabase will convert your query builder settings (filters, summaries, etc.) into a SQL query, and run that query
against your database to get your results.
If our payment sample data is stored in a PostgreSQL database, the SQL query:
SELECT
SUM(CASE WHEN plan = "Basic" THEN payment ELSE 0 END) AS total_payments_basic
FROM invoices
To add multiple conditions with a grouping column, use the SQL query:
SELECT
DATE_TRUNC("month", date_received) AS date_received_month,
SUM(CASE WHEN plan = "Business" THEN payment ELSE 0 END) AS total_payments_business_or_premium
FROM invoices
GROUP BY
DATE_TRUNC("month", date_received)
The SELECT part of the SQl query matches the Metabase SumIf expression:
The GROUP BY part of the SQL query maps to a Metabase Group by column set to “Date Received: Month”.
Spreadsheets
If our payment sample data is in a spreadsheet where “Payment” is in column A and “Date Received” is in column B, the spreadsheet formula:
Python
If our payment sample data is in a pandas dataframe column called df , the Python code:
import datetime as dt
These steps will produce the same result as the Metabase SumIf expression (with the Group by column set to “Date Received: Month”).
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
case checks if a value matches a list of conditions, and returns some output based on the first condition that’s met. Basically, case works the same way as “if… then” Labeling a row based on conditions
Databases
logic, but it’s much nicer to write. from multiple columns
S Y NTAX
People
Embedding Returns the output from the first condition that’s met.
Configuration
Tools
E XA MP LE
Cloud
case(isempty("glass half full"), "empty glass", isnull("glass half full"), "missing glass", "glass half full")
Metabase API
Developer guide
Paid features
6 0-9
18 10-19
31 30-39
57 50+
Power a filter.
For example, if we want to count the unique number of orders for each order date, but only those with a “Shipped” status:
1 2022-04-01 Paid
1 2022-04-03 Shipped
2 2022-05-12 Paid
2 2022-05-12 Cancelled
1. Create the custom expression distinct(case([Status] = "Shipped", [Order ID])) and name it “Total Orders Shipped”.
2022-04-01 1
2022-05-01 0
String
Number
Timestamp
Boolean
JSON
Limitations
All of the outputs must have the same data type.
Avoid::
Do::
Related functions
This section covers functions and formulas that can be used interchangeably with the Metabase case expression, with notes on how to choose the best option for
your use case.
Metabase expressions
Coalesce
Countif
Sumif
Other tools
SQL
Spreadsheets
Python
Coalesce
Using the table from the Coalesce: Consolidating values example:
No notes or comments.
coalesce is much nicer to write if you don’t mind taking the first value when both of your columns are non-blank. Use case if you want to define a specific output for
this case (such as, “I have a note and a comment”).
Countif
Using the table from the Aggregating data example:
1 2022-04-01 Paid
1 2022-04-03 Shipped
2 2022-05-12 Paid
2 2022-05-12 Cancelled
countif(case([Status] = "Shipped"))
countif is equivalent to case when you are counting all rows in the table that meet your conditions. It is not equivalent if you want to count unique rows that meet
your conditions.
Sumif
Using an expanded version of the table from the Aggregating data example:
sumif is equivalent to case when you sum a single column for single condition.
You should use case if you want to sum a second column under a second, separate condition. For example, if you want to sum the Amount column when Status =
“Shipped” and another (hypothetical) column like Refunded Amount when Status = “Refunded”.
SQL
In most cases (unless you’re using a NoSQL database), questions created from the notebook editor are converted into SQL queries that run against your database or
data warehouse. Metabase case expressions are converted into SQL CASE WHEN statements.
SELECT
CASE WHEN "Has Wings" = TRUE AND "Has Face" = TRUE THEN "Bird"
WHEN "Has Wings" = TRUE AND "Has Face" = FALSE THEN "Plane"
WHEN "Has Wings" = FALSE AND "Has Face" = TRUE THEN "Superman"
ELSE "Unknown" END
FROM mystery_sightings
For example, this SQL trick to order bar charts could be written using a Metabase case expression instead.
Spreadsheets
Using the table from the Labeling rows example:
Python
There are many ways to implement conditional logic using Python. We’ll cover the approaches that make sense to convert into Metabase case expressions.
Using the table from the Labeling rows example (and assuming it’s in a dataframe called df ):
numpy select()
conditions = [
(df["has_wings"] == True) & (df["has_face"] == True),
(df["has_wings"] == True) & (df["has_face"] == False),
(df["has_wings"] == False) & (df["has_face"] == True)]
def Identify(df):
if ((df["has_wings"] == True) & (df["has_face"] == True)):
return "Bird"
elif ((df["has_wings"] == True) & (df["has_face"] == False)):
return "Plane"
elif ((df["has_wings"] == False) & (df["has_face"] == True)):
return "Superman"
else:
return "Unknown"
The approaches above are equivalent to the case expression used for Sighting Type:
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
This function is useful when you want to: Creating calculations across
Questions different columns
fill in missing data,
Accepted data types
Dashboards consolidate data from multiple columns, or
Limitations
create calculations across multiple columns.
Data modeling Related functions
People Returns the first non-null value from a list of values. “bananas”
Permissions
Embedding
Configuration
Filling in empty or null values
Tools LE F T_ TAB L E_ C OL RI GH T_TABLE_CO L CCCOOOAAALLLEEESSSCCCEEE((([[[RRRIIIGGGHHHTTT___TTTAAABBBLLLEEE___CCCOOOLLL]]],,, 000)))
Cloud 1 1 1
Metabase API
2 null 0
Troubleshooting
3 null 0
Developer guide
4 4 4
Paid features
Sparse data.
For a more detailed example, see Filling in data for missing report dates.
No notes or comments.
21.00 21.00
4.00 4.00
Calculations in Metabase will return null if any of the input columns are null . This is because null values in your data represent “missing” or “unknown”
information, which isn’t necessarily the same as an amount of “0”. That is, adding 1 + “unknown” = “unknown”.
If you want to treat “unknown” values as zeroes (or some other value that means “nothing” in your data), we recommend using coalesce to wrap the columns used in
your calculations.
String
Number
Timestamp
Boolean
JSON
Limitations
Use the same data types within a single coalesce function. If you want to coalesce values that have different data types:
If you want to use coalesce with JSON or JSONB data types, you’ll need to flatten the JSON objects first. For more information, look up the JSON functions that are
available in your SQL dialect. You can find some common SQL reference guides here.
Related functions
This section covers functions and formulas that can be used interchangeably with the Metabase coalesce expression, with notes on how to choose the best option
for your use case.
Metabase expressions
case
Other tools
SQL
Spreadsheets
Python
All examples use the custom expression and sample data from the Consolidating values example:
No notes or comments.
Case
The Metabase case
case expression
coalesce is much nicer to write if you don’t mind taking the first value when both of your columns are non-blank. Use case
case if you want to define a specific output
(e.g., if you want to return “I have a note and a comment” instead of “I have a note”.).
SQL
In most cases (unless you’re using a NoSQL database), questions created from the notebook editor are converted into SQL queries that run against your database or
data warehouse.
SELECT
COALESCE(notes, comments, "no notes or comments")
FROM
sample_table;
Spreadsheets
If your notes and comments table is in a spreadsheet where “Notes” is in column A, and “Comments” is in column B, then the formula
Alternatively, you may be used to working with a INDEX and MATCH in an array formula if you’re “coalescing” data across three or more columns in a spreadsheet.
Python
Assuming the notes and comments table is in a dataframe called df , the combination of pandas functions combine_first() and fillna()
df['custom_column'] = df['notes'].combine_first(df['comments'])\
.fillna('No notes or comments.')
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Syntax
Installation Isnull
How Metabase handles nulls
isnull checks if a value is a null , a special kind of placeholder that’s used by a database when something is missing or unknown.
Databases Creating a boolean custom column
People
How Metabase handles nulls
Permissions In Metabase tables, null s are displayed as blank cells. Additionally, for string columns, empty strings and strings containing only whitespace characters will be
displayed as blank as well.
Embedding
The table below shows you examples of the output of isnull .
Configuration
M E TA B AS E S H OWS DATABAS E VALU E IIISSSNNNUUULLLLLL(((VVVAAALLLUUUEEE)))
Tools
null true
Cloud
Paid features
*In Oracle and Vertica databases, empty strings are treated as nulls instead.
For example, you can create a new custom column that will contain "Unknown feedback" when the original [Feedback] column is null, and the actual feedback value
when [Feedback] is has a value. The custom expression to do it is:
"" ""
String
Number
Timestamp
Boolean
JSON
Limitations
In Metabase, you must combine isnull with another expression that accepts boolean arguments (i.e., true or false ).
isnull only accepts one value at a time. If you need to deal with blank cells across multiple columns, see the coalesce expression.
If isnull doesn’t seem to do anything to your blank cells, you might have empty strings. Try the isempty
isempty expression instead.
Related functions
This section covers functions and formulas that can be used interchangeably with the Metabase isnull expression, with notes on how to choose the best option for
your use case.
SQL
Spreadsheets
Python
All examples below use the table from the Replacing null values example:
"" ""
SQL
In most cases (unless you’re using a NoSQL database), questions created from the query builder are converted into SQL queries that run against your database or
data warehouse.
Spreadsheets
Spreadsheet #N/A s are the equivalent of database null s (placeholders for “unknown” or “missing” information).
Assuming our sample feedback column is in a spreadsheet where “Feedback” is in column A, then the formula
Python
Numpy and pandas use NaN s or NA s instead of null s.
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
concat joins text data (strings) from two or more columns. Accepted data types
Databases
Related functions
S Y NTAX EXAMPL E
Questions
Further reading
concat(value1, value2, ...) concat("Vienna, ", "Austria")
Dashboards
Actions
Organization
Configuration
Paris France Paris, France
Tools
Kalamata Greece Kalamata, Greece
Cloud
Metabase API
Paid features
String
Number
Timestamp
Boolean
JSON
Related functions
This section covers functions and formulas that work the same way as the Metabase concat expression, with notes on how to choose the best option for your use
case.
SQL
Spreadsheets
Python
SQL
In most cases (unless you’re using a NoSQL database), questions created from the notebook editor are converted into SQL queries that run against your database or
data warehouse.
SELECT
CONCAT(City, ", ", Country) AS "Location"
FROM
richard_linklater_films;
Spreadsheets
If our sample data is in a spreadsheet where “City” is in column A, and “Country” in column B, we can create a third column “Location” like this,
Python
Assuming the sample data is in a dataframe column called df,
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Syntax
Installation Isempty
How Metabase handles empty
isempty checks whether a value in a string column is an empty string ( "" ) or null. Calling isempty on a non-string column would cause an error. strings and null values
Databases
Creating a boolean custom column
Questions
Syntax Replacing empty strings with
another value
Dashboards
Accepted data types
isempty(text column)
Data modeling Limitations
You can use isempty in custom filters, or as the condition for conditional aggregations CountIf
CountIf and SumIf
SumIf. To create a custom column using isempty , you must Related functions
Actions
combine isempty with another function that accepts boolean values, like case
case. Further reading
Organization
People
How Metabase handles empty strings and null values
Permissions In Metabase, columns with string data types will display blank cells for empty strings, strings of whitespace characters, or null values (if the column is nullable in
your database). The table below shows you examples of the output of isempty .
Embedding
M E TA B AS E S H OWS DATABAS E VALU E IIISSSEEEMMMPPPTTTYYY(((VVVAAALLLUUUEEE)))
Configuration
null true
Tools
Troubleshooting
kitten "kitten" false
Developer guide
Paid features
For example, you can create a new custom column that will contain "No feedback" when the original [Feedback] column is empty or null, and the feedback value
when [Feedback] is has a non-empty value. The custom expression to do it is:
String
Number
Timestamp
Boolean
JSON
Limitations
To create a custom column you must combine isempty with another expression that accepts boolean arguments (i.e., true or false ).
isempty only accepts one value at a time. If you need to deal with empty strings from multiple columns, you’ll need to use multiple isempty expressions with the
case expression.
Related functions
This section covers functions and formulas that can be used interchangeably with the Metabase isempty expression, with notes on how to choose the best option for
your use case.
SQL
Spreadsheets
Python
All examples below use the table from the Replacing empty strings example:
SQL
In most cases (unless you’re using a NoSQL database), questions created from the query builder are converted into SQL queries that run against your database or
data warehouse.
Spreadsheets
If our sample feedback column is in a spreadsheet where “Feedback” is in column A, then the formula
Python
Assuming the sample feedback column is in a dataframe column called df["Feedback"] :
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
chart labels, or
Actions
embedding parameters.
Organization
S Y NTAX EXAMPL E
People
regexextract(text, regular_expression) regexextract("regexextract", "ex(.*)")
Permissions
Gets a specific part of your text using a regular expression. “extract”
Embedding
Configuration
Tools
Searching and cleaning text
Cloud
Let’s say that you have web data with a lot of different URLs, and you want to map each URL to a shorter, more readable campaign name.
Metabase API
U RL CAMPAIGN N AME
Troubleshooting
https://www.metabase.com/docs/?utm_campaign=alice alice
Developer guide
https://www.metabase.com/learn/?utm_campaign=neo neo
Paid features
https://www.metabase.com/glossary/?utm_campaign=candy candy
You can create a custom column Campaign Name with the expression:
regexextract([URL], "^[^?#]+\?utm_campaign=(.*)")
Now, you can use Campaign Name in places where you need clean labels, such as filter dropdown menus, charts, and embedding parameters.
String
Number
Timestamp
Boolean
JSON
Limitations
Regex can be a dark art. You have been warned.
regexextract is not supported on H2 (including the Metabase Sample Database), SQL Server, and SQLite.
Related functions
This section covers functions and formulas that work the same way as the Metabase regexextract expression, with notes on how to choose the best option for your
use case.
Metabase expressions
substring
Other tools
SQL
Spreadsheets
Python
Substring
Use substring when you want to search text that has a consistent format (the same number of characters, and the same relative order of those characters).
For example, you wouldn’t be able to use substring to get the query parameter from the URL sample data, because the URL paths and the parameter names both
have variable lengths.
But if you wanted to pull out everything after https://www. and before .com , you could do that with either:
substring([URL], 13, 8)
or
regexextract([URL], "^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/.\n]+)")
SQL
When you run a question using the notebook editor, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
SELECT
url,
SUBSTRING(url, '^[^?#]+\?utm_campaign=(.*)') AS campaign_name
FROM follow_the_white_rabbit
regexextract([URL], "^[^?#]+\?utm_campaign=(.*)")
Spreadsheets
If our sample data is in a spreadsheet where “URL” is in column A, the spreadsheet function
regexextract(A2, "^[^?#]+\?utm_campaign=(.*)")
regexextract([URL], "^[^?#]+\?utm_campaign=(.*)")
Python
Assuming the sample data is in a dataframe column called df ,
regexextract([URL], "^[^?#]+\?utm_campaign=(.*)")
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation Substring
Getting a substring from the left
substring extracts part of some text. This function is useful for cleaning up text (or any value with a string data type) that has a consistent format.
Databases Getting a substring from the right
For example, substring should work well on strings like SKU numbers, ISO codes, and standardized email addresses.
Questions Accepted data types
Actions Extracts part of the text given a starting point (position) and a length (number of characters). “user_id”
Organization
People
Permissions Parameters
Tools
Getting a substring from the left
Cloud
M IS S I ON I D AGENT
Metabase API
19951113006 006
Troubleshooting
20061114007 007
Developer guide
19640917008 008
Paid features
substring([Mission ID], 9, 3)
1 + length([column]) - position_from_right
where position_from_right is the number of characters you want to count from right to left.
M IS S I ON I D AGENT
19951113006 006
20061114007 007
19640917008 008
String
Number
Timestamp
Boolean
JSON
Limitations
substring extracts text by counting a fixed number of characters. If you need to extract text based on some more complicated logic, try regexextract
regexextract.
And if you only need to clean up extra whitespace around your text, you can use the trim
trim, ltrim
ltrim, or rtrim
rtrim expressions instead.
Related functions
This section covers functions and formulas that work the same way as the Metabase substring expression, with notes on how to choose the best option for your use
case.
Metabase expressions
regexextract
Other tools
SQL
Spreadsheets
Python
Regexextract
Use regexextract if you need to extract text based on more specific rules. For example, you could get the agent ID with a regex pattern that finds the last occurrence
of “00” (and everything after it):
substring([Mission ID], 9, 3)
SQL
When you run a question using the notebook editor, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
SELECT
mission_id,
SUBSTRING(mission_id, 9, 3) AS agent
FROM
this_message_will_self_destruct;
substring([Mission ID], 9, 3)
Spreadsheets
If our sample data is in a spreadsheet where “Mission ID” is in column A,
=mid(A2,9,3)
substring([Mission ID], 9, 3)
Python
Assuming the sample data is in a dataframe column called df ,
substring([Mission ID], 9, 3)
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation ConvertTimezone
Creating custom report dates
convertTimezone shifts a timestamp into a specified time zone by adding or subtracting the right interval from the timestamp.
Databases Accepted data types
S Y NTAX EXAMPL E
Questions Limitations
Related functions
convertTimezone(column, target, source) convertTimezone("2022-12-28T12:00:00", "Canada/Pacific",
Dashboards
"Canada/Eastern") Further reading
Data modeling
Shifts a timestamp from the source time zone to the target time Returns the value 2022-12-28T09:00:00 , displayed as December 28, 2022 9:00 AM
Actions zone.
Organization
People
Timestamps and time zones are rather nasty to work with (it’s easy to make mistakes, and difficult to catch them), so you should only try to use convertTimezone if
Permissions the interpretation of your data is sensitive to time-based cutoffs.
For example, if you’re tracking user logins over time, you probably won’t run your business differently if some logins get counted on Mondays instead of Tuesdays.
Embedding
However, if you’re using Metabase to do something precise, like your taxes, you (and the government) will probably care a lot more about the difference between
transactions that occurred on Dec 31 vs. Jan 1.
Configuration
Tools
Parameters
Cloud column can be any of:
Paid features The name of the time zone you want to assign to your column.
source :
Required for columns or expressions with the data type timestamp without time zone .
Optional for columns or expressions with the data type timestamp with time zone .
If Source Time is stored as a timestamp with time zone or a timestamp with offset , you only need to provide the target time zone:
If Source Time is stored as a timestamp without time zone , you must provide the source time zone (which will depend on your database time zone):
It’s usually a good idea to label convertTimezone columns with the name of the target time zone (or add the target time zone to the metadata of a model). We
promise this will make your life easier when someone inevitably asks why the numbers don’t match.
Ask your database admin about timestamp with time zone vs. timestamp without time zone (for more info, see Accepted data types).
P OS S IB LE S OURCE
DE S CRI P TI ON EXAMPL E
TI M E ZON E
Client time zone Time zone where an event happened. A web analytics service might capture data in the local time zone of each
person who visited your website.
Database time Time zone metadata that’s been added to It’s a common database practice to store all timestamps in UTC.
zone timestamps in your database.
No time zone Missing time zone metadata Databases don’t require you to store timestamps with time zone metadata.
Metabase report Time zone that Metabase uses to display Metabase can display dates and times in PST, even if the dates and times are
time zone timestamps. stored as UTC in your database.
For example, say you have a table with one row for each person who visited your website. It’s hard to tell, just from looking at December 28, 2022, 12:00 PM ,
whether the “raw” timestamp is:
stored without time zone metadata (for example, if the website visitor is in HKT, then the timestamp December 28, 2022, 12:00 PM might “implicitly” use Hong
Kong time),
String
Number
Timestamp
Boolean
JSON
We use “timestamp” and “datetime” to talk about any temporal data type that’s supported by Metabase.
If your timestamps are stored as strings or numbers in your database, an admin can cast them to timestamps from the Table Metadata page.
To use convertTimezone without running into errors or pesky undetectable mistakes, you should know that there are a few varieties of timestamp data types:
timestamp with time zone Knows about location. 2022-12-28T12:00:00 AT TIME ZONE 'America/Toronto'
timestamp with offset Knows about the time difference from UTC. 2022-12-28T12:00:00-04:00
Note that the first part of the timestamp is in UTC (same thing as GMT). The time zone or offset tells you how much time to add or subtract for a given time zone.
convertTimezone will work with all three types of timestamps, but the output of convertTimezone will always be a timestamp without time zone .
Limitations
convertTimezone is currently unavailable for the following databases:
Amazon Athena
Druid
Google Analytics
MongoDB
Presto
SparkSQL
SQLite
The Metabase report time zone only applies to timestamp with time zone or timestamp with offset data types. For example:
RAW TI M ES TAM P I N YOU R DATA B A S E DATA TYPE REPORT T IME ZONE DISPLAYED AS
2022-12-28T12:00:00 AT TIME ZONE 'CST' timestamp with time zone ‘Canada/Eastern’ Dec 28, 2022, 7:00 AM
2022-12-28T12:00:00 timestamp without time zone ‘Canada/Eastern’ Dec 28, 2022, 12:00 AM
The Metabase report time zone will not apply to the output of a convertTimezone expression. For example:
2022-12-28T04:00:00
If you use convertTimezone on a timestamp without time zone , make sure to use ‘UTC’ as the source time zone, otherwise the expression will shift your
timestamp by the wrong amount. For example, if our timestamp without time zone is only “implied” to be in CST, we should use ‘UTC’ as the source parameter to
get the same result as above.
For example, if we choose ‘CST’ as the source time zone for a timestamp without time zone :
2022-12-28T10:00:00
displayed in Metabase as
Related functions
This section covers functions and formulas that work the same way as the Metabase convertTimezone expression, with notes on how to choose the best option for
your use case.
SQL
Spreadsheets
Python
SQL
When you run a question using the query builder, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
If our timestamp sample data is a timestamp without time zone stored in a PostgreSQL database:
is the same as the convertTimezone expression with a source parameter set to ‘UTC’:
If source_time is a timestamp with time zone or timestamp with offset (for example, in a Snowflake database), then we don’t need to specify a source time
zone in SQL or in Metabase.
is the same as
Remember that the time zone names depend on your database. For example, Snowflake doesn’t accept most time zone abbreviations (like EST).
Spreadsheets
If our timestamp sample data is in a spreadsheet where “Source Time” is in column A, we can change it to EST by subtracting the hours explicitly:
A1 - TIME(5, 0, 0)
Python
If the timestamp sample data is stored in a pandas dataframe, you could convert the Source Time column to a timestamp object with time zone first(basically
making a timestamp without time zone into a timestamp with time zone ), then use tz_convert to change the time zone to EST:
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation DatetimeAdd
Calculating an end date
datetimeAdd takes a datetime value and adds some unit of time to it. This function is useful when you’re working with time series data that’s marked by a “start” and
Databases
an “end”, such as sessions or subscriptions data. Checking if the current datetime is
within an interval
Questions
S Y NTAX EXAMPL E Accepted data types
Dashboards Limitations
datetimeAdd(column, amount, unit) datetimeAdd("2021-03-25", 1, "month")
Related functions
Data modeling
Takes a timestamp or date value and adds the specified number of time units to it. 2021-04-25 Further reading
Actions
Organization
People
Parameters
Permissions
column can be any of:
Embedding
The name of a timestamp column,
a string in the format "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SS" (as shown in the example above).
Tools
unit can be any of:
Cloud
“year”
“month”
Troubleshooting
“day”
Developer guide “hour”
“minute”
Paid features
“second”
“millisecond”
amount :
CO F F EE O PEN ED ON FINISH BY
Ghost Roaster Giakanja November 27, 2022 December 11, 2022 Yes
Still Fresh Today uses case to check if the current date (now) is between the dates in Opened On and Finish By:
String
Number
Timestamp
Boolean
JSON
We use “timestamp” and “datetime” to talk about any temporal data type that’s supported by Metabase. For more info about these data types in Metabase, see
Timezones.
If your timestamps are stored as strings or numbers in your database, an admin can cast them to timestamps from the Table Metadata page.
Limitations
If you’re using MongoDB, datetimeAdd will only work on versions 5 and up.
Related functions
This section covers functions and formulas that work the same way as the Metabase datetimeAdd expression, with notes on how to choose the best option for your
use case.
Metabase expressions
datetimeSubtract
Other tools
SQL
Spreadsheets
Python
datetimeSubtract
datetimeSubtract and datetimeAdd are interchangeable, since you can use a negative number for amount . It’s generally a good idea to avoid double negatives (such
as subtracting a negative number).
SQL
When you run a question using the query builder, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
Spreadsheets
If our coffee sample data is in a spreadsheet where “Opened On” is in column A with a date format, the spreadsheet function
A:A + 14
Most spreadsheet tools require use different functions for different time units (for example, you’d use a different function to add “months” to a date). datetimeAdd
makes it easy for you to convert all of those functions to a single consistent syntax.
Python
Assuming the coffee sample data is in a pandas dataframe column called df , you can import the datetime module and use the timedelta function:
is equivalent to
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Like so many others, we use cookies to improve your experience on this website. We assume you're OK with it, but you can opt out if you want. Settings Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
Parameters
Installation DatetimeSubtract
Calculating a start date
datetimeSubtract takes a datetime value and subtracts some unit of time from it. You might want to use this function when working with time series data that’s
Databases
marked by a “start” and an “end”, such as sessions or subscriptions data. Checking if the current datetime is
within an interval
Questions
S Y NTAX EXAMPL E Accepted data types
Dashboards Limitations
datetimeSubtract(column, amount, unit) datetimeSubtract("2021-03-25", 1, "month")
Related functions
Data modeling
Takes a timestamp or date value and subtracts the specified number of time units from it. 2021-02-25 Further reading
Actions
Organization
People
Parameters
Permissions
column can be any of:
Embedding
The name of a timestamp column,
a string in the format "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SS" (as shown in the example above).
Tools
unit can be any of:
Cloud
“year”
“month”
Troubleshooting
“day”
Developer guide “hour”
“minute”
Paid features
“second”
“millisecond”
amount :
E VE N T A RRI V E B Y DEPART AT
Dinner November 12, 2022 8:00 PM November 12, 2022 7:30 PM Yes
On My Way uses case to check if the current datetime (now) is between the datetimes in Arrive By and Depart At:
String
Number
Timestamp
Boolean
JSON
We use “timestamp” and “datetime” to talk about any temporal data type that’s supported by Metabase. For more info about these data types in Metabase, see
Timezones.
If your timestamps are stored as strings or numbers in your database, an admin can cast them to timestamps from the Table Metadata page.
Limitations
If you’re using MongoDB, datetimeSubtract will only work on versions 5 and up.
Related functions
This section covers functions and formulas that work the same way as the Metabase datetimeSubtract expression, with notes on how to choose the best option for
your use case.
Metabase expressions
datetimeAdd
Other tools
SQL
Spreadsheets
Python
datetimeAdd
datetimeSubtract and datetimeAdd are interchangeable, since you can use a negative number for amount . We could use either expression for our events example,
but you should try to avoid “double negatives” (such as subtracting a negative number).
SQL
When you run a question using the query builder, Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query
against your database to get your results.
Spreadsheets
Assuming the events sample data is in a spreadsheet where “Arrive By” is in column A with a datetime format, the spreadsheet function
A:A - 30/(60*24)
Most spreadsheets require you to use different calculations for different time units (for example, you’d need to use a different calculation to subtract “days” from a
date). datetimeSubtract makes it easy for you to convert all of those functions to a single consistent syntax.
Python
If our events sample data is in a pandas dataframe column called df , you can import the datetime module and use the timedelta function:
is equivalent to
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies
Upcoming Tour of Metabase advanced Register Now ×
Product Features Documentation Resources Pricing Log in Get started
now returns the current datetime using your Metabase report timezone. Data types
Databases
Limitations
Questions
Creating conditional logic using the current date or time Related functions
Actions
Draft November 1, 2022, 12:00:00 November 30, 2022, 12:00:00 In progress
Organization
Review November 15, 2022, 12:00:00 November 19, 2022, 12:00:00 Needs extension
People
Edit November 22, 2022, 12:00:00 November 22, 2022, 12:00:00 DUE RIGHT NOW!
Permissions
Embedding
Tools
now >= [Start] AND now < [Deadline]
Cloud
To check if you need to ask for an extension:
Metabase API
Developer guide
If you’re looking for an adrenaline rush (and you have real-time data), you can flag the tasks that are due right this second:
Paid features
now = [Deadline]
To set up the Status column that combines all three situations above, you’d wrap everything in a case expression:
Data types
DATA T Y PE RET URNED BY NNNOOOWWW
String
Number
Timestamp
Boolean
JSON
now returns a timestamp with time zone if time zones are supported by your database, otherwise now returns a timestamp without time zone .
For more info about the way these data types behave in Metabase, see Timezones.
Limitations
now might not actually be now (in your local time) if you don’t live in the same timezone as your Metabase report time zone.
If you need to compare now to a column in a different time zone, use convertTimezone to shift both columns into the same time zone. For example:
convertTimezone(now, 'UTC', <report timezone>) >= convertTimezone([Deadline], 'UTC', <source time zone>)
Related functions
Different ways to do the same thing, because while you’d love to use custom expressions more, now’s just not the time.
SQL
Spreadsheets
Python
SQL
When you run a question using the query builder, Metabase will convert your query builder settings (filters, summaries, etc.) into a SQL query, and run that query
against your database to get your results.
By default, now uses your Metabase’s report time zone. If your admin hasn’t set a report time zone, now will use your database’s time zone.
Say you’re using a Postgres database. If your Metabase report time zone is set to EST, you’ll get now in EST:
If you don’t have a report time zone, you’ll get now in the Postgres database’s time zone (typically UTC):
SELECT CURRENT_TIME
Spreadsheets
The spreadsheet function NOW() gets the current date and time in your operating system’s time zone (the time that’s on your computer or mobile device).
Python
You can use pd.Timestamp.now() using the pandas module. This will give you a Timestamp object with the current date and time in your operating system’s time
zone.
Further reading
Watch Demo Self-Service Business Analytics Query Builder Jobs We're hiring! Case Studies
Permissions
Open Source Professional Services The 2023 Metabase Community
More Resources
Status
Github NaNk
© Metabase 2024
Accept cookies