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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Roym
Helper IV
Helper IV

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
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors