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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
namemon
Frequent Visitor

Is it possible to increment an Column in MS SQL inside a WITH and CASE WHEN statement.

Is it possible to increment an Column : Stock_product in MS SQL inside a WITH and CASE WHEN statement.

I am attempting to sum amount products per day (Column : sum_Per_Day) in my table to get Column : Stock_product (Desired results) but I get Stock_product and the result  that running from old order number and it wasn’t what I had hoped for.

 

My code:

WITH SourceD_ AS (
SELECT b.Orders
,b.shop_no
,b.d_Date
,b.product_no
,a.product_name
,b.Chk_Order
,b.sum_Per_Day
,lag(b.Chk_Order) OVER (ORDER by b.Chk_Order , b.Chk_date) AS Prev_Chk_Order
,lag(b.sum_Per_Day) OVER (ORDER by b.Chk_Order , b.Chk_date) AS Prev_sum_Per_Day
,ROW_NUMBER() OVER (ORDER BY b.Chk_Order , b.Chk_date) AS rn
FROM db_datatest b LEFT JOIN db_Mastertest a ON (b.product_no = a.product_no)
WHERE b.d_Date > getdate()-374)
),
SumD_ AS (
SELECT
*,
SUM(CASE
WHEN (Chk_Order != Prev_Chk_Order) THEN sum_Per_Day
WHEN (ISNULL(sum_Per_Day,0) != 0) THEN sum_Per_Day
WHEN (ISNULL(sum_Per_Day,0) = 0) THEN 0
ELSE 0
END) OVER (ORDER BY rn ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as Stock_product
FROM SourceD_
)
SELECT
*
FROM SumD_
ORDER BY rn;

 

I want the following result Column : Stock_product (Desired results)
by If the orders number changes (new orders number) should use value from column : Stock_product calculate on base column : sum_Per_Day to create new value of column : Stock_product

namemon_4-1715053402708.png

 

Best regard.

1 ACCEPTED SOLUTION
Musadev
Resolver III
Resolver III

Hi @namemon 

 

You need to get the running sum for each order and the order should be on the date, right?

Try to use Windows function and partition the data based on the order and date, the date should be in ascending order. 

Please share sample data as mentioned in the screenshot, I will give a shot to it. Thanks

 

View solution in original post

3 REPLIES 3
Musadev
Resolver III
Resolver III

Hi @namemon 

Has your query been resolved now?

 

If yes then mark the first reply as a Solution. It will help the new members. Thanks

 

Musadev
Resolver III
Resolver III

Hi @namemon 

 

You need to get the running sum for each order and the order should be on the date, right?

Try to use Windows function and partition the data based on the order and date, the date should be in ascending order. 

Please share sample data as mentioned in the screenshot, I will give a shot to it. Thanks

 

This solution is work.

It was very thoughtful of you.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.