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
Molin
New Contributor II

Calculating Payback period using DAX

Dear community, 

I am struggeling with at DAX measure to calculate (count) the payback period in months. Unfortunately, I have little flexibility in datamodel, meaning calculated columns is not an options.

Working on a DAX measure, I need to count the months when "Cashflow Rolling" is bigger than "Total Investment" (see picture below). Both "Cashflow Rolling" and "Total Investment" is measures, with "Total Investment" having a REMOVEFILTERS on the DimDate table [YearMonth] is related to. 


In excel I would use =MATCH(), however I cannot find a similar measure in DAX.  


Thank you in advance

Molin_0-1758195231638.png

 

 

1 ACCEPTED SOLUTION
tayloramy
Contributor

Hi @Molin

 

What you are running into is expected: DAX measures do not have an Excel-style MATCH that scans an array. Measures evaluate in the current filter context, so to compute a payback period you need to scan the month set yourself and find the first month where your cumulative cash flow meets or exceeds the (context-constant) Total Investment. We can do that with CALCULATE + FILTER over the date axis (docs: CALCULATE, ALLSELECTED).

Try this: 

Payback Months :=
VAR Invest = [Total Investment]             -- expected to ignore DimDate via REMOVEFILTERS
VAR FirstPaybackDate =
    CALCULATE(
        MIN ( 'DimDate'[Date] ),            -- the first date where rolling >= invest
        FILTER(
            ALLSELECTED ( 'DimDate'[Date] ),-- scan the visible date range (respects slicers)
            [Cashflow Rolling] >= Invest
        )
    )
VAR StartDate =
    CALCULATE( MIN ( 'DimDate'[Date] ), ALLSELECTED ( 'DimDate'[Date] ) )
RETURN
IF (
    ISBLANK ( FirstPaybackDate ),
    BLANK(),                                -- no payback in the selected window
    DATEDIFF ( StartDate, FirstPaybackDate, MONTH ) + 1
)

 

If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution. 

View solution in original post

3 REPLIES 3
tayloramy
Contributor

Hi @Molin

 

What you are running into is expected: DAX measures do not have an Excel-style MATCH that scans an array. Measures evaluate in the current filter context, so to compute a payback period you need to scan the month set yourself and find the first month where your cumulative cash flow meets or exceeds the (context-constant) Total Investment. We can do that with CALCULATE + FILTER over the date axis (docs: CALCULATE, ALLSELECTED).

Try this: 

Payback Months :=
VAR Invest = [Total Investment]             -- expected to ignore DimDate via REMOVEFILTERS
VAR FirstPaybackDate =
    CALCULATE(
        MIN ( 'DimDate'[Date] ),            -- the first date where rolling >= invest
        FILTER(
            ALLSELECTED ( 'DimDate'[Date] ),-- scan the visible date range (respects slicers)
            [Cashflow Rolling] >= Invest
        )
    )
VAR StartDate =
    CALCULATE( MIN ( 'DimDate'[Date] ), ALLSELECTED ( 'DimDate'[Date] ) )
RETURN
IF (
    ISBLANK ( FirstPaybackDate ),
    BLANK(),                                -- no payback in the selected window
    DATEDIFF ( StartDate, FirstPaybackDate, MONTH ) + 1
)

 

If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution. 

Molin
New Contributor II

Hi @tayloramy

Thank you so much for your time, it worked wonders! 

Much appreciated. 

Kind regards

v-menakakota
Honored Contributor II

Hi @Molin ,
Thanks for reaching out to the Microsoft fabric community forum. 

 

I would also take a moment to thank @tayloramy โ€ฏ , for actively participating in the community forum and for the solutions youโ€™ve been sharing in the community forum. Your contributions make a real difference.

I hope the below details help you fix the issue. If you still have any questions or need more help, feel free to reach out. Weโ€™re always here to support you 

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.โ€ฏโ€ฏ 

Best Regards, 
Community Support Team  

Helpful resources

Announcements
Users online (7,584)