<?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"><channel><title><![CDATA[TechCFO]]></title><description><![CDATA[TechCFO]]></description><link>https://futurecfo.dk</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1702892553892/JZbvLMEuX.png</url><title>TechCFO</title><link>https://futurecfo.dk</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Nov 2026 17:47:24 GMT</lastBuildDate><atom:link href="https://futurecfo.dk/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Implementing Time-Dimensional Price Lookups in BigQuery with Price Difference Analysis]]></title><description><![CDATA[Or any other Datawarehouse]]></description><link>https://futurecfo.dk/implementing-time-dimensional-price-lookups-in-bigquery-with-price-difference-analysis</link><guid isPermaLink="true">https://futurecfo.dk/implementing-time-dimensional-price-lookups-in-bigquery-with-price-difference-analysis</guid><category><![CDATA[SQL]]></category><category><![CDATA[bigquery]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[data analysis]]></category><dc:creator><![CDATA[TechCFO]]></dc:creator><pubDate>Mon, 20 Nov 2023 13:41:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702892658558/170fb18f-c4bf-49cb-a733-f00cb5234f1c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In the dynamic world of data analytics, tracking and querying historical product prices with precision is crucial. This blog post delves into setting up a time-dimensional price lookup system in Google BigQuery, enhanced with the capability to calculate the difference between invoiced prices and historical prices. This feature is especially useful for cost controliing and financial analysis, where understanding discrepancies in pricing over time is important.</p>
<h2 id="heading-table-structure">Table Structure</h2>
<h3 id="heading-product-price-table">Product Price Table</h3>
<p>Stores historical records of product prices, each with a defined effective period.</p>
<ul>
<li><p><code>product_number</code>: Unique identifier for the product.</p>
</li>
<li><p><code>price</code>: The quoted price of the product.</p>
</li>
<li><p><code>effective_from</code>: The start date for the price's validity.</p>
</li>
<li><p><code>effective_to</code>: End date for the price's validity (NULL for currently valid prices).</p>
</li>
</ul>
<h3 id="heading-invoice-lines-table">Invoice Lines Table</h3>
<p>Contains invoice line item details.</p>
<ul>
<li><p><code>invoice_number</code>: Unique identifier for the invoice.</p>
</li>
<li><p><code>product_number</code>: Associated product number.</p>
</li>
<li><p><code>invoice_date</code>: Date of the invoice.</p>
</li>
<li><p><code>charged_price</code>: Price charged on the invoice.</p>
</li>
</ul>
<h2 id="heading-query-with-price-difference">Query with Price Difference</h2>
<p>To analyze price differences, we join the tables on <code>product_number</code> and compare <code>invoice_date</code> with the price's effective period. We also calculate the difference between the charged price and the historical price.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span>
  il.invoice_number,
  il.product_number,
  il.invoice_date,
  il.charged_price,
  pp.price <span class="hljs-keyword">AS</span> qouted_price,
  (il.charged_price - pp.price) <span class="hljs-keyword">AS</span> price_difference
<span class="hljs-keyword">FROM</span>
  invoice_lines il
<span class="hljs-keyword">JOIN</span>
  product_prices pp
<span class="hljs-keyword">ON</span>
  il.product_number = pp.product_number
<span class="hljs-keyword">WHERE</span>
  il.invoice_date &gt;= pp.effective_from
  <span class="hljs-keyword">AND</span> (il.invoice_date &lt;= pp.effective_to <span class="hljs-keyword">OR</span> pp.effective_to <span class="hljs-keyword">IS</span> <span class="hljs-literal">NULL</span>)
</code></pre>
<h2 id="heading-practical-example-with-price-difference">Practical Example with Price Difference</h2>
<p>Let's demonstrate this concept with an example involving a price difference.</p>
<h3 id="heading-creating-the-tables">Creating the Tables</h3>
<pre><code class="lang-sql"><span class="hljs-comment">-- Creating the Product Price Table</span>
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> product_prices (
  product_number <span class="hljs-keyword">STRING</span>,
  price FLOAT64,
  effective_from <span class="hljs-built_in">DATE</span>,
  effective_to <span class="hljs-built_in">DATE</span>
);

<span class="hljs-comment">-- Creating the Invoice Lines Table</span>
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> invoice_lines (
  invoice_number <span class="hljs-keyword">STRING</span>,
  product_number <span class="hljs-keyword">STRING</span>,
  invoice_date <span class="hljs-built_in">DATE</span>,
  charged_price FLOAT64
);
</code></pre>
<h3 id="heading-inserting-dummy-data">Inserting Dummy Data</h3>
<pre><code class="lang-sql"><span class="hljs-comment">-- Inserting data into Product Prices</span>
<span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> product_prices (product_number, price, effective_from, effective_to)
<span class="hljs-keyword">VALUES</span>
  (<span class="hljs-string">'P001'</span>, <span class="hljs-number">100.0</span>, <span class="hljs-string">'2021-01-01'</span>, <span class="hljs-string">'2021-06-30'</span>),
  (<span class="hljs-string">'P001'</span>, <span class="hljs-number">110.0</span>, <span class="hljs-string">'2021-07-01'</span>, <span class="hljs-literal">NULL</span>),  <span class="hljs-comment">-- Price change</span>
  (<span class="hljs-string">'P002'</span>, <span class="hljs-number">150.0</span>, <span class="hljs-string">'2021-01-01'</span>, <span class="hljs-literal">NULL</span>);  <span class="hljs-comment">-- Constant price</span>

<span class="hljs-comment">-- Inserting data into Invoice Lines with a price discrepancy</span>
<span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> invoice_lines (invoice_number, product_number, invoice_date, charged_price)
<span class="hljs-keyword">VALUES</span>
  (<span class="hljs-string">'INV001'</span>, <span class="hljs-string">'P001'</span>, <span class="hljs-string">'2021-05-15'</span>, <span class="hljs-number">100.0</span>),  <span class="hljs-comment">-- Correct price</span>
  (<span class="hljs-string">'INV002'</span>, <span class="hljs-string">'P001'</span>, <span class="hljs-string">'2021-07-20'</span>, <span class="hljs-number">115.0</span>),  <span class="hljs-comment">-- Price discrepancy</span>
  (<span class="hljs-string">'INV003'</span>, <span class="hljs-string">'P002'</span>, <span class="hljs-string">'2021-04-10'</span>, <span class="hljs-number">150.0</span>);  <span class="hljs-comment">-- Correct price</span>
</code></pre>
<h3 id="heading-query-results-on-dummy-data">Query Results on Dummy Data</h3>
<p>When the query is executed on the provided dummy data, it yields the following results, demonstrating how the system can identify discrepancies between invoiced and quoted prices:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Invoice Number</td><td>Product Number</td><td>Invoice Date</td><td>Charged Price</td><td>Quoted Price</td><td>Price Difference</td></tr>
</thead>
<tbody>
<tr>
<td>INV001</td><td>P001</td><td>2021-05-15</td><td>$100.00</td><td>$100.00</td><td>$0.00</td></tr>
<tr>
<td>INV002</td><td>P001</td><td>2021-07-20</td><td>$115.00</td><td>$110.00</td><td>$5.00</td></tr>
<tr>
<td>INV003</td><td>P002</td><td>2021-04-10</td><td>$150.00</td><td>$150.00</td><td>$0.00</td></tr>
</tbody>
</table>
</div><h3 id="heading-conclusion">Conclusion</h3>
<p>This enhanced approach in BigQuery not only allows us to track quoted and historical prices over time but also enables us to identify and analyze discrepancies between invoiced prices and quoted prices taking the time element into account and not just analyzing against the current price. Such a system is invaluable for businesses that need to maintain financial accuracy and integrity in their invoicing, controlling and pricing strategies.</p>
<hr />
<h3 id="heading-footnote-adjustments-for-different-sql-environments">Footnote: Adjustments for Different SQL Environments</h3>
<p>This example is tailored for Google BigQuery. If you're implementing a similar project in a different SQL environment or data warehouse, some adjustments to the SQL syntax may be necessary. Here are a few common variations:</p>
<ul>
<li><p><strong>Data Types:</strong></p>
<ul>
<li><p><code>FLOAT64</code> in BigQuery is equivalent to <code>FLOAT</code> in many other SQL systems.</p>
</li>
<li><p>The <code>NUMERIC</code> type in BigQuery might be <code>DECIMAL</code> in other databases.</p>
</li>
</ul>
</li>
<li><p><strong>Function Names:</strong></p>
<ul>
<li><p>Some functions might have different names (e.g., string functions like <code>SUBSTRING</code> vs. <code>SUBSTR</code>).</p>
</li>
<li><p>Date and time functions often vary between systems (e.g., <code>CURRENT_TIMESTAMP</code> vs. <code>NOW()</code>).</p>
</li>
</ul>
</li>
<li><p><strong>Syntax Variations:</strong></p>
<ul>
<li><p>The syntax for creating tables, inserting data, and querying can have minor differences.</p>
</li>
<li><p>Join syntax and window functions might differ slightly.</p>
</li>
</ul>
</li>
<li><p><strong>Performance Optimizations:</strong></p>
<ul>
<li><p>Indexing strategies and query optimization techniques can vary significantly.</p>
</li>
<li><p>Some databases might require specific tuning for better performance.</p>
</li>
</ul>
</li>
</ul>
<p>It's always a good practice to refer to the specific documentation of the SQL environment you are working with to ensure compatibility and optimal performance.</p>
<hr />
]]></content:encoded></item></channel></rss>