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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
CTozzi
Resolver I
Resolver I

Count previous rows based on date and restart counting if meets a criteria

Hi everyone,

 

I am on my first steps learning DAX and this is my first post. 

 

I need to count the number of flights before an aircraft reaches a base. The expected result is on the last column below, as you can see, the aircraft flew 3 flights before reaching a base, then 1 and then 4 flights.
I think the best way would be counting the previous rows, based on date and aircraft nbr. And restart every time it reaches a base.

 

Tail NbrTime of DepartureDeparture AirportArrival AirportBase/Not Base# of flights to reach a Base
CXDR12020/01/03 10:39:00EEVCEFEBNotBase 
CXDR12020/01/03 13:20:00EFEBELEFNotBase 
CXDR12020/01/03 15:59:00ELEFEFMNBase3
CXDR12020/01/04 11:27:00EFMNEHAMBase1
CXDR12020/01/04 14:37:00EHAMEOWSNotBase 
CXDR12020/01/04 16:55:00EOWSEHHKNotBase 
CXDR12020/01/05 08:00:00EHHKEEMLNotBase 
CXDR12020/01/05 12:35:00EEMLEFBHBase4
CXDR12020/01/05 15:05:00EFBHEFMHBase1
SSLFE2020/01/01 14:44:00EHAMEHHHBase1
SSLFE2020/01/02 07:17:00EHHHEEEXNotBase 
SSLFE2020/01/02 10:02:00EEEXEPCSNotBase 
SSLFE2020/01/03 10:43:00EPCSLMMNNotBase 
SSLFE2020/01/03 13:18:00LMMNEBBRNotBase 
SSLFE2020/01/04 11:46:00EBBRELTEBase5
SSLFE2020/01/05 13:15:00ELTEEBBRNotBase 
SSLFE2020/01/05 17:40:00EBBRESLLBase1

Thanks in advance,

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @CTozzi 

I believe the value in the last row of your expected result should be 2 rather than 1

# of flights to reach a Base =
VAR previousbasetime_ =
    CALCULATE (
        MAX ( Table1[Time of Departure] );
        ALLEXCEPT ( Table1; Table1[Tail Nbr] );
        Table1[Base/Not Base] = "Base";
        Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
    )
VAR previousNotbasetime_ =
    IF (
        ISBLANK ( previousbasetime_ );
        CALCULATE (
            MIN ( Table1[Time of Departure] );
            ALLEXCEPT ( Table1; Table1[Tail Nbr] );
            Table1[Base/Not Base] = "NotBase";
            Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
        )
    )
RETURN
    IF (
        Table1[Base/Not Base] = "Base";
        IF (
            ISBLANK ( previousbasetime_ );
            CALCULATE (
                COUNT ( Table1[Time of Departure] );
                ALLEXCEPT ( Table1; Table1[Tail Nbr] );
                Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] );
                Table1[Time of Departure] >= previousNotbasetime_
            );
            CALCULATE (
                COUNT ( Table1[Time of Departure] );
                ALLEXCEPT ( Table1; Table1[Tail Nbr] );
                Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] );
                Table1[Time of Departure] > previousbasetime_
            )
        )
    )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

View solution in original post

3 REPLIES 3
AlB
Super User
Super User

Hi @CTozzi 

I believe the value in the last row of your expected result should be 2 rather than 1

# of flights to reach a Base =
VAR previousbasetime_ =
    CALCULATE (
        MAX ( Table1[Time of Departure] );
        ALLEXCEPT ( Table1; Table1[Tail Nbr] );
        Table1[Base/Not Base] = "Base";
        Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
    )
VAR previousNotbasetime_ =
    IF (
        ISBLANK ( previousbasetime_ );
        CALCULATE (
            MIN ( Table1[Time of Departure] );
            ALLEXCEPT ( Table1; Table1[Tail Nbr] );
            Table1[Base/Not Base] = "NotBase";
            Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
        )
    )
RETURN
    IF (
        Table1[Base/Not Base] = "Base";
        IF (
            ISBLANK ( previousbasetime_ );
            CALCULATE (
                COUNT ( Table1[Time of Departure] );
                ALLEXCEPT ( Table1; Table1[Tail Nbr] );
                Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] );
                Table1[Time of Departure] >= previousNotbasetime_
            );
            CALCULATE (
                COUNT ( Table1[Time of Departure] );
                ALLEXCEPT ( Table1; Table1[Tail Nbr] );
                Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] );
                Table1[Time of Departure] > previousbasetime_
            )
        )
    )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

Thank you @AIB, it worked. The only problem is that I can't run the calculation in my full database. I have to filter two or three months each time, otherwise the excel goes down.

Hi @CTozzi 

Can you share the database? You have to share the URL to the file hosted elsewhere: Dropbox, Onedrive... or just upload the file to a site like tinyupload.com (no sign-up required).

See if this version works better in terms of speed/resources consumption:

 

# of flights to reach a Base V2 =
VAR previousbasetime_ =
    MAXX (
        FILTER (
            ALL ( Table1[Time of Departure], Table1[Tail Nbr], Table1[Base/Not Base] ),
            Table1[Tail Nbr] = EARLIER ( Table1[Tail Nbr] )
                && Table1[Base/Not Base] = "Base"
                && Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
        ),
        Table1[Time of Departure]
    )
VAR previousNotbasetime_ =
    IF (
        ISBLANK ( previousbasetime_ ),
        MINX (
            FILTER (
                ALL ( Table1[Time of Departure], Table1[Tail Nbr], Table1[Base/Not Base] ),
                Table1[Tail Nbr] = EARLIER ( Table1[Tail Nbr] )
                    && Table1[Base/Not Base] = "NotBase"
                    && Table1[Time of Departure] < EARLIER ( Table1[Time of Departure] )
            ),
            Table1[Time of Departure]
        )
    )
RETURN
    IF (
        Table1[Base/Not Base] = "Base",
        IF (
            ISBLANK ( previousbasetime_ ),
            COUNTROWS (
                FILTER (
                    ALL ( Table1[Time of Departure], Table1[Tail Nbr] ),
                    Table1[Tail Nbr] = EARLIER ( Table1[Tail Nbr] )
                        && Table1[Time of Departure] >= previousNotbasetime_
                        && Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] )
                )
            ),
            COUNTROWS (
                FILTER (
                    ALL ( Table1[Time of Departure], Table1[Tail Nbr] ),
                    Table1[Tail Nbr] = EARLIER ( Table1[Tail Nbr] )
                        && Table1[Time of Departure] > previousbasetime_
                        && Table1[Time of Departure] <= EARLIER ( Table1[Time of Departure] )
                )
            )
        )
    )

 

 

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors