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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
bonjourposte
Advocate III
Advocate III

Getting Status from row of previous month

Hi all, I have a big list of loans, with one line per month in the loan term, denoted by the column Accounting Date.  Someone helped me get the "Previous Maturity Date" from the previous month's line, but I can't seem to translate the same code to "Previous Loan Status".  

 

Here is what the table looks like (Loan Status columns in pink).  I want to create the column "PrevLoanStatus":

bonjourposte_0-1725044427763.png

Here is the DAX the person gave me for Previous Maturity Date (it worked):

bonjourposte_0-1725046699938.png

 

When I tried converting that to Previous Loan Status, but it gives an entire column of the very first status when setting up a loan that I thought I had filtered out in Power Query.  
PreviousLoanStatus =
var _cLOAN = LOANHISTSorted[LOAN]
var _AcctDt = LOANHISTSorted[ACCOUNTING_DATE]
var Loansatus = LOANHISTSorted[LOAN_STATUS]
var PrevLoanStatus =
CALCULATE(
    MAX(
        LOANHIST[LOAN_STATUS]),
        FILTER(ALLSELECTED(LOANHISTSorted),
        LOANHISTSorted[LOAN] = _cLOAN
        && LOANHISTSorted[ACCOUNTING_DATE] < _AcctDt))

RETURN
PrevLoanStatus
___________________________________________________________________________
How can I get it to just return a column of the "Loan Status" from the previous Accounting Date for the loan?  
 
Here is my dummy data from that table above:
Loan#Accounting DateLoan Term (mos)Loan StatusPrevLoanStatusMat_DatePrevious Month's Mat_Date
10130-Sep-223REPAYMENTREPAYMENT30-Sep-2230-Sep-22
10131-Oct-223REPAYMENTREPAYMENT30-Nov-2230-Sep-22
10130-Nov-223ARREARS<90REPAYMENT30-Nov-2230-Nov-22
20231-Jan-234ADV_PREIADADV_PREIAD31-Mar-2331-Mar-23
20228-Feb-234REPAYMENTADV_PREIAD31-Mar-2331-Mar-23
20231-Mar-234REPAYMENTREPAYMENT30-Apr-2731-Mar-23
20230-Apr-234REPAYMENTREPAYMENT30-Apr-2730-Apr-27
30328-Feb-232COMMITMENTCOMMITMENT1-Mar-231-Mar-23
30331-Mar-232ADV_POSTIADCOMMITMENT1-Mar-231-Mar-23
40431-Aug-233COMMITMENTCOMMITMENT30-Sep-2330-Sep-23
40430-Sep-233REPAYMENTCOMMITMENT30-Sep-2330-Sep-23
40431-Oct-233REPAYMENTREPAYMENT1-Dec-2430-Sep-23

 

Thank-you so much!

 

EDIT: link to my original question: https://community.fabric.microsoft.com/t5/Desktop/Get-data-from-previous-month-previous-row-but-grou...

11 REPLIES 11
Ashish_Mathur
Super User
Super User

Hi,

Show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Sorry I didn't realize it was unclear.  This is the column I'm wanting to achieve.  

 

bonjourposte_0-1725247845566.png

User SevenHills graciously provided this DAX, but it only partially works.  It works for the first and third loan statuses per loan.  

bonjourposte_1-1725247931048.png

 

 

Hi,

Write these calculated column formulas

