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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Gio_rgio_DLTT
New Member

Calculate Invoice

Hi,

i have a dateset like this: 

Date               Contract         Value   Monthly Total    Invoice Date      Amount to paid
01/01/2023 Contract 118038001/01/2023 
01/01/2023 Contract 120038001/01/2023 
01/02/2023 Contract 18001000  
01/02/2023 Contract 12001000  
01/03/2023 Contract 115055001/03/20231380
01/03/2023 Contract 140055001/03/20231380
01/04/2023 Contract 1350950  
01/04/2023 Contract 1600950  
01/05/2023 Contract 122033001/05/20231500 
01/05/2023 Contract 111033001/05/20231500 
01/01/2023Contract 25015001/01/2023 
01/01/2023Contract 210015001/01/2023 
01/02/2023Contract 2 20035001/02/2023150
01/02/2023Contract 2 15035001/02/2023150
01/03/2023Contract 2 10030001/03/2023350
01/03/2023Contract 2 20030001/03/2023350

The value column the monthly value, while the "Monthly total" column tells me the monthly total (given by the sum of the values over the same month). For example, Contract 1, date 01/01/2023, 380 "monthly total"(180+380). The "Invoice Date" column tells me the date on which the billing is to take place.

I should find a way to calculate the amount to be paid for each billing date (Column "Amount to be paid"). For example: Contract 1; Invoice Date 01/03/2023; Amount to be paid: 1380 (1000 of 01/02/2023 + 380 of 01/01/2023).

So, with respect to the invoice date, i have to consider the months before until the previous billing.

I tried something like this thanks to another post, but it doesn't work:

 

Amount to be paid =

  var thisBillDate = table[Invoice Date]

  var prevBillDate = CALCULATE(

    MAX(table[invoice Date]),

    ALLEXCEPT(table, table[Contract]),

   table[Invoice Date] < thisBillDate

  )

  var result = CALCULATE(

    SUM(table[Monthly total]),

    ALLEXCEPT(tale, table[Contract], table[Date]),

    prevBillDate < table[Invoice Date] &&  table[Invoice Date] <= thisBillDate)

    return IF(NOT ISBLANK(table[Invoice Date]), COALESCE(result, 0))

 

Contract dates without amounts are empty because they are the "Stard date" of the contract, therefore they have no values in previous months. So contract 1 for example, starts on 01/01/2023. The first invoicing takes place on 01/03/2023 and the amount is given by the monthly values between 01/01/2023 and 01/03/2023 (therefore January and February). The second on 01/05/2023 and the range is between 01/03/2023 (previous invoice) and 01/05 2023 and so on..

 

Can someone help me? Thanks

 

 

 

1 REPLY 1
Anonymous
Not applicable

HI @Gio_rgio_DLTT,

Perhaps you can try to use the following measure formula if it help with your scenario:

Amount to be paid =
VAR CurrBillDate = table[Invoice Date]
VAR prevBillDate =
    CALCULATE (
        MAX ( table[invoice Date] ),
        FILTER ( ALLSELECTED ( 'table' ), table[Invoice Date] < CurrBillDate ),
        VALUES ( table[Contract] )
    )
VAR result =
    CALCULATE (
        SUM ( table[Monthly total] ),
        FILTER (
            ALLSELECTED ( 'table' ),
            table[Invoice Date] > prevBillDate
                && table[Invoice Date] <= CurrBillDate
        ),
        VALUES ( table[Contract] )
    )
RETURN
    IF ( ISBLANK ( table[Invoice Date] ) = FALSE (), result + 0 )

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.