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
Roym
Helper III
Helper III

calculate/show result in column based on previous result

With some help from this community I have the following Dax code that is doing the following:

 

1. If the test is effective, and the review is also effective -> Column= Effective
2. If test is effective, and review is Ineffective -> Column = Ineffective 
3. If the test is effective, and the review is Open -> Column = the review result from the previous assessment.
(Review is always prefered above the test result)
 
This works perfectly in almost all instances. But if you look at the below table, it goes 'wrong' with the last two items of session 5. In both cases the test is effective and the review is open. With the item before that it works as it shows in column 'effective' but as these two don't have a review result (open) for the previous instance it is showing open. But it should show effective. Is there a way to achieve this result?

 

Column =
var _previosid=MAXX(filter('Table','Table'[session]=EARLIER('Table'[session])&& 'Table'[ID]< EARLIER('Table'[ID])),'Table'[ID])
var _review=CALCULATE(MAX('Table'[Review]),FILTER('Table','Table'[ID]=_previosid))
VAR _result=
SWITCH(TRUE(),'Table'[Test]="Effective" && 'Table'[Review]="Effective","Effective",
'Table'[Test]="Effective" && 'Table'[Review]="Ineffective","Ineffective",
'Table'[Test]="Effective" && 'Table'[Review]="Open",_review)

return if (_result="",'Table'[Test],_result)
 
Capture.PNG
1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

HI @Roym 
Please try

Column =
VAR CurrentTest = 'Table'[Test]
VAR CurrentReview = 'Table'[Review]
VAR CurrentID = 'Table'[ID]
VAR CurrentsessionTable =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[session] ) )
VAR PreviousRecords =
    FILTER (
        CurrentsessionTable,
        'Table'[ID] < CurrentID
            && 'Table'[Review] <> "Open"
    )
VAR PreviousRecord =
    TOPN ( 1, PreviousRecords, 'Table'[ID] )
VAR PreviousReview =
    MAXX ( PreviousRecord, 'Table'[Review] )
VAR Result =
    IF (
        CurrentTest = "Effective",
        SWITCH (
            CurrentReview,
            "Effective", "Effective",
            "Ineffective", "Ineffective",
            "Open", PreviousReview
        )
    )
RETURN
    COALESCE ( Result, 'Table'[Test] )

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

HI @Roym 
Please try

Column =
VAR CurrentTest = 'Table'[Test]
VAR CurrentReview = 'Table'[Review]
VAR CurrentID = 'Table'[ID]
VAR CurrentsessionTable =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[session] ) )
VAR PreviousRecords =
    FILTER (
        CurrentsessionTable,
        'Table'[ID] < CurrentID
            && 'Table'[Review] <> "Open"
    )
VAR PreviousRecord =
    TOPN ( 1, PreviousRecords, 'Table'[ID] )
VAR PreviousReview =
    MAXX ( PreviousRecord, 'Table'[Review] )
VAR Result =
    IF (
        CurrentTest = "Effective",
        SWITCH (
            CurrentReview,
            "Effective", "Effective",
            "Ineffective", "Ineffective",
            "Open", PreviousReview
        )
    )
RETURN
    COALESCE ( Result, 'Table'[Test] )

Great, this works perfectly!! Thanks!!

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.