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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
PowerUser22
Regular Visitor

Summerize by two columns with condition in the second column

Hello,

 

   I need to summerize the list of flight segment with the same ticket number and whith date of flight is in 48 hours (the passenger could stay 48 hours in transit), so the INPUT table will be :

 

flight date departureticket numberpriceorigin airportdestination airport
01/01/2022123456200JFKLOS
02/01/2022123456170LOSBOS
01/01/2022123466100JFKLOS
03/01/2022123466100LOSBOS
04/01/2022123555100MADBOS
04/01/2022135555100BOSMAD
10/01/2022135555100MADBOS

 

The result will be :

 

flight date departureticket numberpriceflight
01/01/2022123456370JFK-LOS-LOS-BOS
01/01/2022123466100JFK-LOS
03/01/2022123466100LOS-BOS
04/01/2022123555100MAD-BOS
04/01/2022135555100BOS-MAD
10/01/2022135555100MAD-BOS

 

so we summerized the rows with same ticket number and the flight departure date is in 2 days (48 hours) so we are sure that the passenger has stayed in the transit airport.

 

I think that we should create a table and summerize using ticket number and flight departure date is in 2 days , I have succeeded to summerize using fligh departure date and tiket number, so the segment who have the same fligh departure date and same ticket number are summerized, but I don't know how to summerize using date in 2 days. (I think that I should use dateinperiod or datebetween to summerize ).

 

Thanks in advance

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @PowerUser22 

Here is a sample file with the solution https://www.dropbox.com/t/4RQMxBBR2hT1tPbR
You need to create a calculated column that returns the first date if the date-time difference is less than 2 days (48 hours). The new column can be used in the visual instead of the original one which allows automatic groupng by date per ticket number.
1.png2.png
The code of the column is 

Departure Date = 
VAR CurrentDate =
    Flights[flight date departure]
VAR PreviousDate =
    CALCULATE ( 
        MIN ( Flights[flight date departure] ), 
        ALLEXCEPT ( Flights,Flights[ticket number] ) 
    )
VAR TimeSpan =
    CurrentDate - PreviousDate
RETURN 
    IF (
        TimeSpan < 2 && TimeSpan > 0,
        PreviousDate,
        CurrentDate
    )

The measures are

Total Price = SUM ( Flights[price] )
Flight = 
CONCATENATEX ( 
    Flights,
    Flights[origin airport] & " - " & Flights[destination airport],
    UNICHAR ( 10 ),
    Flights[flight date departure],
    ASC
)

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

If you want to create a new table, please check the below attached PBIX file.

Picture1.png

 

 

 

NewTable
VAR conditiontable =
    ADDCOLUMNS (
        Data,
        "@condition",
            IF (
                INT (
                    CALCULATE (
                        MAXX (
                            FILTER (
                                ALL ( Data ),
                                Data[ticket number]
                                    = VAR _currentticket = MAX ( Data[ticket number] ) RETURN _currentticket
                            ),
                            Data[flight date departure]
                        )
                    )
                        - CALCULATE (
                            MINX (
                                FILTER (
                                    ALL ( Data ),
                                    Data[ticket number]
                                        = VAR _currentticket = MAX ( Data[ticket number] ) RETURN _currentticket
                                ),
                                Data[flight date departure]
                            )
                        )
                ) < 2,
                "OK",
                "Next"
            ),
        "@flight",
            Data[origin airport] & "-" & Data[destination airport]
    )
VAR groupOK =
    SELECTCOLUMNS (
        GROUPBY (
            ADDCOLUMNS (
                FILTER ( conditiontable, [@condition] = "OK" ),
                "@flightsummary",
                    CONCATENATEX (
                        FILTER (
                            conditiontable,
                            [@condition] = "OK"
                                && Data[ticket number] = EARLIER ( Data[ticket number] )
                        ),
                        [@flight],
                        "-"
                    )
            ),
            Data[ticket number],
            [@flightsummary],
            "@mindate", MINX ( CURRENTGROUP (), Data[flight date departure] ),
            "@pricesum", SUMX ( CURRENTGROUP (), Data[price] )
        ),
        "@FlightDate", [@mindate],
        "@TicketNumber", Data[ticket number],
        "@Flight", [@flightsummary],
        "@price", [@pricesum]
    )
VAR groupNext =
    SELECTCOLUMNS (
        FILTER ( conditiontable, [@condition] = "Next" ),
        "@FlightDate", Data[flight date departure],
        "@TicketNumber", Data[ticket number],
        "@Flight", [@flight],
        "@price", Data[price]
    )
RETURN
    UNION ( groupOK, groupNext )

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

tamerj1
Super User
Super User

Hi @PowerUser22 

Here is a sample file with the solution https://www.dropbox.com/t/4RQMxBBR2hT1tPbR
You need to create a calculated column that returns the first date if the date-time difference is less than 2 days (48 hours). The new column can be used in the visual instead of the original one which allows automatic groupng by date per ticket number.
1.png2.png
The code of the column is 

Departure Date = 
VAR CurrentDate =
    Flights[flight date departure]
VAR PreviousDate =
    CALCULATE ( 
        MIN ( Flights[flight date departure] ), 
        ALLEXCEPT ( Flights,Flights[ticket number] ) 
    )
VAR TimeSpan =
    CurrentDate - PreviousDate
RETURN 
    IF (
        TimeSpan < 2 && TimeSpan > 0,
        PreviousDate,
        CurrentDate
    )

The measures are

Total Price = SUM ( Flights[price] )
Flight = 
CONCATENATEX ( 
    Flights,
    Flights[origin airport] & " - " & Flights[destination airport],
    UNICHAR ( 10 ),
    Flights[flight date departure],
    ASC
)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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