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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
abogdanov
Helper I
Helper I

Counting records based on dynamic variable

Hello,

 

I have a table that is a log of changes that our applications go through before they either accepted or denied. What I'm trying to  do here is understand the quality of our applications and customer serivce. Here is what my table looks like:

Application idStatus_changed_toModification_datetime
111Full Application01/01/2021 8:45:45 AM
111Validation01/01/2021 8:51:35 AM
111Verification01/01/2021 9:16:45 AM
111Processing01/02/2021 10:45:45 AM
111Not qualified01/02/2021 11:33:45 AM
222Partial Application01/01/2021 5:43:11 AM
222Full Application01/01/2021 6:22:11 AM
222Validation01/01/2021 10:11:11 AM
222Verification01/01/2021 11:43:11 AM
222Processing01/01/2021 1:25:12 PM
222Approved 01/01/2021 3:13:27 PM

 

so what I'm trying to do is to find a way to tell how many applications go from "partial application"  to "full application"; from "full application" to "validation"  or from "partial application" to "approved" etc. What is a good way to execute this? 

 

I have tried creating columns that show the first and last status of each id using MIN() but since they are located on different rows, I can't count them.  

 

Application idStatus_changed_toModification_datetimeOrig StatusFinal Status
111Full Application01/01/2021 8:45:45 AMFull Application 
111Validation01/01/2021 8:51:35 AM  
111Verification01/01/2021 9:16:45 AM  
111Processing01/02/2021 10:45:45 AM  
111Not qualified01/02/2021 11:33:45 AM Not Qualified
222Partial Application01/01/2021 5:43:11 AMPartial Application 
222Full Application01/01/2021 6:22:11 AM  
222Validation01/01/2021 10:11:11 AM  
222Verification01/01/2021 11:43:11 AM  
222Processing01/01/2021 1:25:12 PM  
222Approved 01/01/2021 3:13:27 PM Approved

 

 

Any tips and tricks are welcome!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @abogdanov ,

 

I doubt the order for From-To type could be reversed, such as from partial application to full application, or it also can be from full application to partial application...

 

So in order to consider comprehensive, I use CROSSJOIN() to create a FromTo table as shown below:

 

FromTo =
VAR _from =
    DISTINCT ( SELECTCOLUMNS ( 'Table', "From", 'Table'[Status_changed_to] ) )
VAR _to =
    DISTINCT ( SELECTCOLUMNS ( 'Table', "To", 'Table'[Status_changed_to] ) )
RETURN
    CROSSJOIN ( _from, _to )

 

FromTo table.PNG

 

Now please follow these steps:

1. Find the first status

 

From =
VAR _firstdate =
    CALCULATE (
        FIRSTNONBLANK ( 'Table'[Modification_datetime], TRUE () ),
        ALLEXCEPT ( 'Table', 'Table'[Application id] )
    )
RETURN
    LOOKUPVALUE ( 'Table'[Status_changed_to], [Modification_datetime], _firstdate )

 

2. Find the last status

 

To =
VAR _lastdate =
    CALCULATE (
        LASTNONBLANK ( 'Table'[Modification_datetime], TRUE () ),
        ALLEXCEPT ( 'Table', 'Table'[Application id] )
    )
RETURN
    LOOKUPVALUE ( 'Table'[Status_changed_to], [Modification_datetime], _lastdate )

 

 

3. Create a measure to count the number of application with each From - To type:

 

Measure =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Application id] ),
    FILTER (
        'Table',
        'Table'[From] = MAX ( 'FromTo'[From] )
            && 'Table'[To] = MAX ( 'FromTo'[To] )
    )
)

 

The final output is shown below:

Counting records based on dynamic variable.PNG

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Please show the exact result which you are expecting.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @abogdanov ,

 

I doubt the order for From-To type could be reversed, such as from partial application to full application, or it also can be from full application to partial application...

 

So in order to consider comprehensive, I use CROSSJOIN() to create a FromTo table as shown below:

 

FromTo =
VAR _from =
    DISTINCT ( SELECTCOLUMNS ( 'Table', "From", 'Table'[Status_changed_to] ) )
VAR _to =
    DISTINCT ( SELECTCOLUMNS ( 'Table', "To", 'Table'[Status_changed_to] ) )
RETURN
    CROSSJOIN ( _from, _to )

 

FromTo table.PNG

 

Now please follow these steps:

1. Find the first status

 

From =
VAR _firstdate =
    CALCULATE (
        FIRSTNONBLANK ( 'Table'[Modification_datetime], TRUE () ),
        ALLEXCEPT ( 'Table', 'Table'[Application id] )
    )
RETURN
    LOOKUPVALUE ( 'Table'[Status_changed_to], [Modification_datetime], _firstdate )

 

2. Find the last status

 

To =
VAR _lastdate =
    CALCULATE (
        LASTNONBLANK ( 'Table'[Modification_datetime], TRUE () ),
        ALLEXCEPT ( 'Table', 'Table'[Application id] )
    )
RETURN
    LOOKUPVALUE ( 'Table'[Status_changed_to], [Modification_datetime], _lastdate )

 

 

3. Create a measure to count the number of application with each From - To type:

 

Measure =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Application id] ),
    FILTER (
        'Table',
        'Table'[From] = MAX ( 'FromTo'[From] )
            && 'Table'[To] = MAX ( 'FromTo'[To] )
    )
)

 

The final output is shown below:

Counting records based on dynamic variable.PNG

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Eyelyn, I think your solution is exactly what I needed! One question though, would the From and To formulas fail if a single id has repetitions? let's say one id went from Validation to Verification and then back to Validation

Mohammad_Refaei
Solution Specialist
Solution Specialist

You can do this with simple and stratighforward approach through adjusting the data model.

Build a calendar table, a table to define the status sequence and simple count.

Please check this sample file.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.