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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Nazer
Frequent Visitor

Returning a starting and ending value based on another column

Hello,

 

I started using Power Pivot and Power Query for the first time two days ago to analyze a spreadsheet of transactions. Some of the transactions in the spreadsheet are categorized into groups, e.g. "Group 1', "Group 2' and so on. Here's some sample data to illustrate:

 

IndexDateStatement BalanceGroupings
15/04/2019 100Ungrouped
25/04/2019$400Ungrouped
37/04/2019 247Group 1
48/04/2019 300Group 1
59/04/2019 567Group 1
610/04/2019 543Group 1
711/04/2019 912Group 1
812/04/2019 342Group 1
912/04/2019 678Group 1
1014/04/2019 986Group 1
1115/04/2019$200Group 1
1216/04/2019 100Group 2

 

I need assistance in getting the opening and closing value that corresponds to the particular group, I did a fair amount of research but was unable to find a solution. In reference to the sample data above, when analyzing group 1, the value $400 should be returned for the opening balance and $200 for the closing balance. The value I need is in the row before 'Group 1' starts. This is just an example there are more columns such as debits, credits, and account name. There are also gaps between the groupings so the opening balance of group 2 isn't the closing balance of group 1, it would the value in the balance column in the row before group 2 starts. The result I want cannot be calculated by debits or credits, it is independent.

 

Here is how I want the results visualised:

 

example.png

I have already created DAX measures to display the start/end date of the transactions that correspond to these groups and the number of days in that range.

 

Nazer_0-1650454913112.png

These measures are functional and filter correctly based on the 'Groupings' column in my pivot table visualisation.

 

Here are my tables and relationships, all dimension tables including a date dimension table are connected to the main fact table. The balance amount and grouping category is column 'StaBalanceAmount' and 'CategoryGroup' respectively.

erd.jpg

 

Thank you for any assistance.

1 ACCEPTED SOLUTION

Hi @Nazer,

 

I use this table for testing.

vcgaomsft_0-1650874679251.png

Please create two measures.

Opening Statement Blance = 
VAR _group = MAX('Table'[Groupings])
VAR _index = CALCULATE(MIN('Table'[Index])-1,'Table'[Groupings]=_group)
VAR _statement_balance = CALCULATE(SUM('Table'[Statement Balance]),FILTER(ALL('Table'),'Table'[Index]=_index))
RETURN
_statement_balance
Closing Statement Blance = 
VAR _group = MAX('Table'[Groupings])
VAR _date = CALCULATE(MAX('Table'[Date]),'Table'[Groupings]=_group)
VAR _index = CALCULATE(MIN('Table'[Index]),'Table'[Date]=_date)
VAR _statement_balance = CALCULATE(SUM('Table'[Statement Balance]),FILTER(ALL('Table'),'Table'[Index]=_index))
RETURN
_statement_balance

vcgaomsft_1-1650874785594.png

Attached the pbix file for reference.

 

Best Regards,
Community Support Team_Gao

 

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!

How to get your questions answered quickly -- How to provide sample data

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

@Nazer So, you should be able to do something like:

Beginning Balance =
  VAR BeginningDate = FIRSTDATE(fTransactions[Date])
RETURN
  MAXX(FILTER(fTranscations,[Date]=BeginningDate),[Statement Balance])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

Thanks for your help.

 

It definitely helped me progress in the right direction. The issue is for opening balance it is returning the value that corresponds to the first row of Group 1 instead of the one before it, so from my example in the original post it would return $247 instead of $400.

 

For the closing balance, since there are sometimes multiple transactions on the same closing date it is returning the balance amount of the first transaction for the date, not the last one which is what I need. So for example:

 

Nazer_0-1650459994461.png

It is returning $150 instead of the $600 closing amount I need. I will look into possibly referencing the index number, since the right transaction would correspond to the max index number of the date range that is filtered to the groupings.

 

Hi @Nazer,

 

I use this table for testing.

vcgaomsft_0-1650874679251.png

Please create two measures.

Opening Statement Blance = 
VAR _group = MAX('Table'[Groupings])
VAR _index = CALCULATE(MIN('Table'[Index])-1,'Table'[Groupings]=_group)
VAR _statement_balance = CALCULATE(SUM('Table'[Statement Balance]),FILTER(ALL('Table'),'Table'[Index]=_index))
RETURN
_statement_balance
Closing Statement Blance = 
VAR _group = MAX('Table'[Groupings])
VAR _date = CALCULATE(MAX('Table'[Date]),'Table'[Groupings]=_group)
VAR _index = CALCULATE(MIN('Table'[Index]),'Table'[Date]=_date)
VAR _statement_balance = CALCULATE(SUM('Table'[Statement Balance]),FILTER(ALL('Table'),'Table'[Index]=_index))
RETURN
_statement_balance

vcgaomsft_1-1650874785594.png

Attached the pbix file for reference.

 

Best Regards,
Community Support Team_Gao

 

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!

How to get your questions answered quickly -- How to provide sample data

Thank you, yes this solution works. Late reply and I used a slightly different way but I'm sure your version is better.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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