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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.