Im trying to perform some DATEDIFF calculations but i simply lack the knowledge to complete the task.

I got a table that keeps record of the articles sold with date when the operation took place and historic prices of the article. Since i live in a country with 30% of anual inflation. I must consider that factor to calculate the earning by every article sold.

It all works fine while i have the buying price saved and the date when a product price change took place. But, when that data was not saved as an approximation i would like to use the date when the operation with a different price took place.
As result of the following select operations:

```
USE Rusich
DECLARE 
    @IDNegocio AS INT,
    @UsarIDProducto AS BIT,
    @IDProducto AS VARCHAR(50), 
    @FechaInicio AS DATE, 
    @FechaFinal AS DATE, 
    @TamañoMinimoDeMuestra AS INT, 
    @SeVendeMinimoCadaXDias AS INT,
    @Inflacion AS DECIMAL(18,3),
    @AuxMargen AS DECIMAL(18,2),
    @AuxDifDias AS INT;

SET @FechaInicio = '01/02/10';
SET @FechaFinal = '28/02/18';
SET @IDNegocio = 1;
SET @IDProducto = '6910101404918';                  /*<<<< Cambiar a NULL segun necesidad*/
SET @TamañoMinimoDeMuestra = 1;
SET @SeVendeMinimoCadaXDias = 90;
SET @Inflacion = 0.083;

SELECT 
    AUXFechas.IDProducto,
    AUXFechas.[$ Precio de Venta Historico],
    AUXFechas.[Fecha Precio],
    AUXFechas.[Fecha Venta],
    AUXFechas.FechaAnterior,
    AUXFechas.DiasDiferencia,
    CAST(
        CASE WHEN 
            AUXFechas.IDProducto NOT IN ('112', '113' , '114')
            AND AUXFechas.[Fecha Precio] <= AUXFechas.[Fecha Venta]
            AND StockDetalles.[Precio de Compra] <> 0 
            AND StockDetalles.[Precio de Compra] IS NOT NULL THEN
                (1 - StockDetalles.[Precio de Compra] / AUXFechas.[$ Precio de Venta Historico]) * 100 - (AUXFechas.[$ Descuento]/100) - DATEDIFF(DAY, AUXFechas.[Fecha Precio], AUXFechas.[Fecha Venta]) * @Inflacion
        ELSE
            NULL
        END 
    AS DECIMAL(18,2)) AS [% Margen Ganancia Real]
FROM
    StockDetalles JOIN (
SELECT
        Stock.ID AS IDProducto,
        Stock.Cantidad AS #Disponible,
        Recibos.Cantidad as [#Vendida],
        Recibos.Precio AS [$ Precio de Venta Historico],
        Recibos.Descuento AS [$ Descuento],
        Stock.[Precio de Venta] AS [$ Precio],
        Stock.[Fecha Actualizacion de Precio] AS [Fecha Precio],
        CAST(RecibosRegistros.Fecha AS DATE) AS [Fecha Venta],
        ISNULL(LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha), Stock.[Fecha Actualizacion de Precio]) AS FechaAnterior,
        /*Considera el caso cuando no existe un registro anterior y usa la fecha de actualizacion de precio si es posible*/
        CASE WHEN (LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha) IS NULL) AND CAST(RecibosRegistros.Fecha AS DATE) > Stock.[Fecha Actualizacion de Precio] THEN
            (DATEDIFF(DAY, CAST(RecibosRegistros.Fecha AS DATE),
            /*PARTITION BY PARTICIONA POR COLUMNA*/
            ISNULL(LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha), [Fecha Actualizacion de Precio])) / Recibos.Cantidad) * -1
        ELSE
            (DATEDIFF(DAY, CAST(RecibosRegistros.Fecha AS DATE),
            /*PARTITION BY PARTICIONA POR COLUMNA*/
            LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha)) / Recibos.Cantidad) * -1
        END AS DiasDiferencia
    FROM 
        RecibosRegistros 
        JOIN Recibos ON RecibosRegistros.IDRecibo = Recibos.IDRecibo 
        JOIN Stock ON Recibos.IDProducto = Stock.ID 
    WHERE 
        RecibosRegistros.NegocioID = @IDNegocio 
        AND Stock.IDNegocio = @IDNegocio 
        AND Stock.ID != '111'
        AND Stock.ID != '112'
        AND Stock.ID != '113'
        AND Stock.ID != '114'
        AND Stock.ID = COALESCE(@IDProducto,Stock.ID)) AS AUXFechas ON StockDetalles.ID = AUXFechas.IDProducto
        JOIN Categorias ON StockDetalles.CategoriaID = Categorias.ID

```

Im obtaining this result:

```
IDProducto  $ Precio de Venta Historico Fecha Precio    Fecha Venta FechaAnterior   DiasDiferencia  % Margen Ganancia Real
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6910101404918   15,00           2012-10-18  2012-07-23  2012-10-18  NULL        NULL
6910101404918   15,00           2012-10-18  2012-09-01  2012-07-23  40      NULL
6910101404918   21,00           2012-10-18  2013-07-01  2012-09-01  303     NULL
6910101404918   21,00           2012-10-18  2013-09-02  2013-07-01  63      NULL
6910101404918   21,00           2012-10-18  2013-09-24  2013-09-02  22      NULL
0200002003867   28,00           2014-03-05  2014-07-08  2014-03-05  125     NULL
0200002003867   28,00           2014-03-05  2014-07-08  2014-07-08  0       NULL
0200002003867   28,00           2014-03-05  2014-10-23  2014-07-08  107     NULL
0200002003867   28,00           2014-03-05  2015-01-21  2014-10-23  90      NULL
0200002003867   28,00           2014-03-05  2015-04-06  2015-01-21  75      NULL

```

What i need to do is get a column with date (Fecha Precio) where [$ Precio de Venta Historico] changed.

Output should be:

```
IDProducto      $ Precio de Venta Historico Fecha Precio    Fecha Venta FechaAnterior   DiasDiferencia  % Margen Ganancia Real  Fecha Cambio
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6910101404918   15,00               2012-10-18  2012-07-23  2012-10-18  NULL        NULL            NULL
6910101404918   15,00               2012-10-18  2012-09-01  2012-07-23  40      NULL            2012-10-18
6910101404918   21,00               2012-10-18  2013-07-01  2012-09-01  303     NULL            NULL
6910101404918   21,00               2012-10-18  2013-09-02  2013-07-01  63      NULL            2012-09-01
6910101404918   21,00               2012-10-18  2013-09-24  2013-09-02  22      NULL            2012-09-01
0200002003867   28,00               2014-03-05  2014-07-08  2014-03-05  125     NULL            NULL
0200002003867   28,00               2014-03-05  2014-07-08  2014-07-08  0       NULL            2014-07-08
0200002003867   28,00               2014-03-05  2014-10-23  2014-07-08  107     NULL            2014-07-08
0200002003867   28,00               2014-03-05  2015-01-21  2014-10-23  90      NULL            2014-07-08
0200002003867   28,00               2014-03-05  2015-04-06  2015-01-21  75      NULL            2014-07-08

```

My guess is that the solution must be related with LAG/LEAD and PartitionBY IDProducto but i dont know how to do it. Please Help