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
larsm11
Frequent Visitor

Need help to make calculations based on a selection of matching rows in a related table

Hi - I'm relatively new to DAX, and I have the following issue that I cannot get passed.

I have a table with invoices, and an related table (they share InvoiceID) with all ledger postings related to these invoices.

I want to create a custom column in the Invoice table with payment days (using DAX) based on the following rationale:

  • Find all postings in the Postings table with matching invoiceID as the row in the invoice table (e.g. 77536391 in the sample)
  • Calculate the days between date of the posting with the highest positive Amount value and the matching posting with the highest negative amount
  • Add the calculated number to the Payment Days custom column in the Invoice table

Hopeful someone can help.

/LarsM

Invoice and Postings table extract.

Invoice table extractInvoice table extractPostings table extractPostings table extract

2 REPLIES 2
Anonymous
Not applicable

 

[Payment Days] =  // calc column
VAR __maxAmount = 
	MAXX(
		RELATEDTABLE( Postings ),
		Postings[Amount]
	)
var __minAmount =
	MINX(
		RELATEDTABLE( Postings ),
		Postings[Amount]
	)
var __amountsAreCorrect =
	__maxAmount > 0
	&& ( __maxAmount + __minAmount = 0 )
var __dayOfMaxAmount =
	CALCULATE(
		VALUES( Postings[Date] ),
		Postings[Amount] = __maxAmount
	)
var __dayOfMinAmount =
	CALCULATE(
		VALUES( Postings[Date] ),
		Postings[Amount] = __minAmount
	)
var __onlyOnePairOfDatesFound =
	COUNTROWS( __dayOfMinAmount ) = 1
	&&
	1 = COUNTROWS( __dayOfMaxAmount )
var __shouldCalculate =
	__amountsAreCorrect && __onlyOnePairOfDatesFound
RETURN
if( __shouldCalculate, int(__dayOfMaxAmount - __dayOfMinAmount) )

 

 

This should cover all the problems you might have in the Postings table. For instance, there could not be a matching negative amount.. and the likes.

 

Best

D

Mariusz
Community Champion
Community Champion

Hi @larsm11 

 

Try this 

Column = 
VAR __amounts = 
    CALCULATETABLE( 
        {
            MIN( Postings[Amount] ),
            MIN( Postings[Amount] ) 
        }
    )
RETURN 
CALCULATE(
    DATEDIFF( MIN( Postings[Date] ), MAX( Postings[Date] ), DAY ),
    TREATAS( __amounts, Postings[Amount] )
)

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

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.

Top Solution Authors