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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
avinash2807
Regular Visitor

Calculate Closing Balance for Week in Every Month

I want to calculate closing balance of week , 

I have two tables one sales table with sales on each day and another calendar table with each day , week number, and week range 

 

Sales Table 

CalendarDate  QtyAvailable Category

2022-07-01     30                  Category1

2022-07-01     15                  Category2

2022-07-02     28                  Category1

2022-07-02     15                  Category2

2022-07-03     12                  Category2

2022-07-03     26                  Category2

2022-07-04     32                  Category2

2022-07-04     22                  Category2

2022-07-05     36                  Category2

2022-07-05     16                  Category2 

2022-07-06     23                  Category2

2022-07-06     33                  Category2

2022-07-07     24                  Category2

2022-07-07     24                  Category2

2022-07-08     13                  Category2

2022-07-08     13                  Category2

2022-07-09     44                  Category2

2022-07-09     24                  Category2

 

CalendarTable WeekNumber  WeekRange

2022-07-01      27                    June26-Jul2

2022-07-02      27                    June26-Jul2

2022-07-03      28                    July-3-July9

2022-07-04      28                    July-3-July9

2022-07-05      28                    July-3-July9

2022-07-06      28                    July-3-July9

2022-07-07      28                    July-3-July9

2022-07-08      28                    July-3-July9

2022-07-09      28                    July-3-July9

 

I want to display Qty available on Last Day of Each Week 

 

WeekRange     QtyAvailable

June26-Jul2      43 --- Sum of Qty Available on July 2 which is weeks last day across all catgories (28 for Cat1 and 15 Cat2 , Sum=43)

July-3-July9      68---Sum of  Qty Available on July 9 which is weeks last day across all catgories ( 44 for Cat1 and 24 Cat2 , Sum=68)

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

lastnonblankdate = 
LASTNONBLANK ( Sales[CalendarDate], CALCULATE ( SUM ( Sales[QtyAvailable] ) ) )

 

lastnonblankvaue by category: =
VAR _lastnonblankdatebycategory =
    ADDCOLUMNS (
        SUMMARIZE ( Sales, Category[Category] ),
        "@lastnonblankdate", [lastnonblankdate]
    )
RETURN
    IF (
        HASONEVALUE ( CalendarTable[WeekRange] ),
        CALCULATE (
            SUM ( Sales[QtyAvailable] ),
            TREATAS (
                _lastnonblankdatebycategory,
                Category[Category],
                CalendarTable[CalendarDate]
            )
        )
    )

 


 

    Microsoft MVP
 

 

   


      If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


   


     
        LinkedInVisit my LinkedIn page
     

   


   


     
        Outlook BookingSchedule a short Teams meeting to discuss your question

     

   


 


View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

lastnonblankdate = 
LASTNONBLANK ( Sales[CalendarDate], CALCULATE ( SUM ( Sales[QtyAvailable] ) ) )

 

lastnonblankvaue by category: =
VAR _lastnonblankdatebycategory =
    ADDCOLUMNS (
        SUMMARIZE ( Sales, Category[Category] ),
        "@lastnonblankdate", [lastnonblankdate]
    )
RETURN
    IF (
        HASONEVALUE ( CalendarTable[WeekRange] ),
        CALCULATE (
            SUM ( Sales[QtyAvailable] ),
            TREATAS (
                _lastnonblankdatebycategory,
                Category[Category],
                CalendarTable[CalendarDate]
            )
        )
    )

 


 

    Microsoft MVP
 

 

   


      If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


   


     
        LinkedInVisit my LinkedIn page
     

   


   


     
        Outlook BookingSchedule a short Teams meeting to discuss your question

     

   


 


Hello @Jihwan_Kim ,
I wanted to use your method but I encounter a problem with it and I can't find a way to make it works. The issue is that how do I handle cases where a category has not been updated in a week range. I need to still use the last QuantityAvailable. For exemple with these data : 

og_gremlins_0-1702243599476.png

If my week range is Jan08-Jan14, final balance should be : 95808,99 + (-1646984,4056) + 91288,25 = -1459887,17

However with the current formula it ignores the value that is not in WeekRange. How could we tackle this issue. The aim is then to display this measure in matrix with WeekRange as column. What are your thoughts or ideas about this ?

works very well, thanks @Jihwan_Kim for the quick help

Helpful resources

Announcements
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.