Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
debiagui
Resolver I
Resolver I

Applying a window frame between in Lakehouse SQL

We're creating some SQL views in a Lakehouse.

In particular customer need to process the moving total over 3 months. I was lookng at this: OVER Clause (Transact-SQL) - SQL Server | Microsoft Learn

But seems like Lakehouse doesn't support something like:  ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

 

full query:

SELECT
    PRD.ProductId,
    SO.MarketId,
    CA.MonthSeq AS MonthGrouping,
    SUM(SO.SalesAmount) OVER (
        PARTITION BY PRD.ProductId, SO.MarketId
        ORDER BY CA.MonthSeq
        ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
    ) AS MovingTotal
FROM
    SellOut AS SO
INNER JOIN
    Product AS PRD ON SO.ProductId = PRD.ProductId
INNER JOIN
    Calendar AS CA ON SO.Date= CA.Date
GROUP BY
    PRD.ProductId,
    SO.MarketId,
    CA.MonthSeq 
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @debiagui ,

 

The `ROWS BETWEEN` syntax, commonly used for defining window frames in SQL Server, might not directly apply in the Lakehouse environment. This is because the SQL analytics endpoint in Lakehouse, while providing a rich set of functionalities for data analysis, does not support the full T-SQL surface area, including certain windowing functions and syntaxes.

 

For calculating moving totals in Lakehouse, Here are a couple of suggestions:

 

  1. You could leverage SparkSQL to perform complex windowing operations. SparkSQL provides extensive support for window functions, including moving averages and totals. More information on SparkSQL window functions can be found here:

Built-in Functions - Spark 3.5.1 Documentation (apache.org)

 

  1. Use self-linking methods to simulate window functions, please try this query:
SELECT
    A.ProductId,
    A.MarketId,
    A.MonthSeq AS MonthGrouping,
    SUM(B.SalesAmount) AS MovingTotal
FROM
    (SELECT
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq
     FROM
        SellOut AS SO
     INNER JOIN
        Product AS PRD ON SO.ProductId = PRD.ProductId
     INNER JOIN
        Calendar AS CA ON SO.Date= CA.Date
     GROUP BY
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq) AS A
INNER JOIN
    (SELECT
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq,
        SO.SalesAmount
     FROM
        SellOut AS SO
     INNER JOIN
        Product AS PRD ON SO.ProductId = PRD.ProductId
     INNER JOIN
        Calendar AS CA ON SO.Date= CA.Date) AS B
ON
    A.ProductId = B.ProductId AND
    A.MarketId = B.MarketId AND
    B.MonthSeq BETWEEN A.MonthSeq - 2 AND A.MonthSeq
GROUP BY
    A.ProductId,
    A.MarketId,
    A.MonthSeq
ORDER BY
    A.ProductId,
    A.MarketId,
    A.MonthSeq;

 

  1. If the lack of support for certain SQL syntaxes significantly impacts your use cases, consider providing feedback to the Lakehouse platform team. While direct support might not be available now, user feedback can influence future enhancements and feature additions.

 

For more insights into the capabilities and limitations of the SQL analytics endpoint in Lakehouse, you might find the following documentation useful:

What is a lakehouse? - Microsoft Fabric | Microsoft Learn

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @debiagui ,

 

The `ROWS BETWEEN` syntax, commonly used for defining window frames in SQL Server, might not directly apply in the Lakehouse environment. This is because the SQL analytics endpoint in Lakehouse, while providing a rich set of functionalities for data analysis, does not support the full T-SQL surface area, including certain windowing functions and syntaxes.

 

For calculating moving totals in Lakehouse, Here are a couple of suggestions:

 

  1. You could leverage SparkSQL to perform complex windowing operations. SparkSQL provides extensive support for window functions, including moving averages and totals. More information on SparkSQL window functions can be found here:

Built-in Functions - Spark 3.5.1 Documentation (apache.org)

 

  1. Use self-linking methods to simulate window functions, please try this query:
SELECT
    A.ProductId,
    A.MarketId,
    A.MonthSeq AS MonthGrouping,
    SUM(B.SalesAmount) AS MovingTotal
FROM
    (SELECT
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq
     FROM
        SellOut AS SO
     INNER JOIN
        Product AS PRD ON SO.ProductId = PRD.ProductId
     INNER JOIN
        Calendar AS CA ON SO.Date= CA.Date
     GROUP BY
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq) AS A
INNER JOIN
    (SELECT
        PRD.ProductId,
        SO.MarketId,
        CA.MonthSeq,
        SO.SalesAmount
     FROM
        SellOut AS SO
     INNER JOIN
        Product AS PRD ON SO.ProductId = PRD.ProductId
     INNER JOIN
        Calendar AS CA ON SO.Date= CA.Date) AS B
ON
    A.ProductId = B.ProductId AND
    A.MarketId = B.MarketId AND
    B.MonthSeq BETWEEN A.MonthSeq - 2 AND A.MonthSeq
GROUP BY
    A.ProductId,
    A.MarketId,
    A.MonthSeq
ORDER BY
    A.ProductId,
    A.MarketId,
    A.MonthSeq;

 

  1. If the lack of support for certain SQL syntaxes significantly impacts your use cases, consider providing feedback to the Lakehouse platform team. While direct support might not be available now, user feedback can influence future enhancements and feature additions.

 

For more insights into the capabilities and limitations of the SQL analytics endpoint in Lakehouse, you might find the following documentation useful:

What is a lakehouse? - Microsoft Fabric | Microsoft Learn

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.