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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Rakshana
Frequent Visitor

Help needed: DAX for opening and closing balance for invoices

Hi all
I have data where payments are made against invoices. Every invoice can have one or many payment vouchers. 
I have written 2 measures 

OpenBal = CALCULATE([Billings] - [Collections],
FILTER(ALL(MasterDate[Date]),MasterDate[Date]<MIN(MasterDate[Date])))

ClosingBal =
var closebal = CALCULATE([Billings] - [Collections] ,
FILTER(ALL(MasterDate[Date]),MasterDate[Date]<=MAX([Date])))

return IF([Collections]<0,0, closebal)  --Sometimes there are debit payments so the negative collections should mean there's no balance for the customer to pay


But when brought in a matrix, the measure calculates line wise and the result shows like this - 

 March
 OpenBalBillingsCollectionsClosingBal
Invoice1 30,00030,0000
 Voucher1 30,00018,00012,000
 Voucher2 30,00012,00018,000


Both Voucher1 and Voucher2 are marked against the same invoice.
Even though total shows 0 here, the individual balances are carried over in the next few months. How can I rectify this.
Any advice/suggestion highly appreciated.
@tamerj1  @Greg_Deckler 

2 REPLIES 2
BeaBF
Super User
Super User

@Rakshana Hi! You can use SUMX and FILTER to iterate through each invoice and its associated vouchers to calculate the open balance correctly:

OpenBal =
SUMX (
FILTER (
ALL ( 'YourTableName' ),
'YourTableName'[InvoiceID] = EARLIER ( 'YourTableName'[InvoiceID] )
&& 'YourTableName'[VoucherDate] < EARLIER ( 'YourTableName'[VoucherDate] )
),
'YourTableName'[Billings] - 'YourTableName'[Collections]
)

ClosingBal =
SUMX (
FILTER (
ALL ( 'YourTableName' ),
'YourTableName'[InvoiceID] = EARLIER ( 'YourTableName'[InvoiceID] )
&& 'YourTableName'[VoucherDate] <= MAX ( 'YourTableName'[VoucherDate] )
),
IF ( 'YourTableName'[Collections] < 0, 0, 'YourTableName'[Billings] - 'YourTableName'[Collections] )
)


Try, BBF

Hey thank you for your response. 
But this throws a syntax error for EARLIER
Is there any other way to calculate Opening and Closing balance without it forming a circular dependency.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.