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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
kdesai
Frequent Visitor

Help with a KPI calulation in Power Bi

Hi,

 

I am trying to calculate a KPI for each patient, the KPI is called "Initial prescription start date(IPST)".

 

The definition of IPST is if the patient has a negative history of using that particular medication for 60 days before a start date that start date is a IPST.

 

For example- See screen shot below, for patient with ID=101, I will start with IPST as 4/15/2019 , the difference in days between 4/15/2019 and 4/1/2019 is 14 <60 thus I will change my IPST to 4/1/2019.

Continuing with this iteration IPST for 101 is 3/17/2019 and 102 is 3/18/2018 as shown on the right hand side 

table.

 

Can someone help to implement this logic in power BI. Looks like I will need to implement a loop, not sure how to do that using power bi.

ipst.PNG

1 ACCEPTED SOLUTION

Hey @kdesai ,

 

besides my question, about the date for patient ID 102,

Here you will find a pbix file that contains some sample data:

The DAX for column theDate:

theDate = 
var thisPatientID = 'Table1'[Patient]
var thisDate = 'Table1'[Date]
var theThresholdDate = 
    MAXX(
        FILTER(
            ADDCOLUMNS(
                ADDCOLUMNS(
                    SUMMARIZE(
                        FILTER(
                            ALL('Table1')
                            ,'Table1'[Patient] = thisPatientID
                        )
                        ,Table1[Patient]
                        ,Table1[Date]
                    )
                    ,"succeeding date",
                        CALCULATE(
                            MIN('Table1'[Date])
                            ,var newDate = [Date]
                            return
                            FILTER(
                                ALL(Table1)
                                ,[Patient] = thisPatientID && [Date] > newDate
                            )
                        ) 
                )
                ,"days", DATEDIFF([Date],[succeeding date],DAY)
            )
            ,[days] >= 60
        )
        ,[Date]
    )
return
MINX(
    FILTER(
            ADDCOLUMNS(
                ADDCOLUMNS(
                    SUMMARIZE(
                        FILTER(
                            ALL('Table1')
                            ,'Table1'[Patient] = thisPatientID
                        )
                        ,Table1[Patient]
                        ,Table1[Date]
                    )
                    ,"succeeding date",
                        CALCULATE(
                            MIN('Table1'[Date])
                            ,var newDate = [Date]
                            return
                            FILTER(
                                ALL(Table1)
                                ,[Patient] = thisPatientID && [Date] > newDate
                            )
                        ) 
                )
                ,"days", DATEDIFF([Date],[succeeding date],DAY)
            )
            ,[Patient] = thisPatientID && [Date] > theThresholdDate
        )
        ,[Date]
)
    

is addmittedly a more complex DAX statement, but this is due to the hidden complexities of your question.

 

From my understanding you are looking for the date that is the 1st date after a period if 60 days.

 

Here you will see a little screenshot:

image.png

Please be aware that I used different values for the Patient column. The columns "the date diff" and "the succeeding date" are not relvant. I just used them to check intermediate results.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

Hey, 

 

can you please explain why the data for patient 102 is 2018-03-18 and not 2019-03-15?

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Hey @kdesai ,

 

besides my question, about the date for patient ID 102,

Here you will find a pbix file that contains some sample data:

The DAX for column theDate:

theDate = 
var thisPatientID = 'Table1'[Patient]
var thisDate = 'Table1'[Date]
var theThresholdDate = 
    MAXX(
        FILTER(
            ADDCOLUMNS(
                ADDCOLUMNS(
                    SUMMARIZE(
                        FILTER(
                            ALL('Table1')
                            ,'Table1'[Patient] = thisPatientID
                        )
                        ,Table1[Patient]
                        ,Table1[Date]
                    )
                    ,"succeeding date",
                        CALCULATE(
                            MIN('Table1'[Date])
                            ,var newDate = [Date]
                            return
                            FILTER(
                                ALL(Table1)
                                ,[Patient] = thisPatientID && [Date] > newDate
                            )
                        ) 
                )
                ,"days", DATEDIFF([Date],[succeeding date],DAY)
            )
            ,[days] >= 60
        )
        ,[Date]
    )
return
MINX(
    FILTER(
            ADDCOLUMNS(
                ADDCOLUMNS(
                    SUMMARIZE(
                        FILTER(
                            ALL('Table1')
                            ,'Table1'[Patient] = thisPatientID
                        )
                        ,Table1[Patient]
                        ,Table1[Date]
                    )
                    ,"succeeding date",
                        CALCULATE(
                            MIN('Table1'[Date])
                            ,var newDate = [Date]
                            return
                            FILTER(
                                ALL(Table1)
                                ,[Patient] = thisPatientID && [Date] > newDate
                            )
                        ) 
                )
                ,"days", DATEDIFF([Date],[succeeding date],DAY)
            )
            ,[Patient] = thisPatientID && [Date] > theThresholdDate
        )
        ,[Date]
)
    

is addmittedly a more complex DAX statement, but this is due to the hidden complexities of your question.

 

From my understanding you are looking for the date that is the 1st date after a period if 60 days.

 

Here you will see a little screenshot:

image.png

Please be aware that I used different values for the Patient column. The columns "the date diff" and "the succeeding date" are not relvant. I just used them to check intermediate results.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.