Previous maturity date = COALESCE(CALCULATE(MAX(Data[Mat_Date]),FILTER(Data,Data[Loan#]=EARLIER(Data[Loan#])&&Data[Mat_Date]<EARLIER(Data[Mat_Date]))),Data[Mat_Date])
Prev loan status = COALESCE(CALCULATE(MAX(Data[Loan Status]),FILTER(Data,Data[Loan#]=EARLIER(Data[Loan#])&&Data[Accounting Date]<EARLIER(Data[Accounting Date]))),Data[Loan Status])

Hope this helps.

Ashish_Mathur_0-1725277967150.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks so much, @Ashish_Mathur, I'm definitely getting warmer.  Your formula worked in the way that SevenHills' didn't, but there are still some loan numbers that yours didn't work for: the ones where there were three loan statuses- it just took the first and the third.  

 

Here is one where yours WORKED and theirs didn't: 

bonjourposte_1-1725385516559.png

Your DAX

bonjourposte_2-1725385586981.png

 

 

And here is one where theirs captured all three statuses, but don't capture the previous line like yours did:

bonjourposte_0-1725384832277.png

What I was using for theirs (I had to modify it a bit to get the above):

bonjourposte_3-1725385642859.png

 

Somehow the best of both formulas need to be combined.

 

I truly appreciate your time and effort, I am not good with CALCULATE, especially ALLSELECTED!

 

You are welcome.  I cannot help with just an image.  As you can see in my previous post, i got exactly the result that you wanted.  If you did not describe the question properly, now is the time to do so showing all possible scenarios and the expected result.  Also, share data in a format that can be pasted in an MS Excel file.  Sharing a picture is useless.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks, @Ashish_Mathur.  I'm sending one that works and two that don't work.  I'm still trying to understand why the sample data gave no errors.  Could it be that the other columns in the table provide additional context?  

 

https://drive.google.com/drive/folders/1LOYfSdoU05shvP_U3B1fPcGgXk53XGDg?usp=sharing

____________________________________

Here is the DAX you provided:

bonjourposte_0-1725465568773.png

PreviousLoanStatusAshish =
COALESCE(
    CALCULATE(
        MAX(
            LOANHISTSorted[LOAN_STATUS]),
            FILTER(LOANHISTSorted,LOANHISTSorted[LOAN] = EARLIER(LOANHISTSorted[LOAN])
            &&
            LOANHISTSorted[ACCOUNTING_DATE] < EARLIER(LOANHISTSorted[ACCOUNTING_DATE]))),
            LOANHISTSorted[LOAN_STATUS])

______________________________________

Here is SevenHills' Dax:

PreviousLoanStatus7Hills =
var _cal =
CALCULATE(
    MAX(
        LOANHISTSorted[LOAN_STATUS]),
        LOANHISTSorted[ACCOUNTING_DATE] < (LOANHISTSorted[ACCOUNTING_DATE]),
        FILTER(ALLSELECTED(LOANHISTSorted),
        LOANHISTSorted[LOAN] = EARLIER(LOANHISTSorted[LOAN])
        &&
        LOANHISTSorted[ACCOUNTING_DATE] = EARLIER(LOANHISTSorted[ACCOUNTING_DATE])))
   

    RETURN
    COALESCE(_cal, LOANHISTSorted[LOAN_STATUS])
_________________________________________

 

I feel like it should be a combination of the two somehow?

 

Or could it be that the DAX is putting the Loan_Statuses in alphabetical order or something?  Here is a list of possible Statuses:

bonjourposte_1-1725465831153.png

 

 

I am confused.  Just share the download link of 1 Excel file with a column of expected results.  Simple.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Sorry I tested the link twice.  not sure what's wrong.  I'll just repost the question for someone else to answer.  Thanks for all the attention you've put toward my question.

Hi @bonjourposte ,
Your code is working fine in my environment, you can check my screenshot to see if it meets your needs, I hope it helps!

vxingshenmsft_0-1725269907258.png

Hope it helps!

 

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi @v-xingshen-msft , yeah for some reason I'm not getting the same results.  This is what I get with that formula: 

bonjourposte_5-1725386666599.png

 

What I used for that last column:
PreviousLoanStatusXing =
var _cLOAN = MAX(LOANHISTSorted[LOAN])
var _AcctDt = MAX(LOANHISTSorted[ACCOUNTING_DATE])
var _Loanstatus = MAX(LOANHISTSorted[LOAN_STATUS])
var _PreLoanStatus =
CALCULATE(
    MAX(
        LOANHISTSorted[LOAN_STATUS]),
        FILTER(ALLSELECTED(LOANHISTSorted),
        LOANHISTSorted[LOAN] = _cLOAN && LOANHISTSorted[ACCOUNTING_DATE] < _AcctDt)
)
RETURN
_PreLoanStatus

 

lbendlin
Super User
Super User

If "previous row"  always means "prior month"  then you are better off adding a calendar table to your data model and using the default DATEADD(xx,-1,MONTH)  time intelligence function.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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