cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors