<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhakti Padwal on Medium]]></title>
        <description><![CDATA[Stories by Bhakti Padwal on Medium]]></description>
        <link>https://medium.com/@bhaktipadwal15?source=rss-f2d14ba969d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JNkHRd9BmNpL1zcN</url>
            <title>Stories by Bhakti Padwal on Medium</title>
            <link>https://medium.com/@bhaktipadwal15?source=rss-f2d14ba969d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:10:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhaktipadwal15/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding LEAD and LAG functions in SQL and PySpark]]></title>
            <link>https://medium.com/@bhaktipadwal15/understanding-lead-and-lag-functions-in-sql-and-pyspark-a2d1e2573900?source=rss-f2d14ba969d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2d1e2573900</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[sql-window-functions]]></category>
            <category><![CDATA[pyspark]]></category>
            <dc:creator><![CDATA[Bhakti Padwal]]></dc:creator>
            <pubDate>Thu, 20 Mar 2025 06:33:10 GMT</pubDate>
            <atom:updated>2025-03-20T06:41:44.238Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with time-series or ordered data, we often need to compare values from different rows with the same column. This is where LEAD and LAG functions help.</p><p>Both LEAD and LAG are window functions.</p><p><strong>Let’s first understand LAG function.</strong></p><p>The LAG function fetches the value from the previous row in a specified column based on ordering column.</p><p>Let’s consider below sales data. We need to find sales of current day with previous day.</p><p><strong>SQL example:</strong></p><pre>sales_data<br>order_date sales<br>01-03-2025 100<br>02-03-2025 120<br>03-03-2025 90<br>04-03-2025 150<br><br>SELECT order_date, sales, <br>LAG(sales,1) OVER (ORDER BY order_date) as previous_day_sales<br>from sales_data;<br><br>Expected output :<br>order_date sales previous_day_sales<br>01-03-2025 100    NULL<br>02-03-2025 120    100<br>03-03-2025 90     120<br>04-03-2025 150    90</pre><ol><li>The first row has NULL because there’s no previous day’s data.</li></ol><p>2. Each row fetches the sales value form the previous row.</p><p>3 .OVER(ORDER BY order_date) ensures data is processed in the correct order.</p><p><strong>PySpark example:</strong></p><pre>from pyspark.sql.functions import lag<br>from pyspark.sql.window import Window<br><br>#considering sales_df already created with sales data<br><br>window_spec = Window.orderBy(&quot;order_date&quot;)<br><br>df = sales_df.withColumn(&quot;next_day_sales&quot;,lag(&quot;sales&quot;,1).over(window_spec))<br><br>df.show()<br><br><br>Expected output : <br>order_date sales previous_day_sales<br>01-03-2025 100    NULL<br>02-03-2025 120    100<br>03-03-2025 90     120<br>04-03-2025 150    90</pre><ol><li>Window.orderBy(“order_date”) defines the order of rows.</li><li>lag(“sales”, 1).over(window_spec) gets the previous row’s value.</li></ol><p><strong>LEAD function</strong></p><p>The LEAD function fetches the value from the next row in a specified column.</p><p>Let’s consider same sales data as above.</p><p><strong>SQL example:</strong></p><pre>order_date sales<br>01-03-2025 100<br>02-03-2025 120<br>03-03-2025 90<br>04-03-2025 150<br><br>SELECT order_date, sales, <br>LEAD(sales,1) OVER (ORDER BY order_date) as next_day_sales<br>from sales_data;<br><br>Expected output :<br>order_date sales next_day_sales<br>01-03-2025 100    120<br>02-03-2025 120    90<br>03-03-2025 90     150<br>04-03-2025 150    NULL</pre><ol><li>The last row has NULL because there’s no next day’s data.</li></ol><p>2. Each row fetches the sales value form the next row.</p><p>3. OVER(ORDER BY order_date) ensures data is processed in the correct order.</p><p><strong>PySpark example:</strong></p><pre>from pyspark.sql.functions import lead<br>from pyspark.sql.window import Window<br><br>#considering sales_df already created with sales data<br><br>window_spec = Window.orderBy(&quot;order_date&quot;)<br><br>df = sales_df.withColumn(&quot;next_day_sales&quot;,lead(&quot;sales&quot;,1).over(window_spec))<br><br>df.show()<br><br>Expected output : <br>order_date sales next_day_sales<br>01-03-2025 100    120<br>02-03-2025 120    90<br>03-03-2025 90     150<br>04-03-2025 150    NULL</pre><ol><li>Window.orderBy(“order_date”) defines the order of rows.</li><li>lead(“sales”, 1).over(window_spec) gets the previous row’s value.</li></ol><p>Real world use cases for LEAD and LAG :</p><ol><li>Stock market analysis : Compare today’s stock price with the previous or next day.</li><li>Customer retention : Find the previous and next purchase date for each customer.</li><li>Churn Prediction : Compare user activity before and after a key event.</li></ol><p>Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2d1e2573900" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>