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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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.
AlB
Community Champion
Community Champion

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
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.