What is ZN in Tableau?
Nothing brings a dashboard-building session to a screeching halt faster than a rogue collection of NULL values. They create ugly gaps in your line charts, return unexpected results in calculations, and leave blank spaces in your tables that make your final report look unfinished. Thankfully, Tableau has an incredibly straightforward function to handle this exact problem: ZN().
This article will show you exactly what the ZN function is and how to use it. We'll walk through why NULLs are so problematic and provide step-by-step examples of how ZN can clean up your visualizations and calculations in seconds.
Understanding the ZN Function in Tableau
The name ZN is short for "Zero if Null." Its job is beautifully simple: it checks an expression or a field, and if it finds a NULL value, it replaces it with a zero (0). If the value is anything other than NULL (a number, for instance), it leaves it unchanged.
The basic syntax for the function looks like this:
ZN([Your Measure])
Imagine you have a small dataset for product sales, but the data is incomplete. For day 2, no sales were recorded, resulting in a NULL value instead of a zero.
- Original Data: [150, NULL, 300, 225]
- After Applying ZN(): [150, 0, 300, 225]
That's it. It’s a simple swap, but the impact it has on your dashboards is massive. By turning empty marks into concrete zeros, you give Tableau a complete dataset to work with, leading to more accurate and reliable visualizations.
The Problem with NULLs: Why ZN is So Useful
NULL values represent the absence of data. While that’s technically accurate, in a business context, the absence of data often means something specific - like zero sales, zero website visits, or zero clicks. When Tableau sees NULL, it doesn’t see a zero, it sees a void, and that void causes several common problems.
1. Broken Visualizations
The most common place you'll feel the pain of NULLs is in time-series line charts. If you're plotting sales by month and a particular month has no sales data, Tableau will display a gap in your line chart. For a stakeholder trying to understand performance trends, this visual break can be jarring and confusing. It breaks the flow of the story you're trying to tell with your data.
By using ZN to fill that gap with a zero, the line chart connects to the X-axis, clearly showing that sales dropped to zero for that period before picking back up. This creates a continuous, easy-to-read narrative of performance over time.
2. Skewed Mathematical Calculations
NULL values can quietly wreak havoc on your calculations, particularly averages. Most aggregations in Tableau, like SUM(), simply ignore NULL values. If you are summing [100, 50, NULL], the result is 150. This is usually fine.
However, when calculating an AVG(), this behavior can be misleading. AVG() is calculated as SUM([Values]) / COUNT([Values]). Since NULL is ignored, it isn’t included in the count. So, the average of [100, 50, NULL] isn't (100 + 50 + 0) / 3 = 50. It's (100 + 50) / 2 = 75.
If your goal is to find the average sales across all three days (including the day with no sales), the 75 result is incorrect. By applying the ZN function first, your dataset becomes [100, 50, 0]. Now, AVG() calculates (100 + 50 + 0) / 3 = 50, giving you the true daily average.
3. Unappealing Tables and Crosstabs
Blank cells in a data table or crosstab can look messy and unprofessional. A blank might mean zero, or it might mean the data hasn't loaded properly - it forces your audience to guess. Replacing those blanks with clear, intentional zeros makes your tables far easier to read and interpret. Everyone understands that 0 means zero, a blank space is open to interpretation.
A Practical Guide: Using ZN in a Calculated Field
Let's walk through the most common way to apply the ZN function: within a calculated field. For this example, we'll fix a broken monthly sales line chart.
Step 1: Create a Calculated Field
In your Tableau worksheet, right-click anywhere in the left-hand Data pane (where your dimensions and measures are listed). From the context menu, select Create Calculated Field.
Step 2: Name Your New Field
A dialog box will open. Give your new field an intuitive name. If your original field is [Sales], a good name would be Sales (ZN) or Sales (Zero Filled).
Step 3: Write the ZN Formula
In the formula box, you'll apply the ZN function to your aggregated measure. Most of the time, you'll wrap your aggregation (like SUM(), AVG(), COUNT()) inside the ZN function. This is because you want to turn the final result into a zero if that specific aggregation comes up empty for a certain dimension (like a month or a region).
The formula would look like this:
ZN(SUM([Sales]))
This tells Tableau: "First, calculate the sum of sales for each data point on my chart. If the result of any of those sums is NULL, show a zero instead."
Click OK to save your calculated field.
Step 4: Use the New Field in Your View
Now, you will see Sales (ZN) in your Measures list. Drag your original SUM([Sales]) pill off the Rows shelf and replace it with your newly created Sales (ZN) field.
Instantly, you'll see any gaps in your line chart disappear. The line will now drop down to zero for any periods that previously contained NULL data, giving you a complete and accurate visualization.
Alternatives to ZN: IFNULL and IIF Functions
The ZN function is perfect for replacing NULL with a zero, but sometimes you might need more flexibility. Tableau offers other logical functions that can achieve a similar result but with more customization.
The IFNULL Function
IFNULL is the parent function of ZN. It allows you to check for a NULL value and replace it with any value you specify - not just zero.
The syntax is:
IFNULL([Your Measure], [Value to return if NULL])
ZN([Sales])is the same asIFNULL([Sales], 0).
When to use IFNULL: Use it when you need to replace NULL with something other than zero. For example, maybe you want to display the text "Not Available" in a table, or you want to fill missing data with the average of all other data points available.
Examples:
`// Example 1: Replace NULL with a string IFNULL(SUM([Profit]), "N/A")
// Example 2: Replace NULL with the average of all window sales IFNULL(SUM([Sales]), WINDOW_AVG(SUM([Sales])))`
The IIF Function
IIF is Tableau's version of a general-purpose IF-THEN-ELSE statement. It lets you check if a logical condition is true and returns one value if it is, and another if it's false.
The syntax is:
IIF([Condition], [Value if TRUE], [Value if FALSE])
To replicate the ZN function, you’d use IIF with the ISNULL() function:
ZN([Sales])produces the same output asIIF(ISNULL([Sales]), 0, [Sales]).
When to use IIF: Use IIF for situations involving more complex logical conditions that go beyond a simple null check (e.g., categorizing sales as "High" or "Low").
Real-World Scenarios: When to Reach for ZN
Once you get comfortable with ZN, you'll start seeing opportunities to use it everywhere. Here are some of the most common applications:
- Fixing Gaps in Time-Series Line Charts: As we covered, this is the classic use case - ensuring dates with no data show up as zero activity rather than a void.
- Calculating Accurate Averages: When you need to include periods of inactivity in your average calculation (e.g., average daily users),
ZNmakes sure that "zero user" days are counted properly. - Creating 'Dense' Data for Heatmaps: A heatmap with blank squares can be hard to read. Using
ZNensures that every intersection of your rows and columns has a numerical value, even if it's zero, resulting in a cleaner color scale and no gaps. - Preparing Data for Table Calculations: Table calculations like
RUNNING_TOTALorPERCENT_DIFFERENCEcan behave unexpectedly withNULLs. Converting them to zeros upfront can often lead to more intuitive and correct results. - Improving Readability in Text Tables: A dashboard consumer shouldn't have to wonder what a blank space in a report means.
ZNreplaces ambiguity with the certainty of a zero.
Final Thoughts
The ZN function is a simple yet incredibly effective tool in any Tableau user's toolkit. It cleans up missing data by converting NULL values into zeros, instantly solving common issues like broken line charts, inaccurate calculations, and messy tables. Mastering this small function will improve the clarity and professionalism of every dashboard you build.
Wrestling with calculations and data gaps inside a BI tool is a common friction point in data analysis. While functions like ZN help, they're often part of a larger, manual reporting process that consumes hours every week. With Graphed, we automate this entire workflow. Instead of cleaning data and building charts click-by-click, you can just connect your sources (like Google Analytics, Shopify, or Salesforce) and ask in plain English for the dashboard you need. We handle cleaning the data and building the visuals so you can get perfectly built, real-time reports in seconds, not hours.
Related Articles
How to Connect Facebook to Google Data Studio: The Complete Guide for 2026
Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.
Appsflyer vs Mixpanel: Complete 2026 Comparison Guide
The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.
DashThis vs AgencyAnalytics: The Ultimate Comparison Guide for Marketing Agencies
When it comes to choosing the right marketing reporting platform, agencies often find themselves torn between two industry leaders: DashThis and AgencyAnalytics. Both platforms promise to streamline reporting, save time, and impress clients with stunning visualizations. But which one truly delivers on these promises?