---
title: "ClickHouse® tips #7: Forward and backfilling null values"
excerpt: "Making use of array functions to do it. Part 7."
authors: "Alejandra Rodriguez"
categories: "The Data Base"
createdOn: "2023-09-25 00:00:00"
publishedOn: "2021-05-18 00:00:00"
updatedOn: "2022-09-08 00:00:00"
status: "published"
---

<p>Many times you have data with null values and you’d like to fill them with non-null values. For example, imagine this is your data:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20%2A%20FROM%20num_str%0A&amp;language=Tinybird&amp;title=A%20dataset%20with%20some%20null%20values&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>You could replace them by a constant value like this:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%0A%20%20%20%20n%2C%0A%20%20%20%20v%2C%0A%20%20%20%20ifNull%28v%2C%20%27Z%27%29%20v_simple_fill%2C%0A%20%20%20%20ifNull%28v%2C%20%28SELECT%20topK%281%29%28v%29%5B1%5D%20from%20num_str%29%20as%20mode%29%20as%20v_mode_fill%0AFROM%20num_str%0A&amp;language=Tinybird&amp;title=Replace%20by%20constant%20value&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>But in many cases you’ll want to fill them with the latest (or next) value available. This is a one-liner in libraries like Pandas with the <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.fillna.html">fillna</a> method, and on Postgres <a href="https://hakibenita.com/sql-for-data-analysis#back-and-forward-fill">this way</a>. On ClickHouse® is also possible using array functions:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%0A%20%20tuples.1%20n%2C%0A%20%20tuples.2%20v%2C%0A%20%20tuples.3%20v_ffill%2C%0A%20%20tuples.4%20v_bfill%0AFROM%0A%20%20%28SELECT%0A%20%20%20%20%20%20arrayJoin%28%0A%20%20%20%20%20%20%20%20arrayZip%28%0A%20%20%20%20%20%20%20%20%20%20groupArray%28n%29%20AS%20n%2C%0A%20%20%20%20%20%20%20%20%20%20arrayMap%28x%20-%3E%20x%20%21%3D%20%27wadus%27%20%3F%20x%20%3A%20null%2C%20groupArray%28v_nulls_replaced%29%29%20AS%20v%2C%0A%20%20%20%20%20%20%20%20%20%20arrayFill%28x%20-%3E%20x%20%21%3D%20%27wadus%27%2C%20groupArray%28v_nulls_replaced%29%29%20AS%20v_ffill%2C%0A%20%20%20%20%20%20%20%20%20%20arrayReverseFill%28x%20-%3E%20x%20%21%3D%20%27wadus%27%2C%20groupArray%28v_nulls_replaced%29%29%20AS%20v_bfill%0A%20%20%20%20%20%20%20%20%29%0A%20%20%20%20%20%20%29%20tuples%0A%20%20FROM%0A%20%20%20%20%20%28SELECT%20%0A%20%20%20%20%20%20%20%20%2A%2C%0A%20%20%20%20%20%20%20%20ifNull%28v%2C%20%27wadus%27%29%20v_nulls_replaced%0A%20%20%20%20%20%20FROM%20num_str%20%0A%20%20%20%20%20%20ORDER%20BY%20n%20ASC%29%0A%20%20%29%0A&amp;language=Tinybird&amp;title=Forward%20and%20backfilling%20null%20values%20on%20Postgres&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p><a href="https://ui.tinybird.co/snapshot/5a8af1b52f93493fb61c7c62d59a6167">Here</a> is a full explanation on what happens in each of the subqueries of this last query and of what each of the array functions does.</p>
