Forum Discussion
Need help with time intelligent dax
Hi,
I have year to date data, which does not include opening balance. I need to calculate opening balance at the begining of each quarter (which is closing balance of last day of previous year quarter + Opening balance of that year) and then closing balance for that quarter. I have given the example below. Data is quarterly year to date so my dates would be always be same every year. Let me know if any one can help with the DAX to over come this issue.
| Data I have | Data I have | Nature of amount | This is the result I want to calculate | This is the result I want to calculate |
| Amount | Date | Nature of amount | Opening Balance for the year | Closing Balance at the end of each quarter |
| 100.00 | 31/12/2021 | Closing Balance of 2021 | ||
| 20.00 | 31/03/2022 | Cumulative Year to date number | 100 | 120 |
| 30.00 | 30/06/2022 | Cumulative Year to date number | 100 | 130 |
| 50.00 | 30/09/2022 | Cumulative Year to date number | 100 | 150 |
| 80.00 | 31/12/2022 | Cumulative Year to date number | 100 | 180 |
| 30.00 | 31/03/2023 | Cumulative Year to date number | 180 | 210 |
| 40.00 | 30/06/2023 | Cumulative Year to date number | 180 | 220 |
| 50.00 | 30/09/2023 | Cumulative Year to date number | 180 | 230 |
| 60.00 | 31/12/2023 | Cumulative Year to date number | 180 | 240 |
| 40.00 | 31/03/2024 | Cumulative Year to date number | 240 | 280 |
| 60.00 | 30/06/2024 | Cumulative Year to date number | 240 | 300 |
| 80.00 | 30/09/2024 | Cumulative Year to date number | 240 | 320 |
| 90.00 | 31/12/2024 | Cumulative Year to date number | 240 | 330 |
Thank you.
Regards,
Prateek
visitprateek
I added a dates table as it supports time intelligence functions and it will be useful for your model, you may extend it with additional columns for various uses.
Create the following measures:Total Amount = SUM(Data[Amount])Opening Balance - Year = VAR __CurrDate = MAX('Dates'[Date]) VAR __Result = IF( [Total Amount] <>BLANK(), CALCULATE( [Total Amount], MONTH( 'Dates'[Date] ) = 12, YEAR('Dates'[Date]) < YEAR( __CurrDate) ) ) RETURN __ResultClosing Balance - Qtr End = [Opening Balance - Year] + [Total Amount]File is attached
7 Replies
- DataNinja777
Super User
Hi visitprateek ,
To calculate the Opening Balance at the beginning of each quarter and the Closing Balance at the end of each quarter in Power BI, we need to retrieve the Closing Balance of the last day of the previous year and ensure that the Opening Balance is correctly carried over at the start of each quarter. Since the data consists of quarterly year-to-date values, the Opening Balance for a given year is derived from the Closing Balance of the previous year.
The following DAX measure calculates the Opening Balance for the Year by retrieving the Closing Balance of the last day of the previous year:
Opening Balance for the Year = VAR PreviousYearClosing = CALCULATE( MAX('Table'[Amount]), 'Table'[Date] = MAX('Table'[Date]) - 1, 'Table'[Nature of amount] = "Closing Balance of 2021" ) VAR FirstDateOfYear = CALCULATE( MIN('Table'[Date]), ALLEXCEPT('Table', 'Table'[Year]) ) RETURN IF( MAX('Table'[Date]) = FirstDateOfYear, PreviousYearClosing, BLANK() )To calculate the Closing Balance at the End of Each Quarter, we simply retrieve the Year-to-Date amount at each quarter-end:
Closing Balance at End of Quarter = MAX('Table'[Amount])For the Opening Balance at the Start of Each Quarter, we ensure that it carries over correctly from the previous quarter:
Opening Balance for Each Quarter = VAR PreviousQuarterClosing = CALCULATE( MAX('Table'[Amount]), 'Table'[Date] = MAX('Table'[Date]) - 1 ) VAR OpeningBalanceYear = CALCULATE( MAX('Table'[Amount]), 'Table'[Date] = MAX('Table'[Date]) - 365 ) RETURN IF( MAX('Table'[Date]) = MINX(FILTER('Table', 'Table'[Year] = YEAR(MAX('Table'[Date]))), 'Table'[Date]), OpeningBalanceYear, PreviousQuarterClosing )With these measures, the Opening Balance for each year correctly takes the previous year's Closing Balance, while the Opening Balance for each quarter derives from the previous quarter's Closing Balance. The Closing Balance remains the quarterly Year-to-Date value provided in the dataset. This ensures that each quarter starts with the correct cumulative balance while maintaining consistency across years.
Best regards,
- visitprateekFrequent Visitor
Hi, Thank you for your quick reply. I tried the DAX but still not getting the desired outcome. Also in the first DAX, I dont have any column for Nature of Amount, that was only for the information.
- Fowmy
Super User
visitprateek
I added a dates table as it supports time intelligence functions and it will be useful for your model, you may extend it with additional columns for various uses.
Create the following measures:Total Amount = SUM(Data[Amount])Opening Balance - Year = VAR __CurrDate = MAX('Dates'[Date]) VAR __Result = IF( [Total Amount] <>BLANK(), CALCULATE( [Total Amount], MONTH( 'Dates'[Date] ) = 12, YEAR('Dates'[Date]) < YEAR( __CurrDate) ) ) RETURN __ResultClosing Balance - Qtr End = [Opening Balance - Year] + [Total Amount]File is attached
- visitprateekFrequent Visitor
Thank you. This is super helpful and easy to understand 🙂
- ThxAlot
Super User
- visitprateekFrequent Visitor
Thank you. This is helpful 🙂
- Ashish_Mathur
Super User
Hi,
Share the raw data table. Also, do you want to write a measure or a calculated column formula?