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
Anonymous
Not applicable

Problem with the column that calculates the previous value of a column

Hi,

 

I have table "prod_snapshot", that is snapshot data of customer records. The table has a calculated column "default tagi", that tags default with an IF-statement, returning either 1 or 0 based on whether the customer is defaulted or not. I need a column that returns the previous value of a default tagi column. 

 

I have used syntax:

previous default = CALCULATE(FIRSTNONBLANK('prod snapshot'[default tagi], 1 ), filter('prod snapshot', 'prod snapshot'[loan_id] = earlier( 'prod snapshot'[loan_id]) && 'prod snapshot'[Year Month order] = earlier('prod snapshot'[Year Month order]))). 
 
The Year Month Order is a column that gives order number for each month in an ascending order.
The problem is that for some customer records (=loan_id) it works and for some it gives the current value of default tagi. 
 
When Year Month Order = 98 previous default returns 1, that is not the previous value of default tagi. 
JCL_3-1648031041354.pngJCL_0-1648030874390.pngJCL_2-1648030966993.png
For this loan_id, it returns the correct value:
JCL_4-1648031120111.pngJCL_0-1648030874390.pngJCL_5-1648031148961.png

 

 Could anyone help me with this?

 

1 ACCEPTED SOLUTION

Replace the CALCULATETABLE with FILTER, that works in the sample I've mocked up.

View solution in original post

10 REPLIES 10
kumarthombre20
Resolver I
Resolver I

@Anonymous could you please try the below DAX code

kumarthombre20_0-1648046566973.png

 

johnt75
Super User
Super User

You can use TOPN

previous default =
var currentLoan = 'Table'[loan_id]
var currentYearMonth = 'Table'[Year Month order]
return SELECTCOLUMNS( TOPN(1, CALCULATETABLE( REMOVEFILTERS('Table'), 
   'Table'[loan_id] = currentLoan && 'Table'[Year Month order] < currentYearMonth ),
   'Table'[Year Month order], DESC),
"@value", 'Table'[default tagi]
)
Anonymous
Not applicable

Thanks for your reply! Unfortunately, it doesn't work. I replaced REMOVEFILTERS() with ALL(), as apparently REMOVEFILTERS cannot be used with calculatetable expression, but still throws this error message: 

JCL_0-1648042520020.png

 

Do you have multiple entries in a month for a loan ? If so you may need to add a unique index column using Power Query and you can then add that index column as another sort on the TOPN, that should only return 1 value per loan then

Anonymous
Not applicable

Yes I have, but I want that the previous default column considerers only those values from default tagi -column that are from different month.

 

I made Index column, that gives different index for every report date: 

Index = RANKX (FILTER ('prod snapshot', EARLIER ('prod snapshot'[loan_id]) = 'prod snapshot'[loan_id]),[report_date],, ASC)
 
Then I tried to plug this into TOPN- function:
previous default =
var currentLoan = 'prod snapshot'[loan_id]
var currentYearMonth = 'prod snapshot'[Year Month order]
return SELECTCOLUMNS( TOPN(1, CALCULATETABLE( ALL('prod snapshot'),
'prod snapshot'[loan_id] = currentLoan && 'prod snapshot'[Year Month order] < currentYearMonth ),
'prod snapshot'[Index], DESC),
"@value", 'prod snapshot'[default tagi]
)
 
It still gives the same error. I am pretty new to power bi, so I am not quite sure about how that function operates.
Could you please show me how it should be included and if the Index column is correctly defined? 

The problem with your index column is that it will give the same result to different entries with the same date, and so you have the same problem as before.

I would use Power Query to add the column. Sort by the date column then add an index column, doesn't matter whether it starts from 0 or 1.

That will guarantee that there will be a unique index and you can use that in TOPN

Anonymous
Not applicable

Thanks, the index column was easy to do in Power Query as you told.

The TOPN-function works now, but now previous default column seems to return"1" for every report date:

 

JCL_1-1648046472550.pngJCL_0-1648046449378.png

 

Was the TOPN -function correct as I posted it or should the Index column be included in a different way?

Replace the CALCULATETABLE with FILTER, that works in the sample I've mocked up.

Anonymous
Not applicable

That works, thanks a lot for your help!

daXtreme
Solution Sage
Solution Sage

define table Loans =
SELECTCOLUMNS(
    {
        (1, 1, 0),
        (1, 2, 0),
        (1, 3, 1),
        ----------
        (2, 1, 0),
        (2, 2, 1),
        (2, 3, 1)
    },
    "loan_id", [Value1],
    "ym_order", [Value2],
    "def_tagi", [Value3]
)
EVALUATE
    ADDCOLUMNS(
        Loans,
        "prev_def",
            var curr_loan_id = Loans[loan_id]
            var curr_ym_order = Loans[ym_order]
            var prev_def_tagi =
                MAXX(
                    filter(
                        Loans,
                        Loans[loan_id] = curr_loan_id
                        &&
                        Loans[ym_order] = curr_ym_order - 1
                    ),
                    Loans[def_tagi]
                )
            return
                COALESCE( prev_def_tagi, 0 )
    )

Run this in DAX Studio. The definition of "prev_def" is what you need.

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