Forum Discussion

Ryukatan10's avatar
Ryukatan10
Regular Visitor
4 years ago
Solved

Considering Specific Days Between Two Dates

Hello everyone, I'm stuck with this issue and don't know how to solve it.

 

I have a database that show several rows like this one:

 

FlightAcftMonTueWedThuFriSatSunBegin OperationEnd Operation
5467C208000000705/01/202206/27/2022

 

In this case, I have a flight registration that will ocurr from 05/01/2022 to 06/27/2022, only at Sunday, because all the others weekday are "0" in this row.

 

I would like that Power BI shows or undertand something like that:

 

FlightDate
546705/01/2022
546705/08/2022
546705/15/2022
546705/22/2022
546705/29/2022
546706/05/2022
546706/12/2022
546706/19/2022
546706/26/2022

 

I already have another table with dates in one column and weekday in number from 1 to 7 in other column, but even if I create a relationship with al the seven columns form the flights database with this table of dates, it not work in the way that i want.

 

Can Anyone Help Me?

  • Ryukatan10 
    Here is a sample file with the solution https://www.dropbox.com/t/08giOHP4Wl2Oy6Q7

    You need first to unpivote the weekday columns in the flights table:

    Rename the "Value" column

    Delete the "Attribute" column 

    Filter out Zero values

    Create the relationship with the Date table (Make sure the Weekday numbering system is the same in both tables). In your real data the relationship will be Many to Many

    Create the table visual (Flight > from Flights table and Date > from Date table)

    Create the Filter measure that will filter only the existing Dates between "Begining" and "End"

     

     

     

     

    Flight Dates = 
    COUNTROWS ( 
        FILTER (  
            'Date',
            'Date'[Date] >= MAX( Flights[Begin Operation] )
                && 'Date'[Date] <= MAX ( Flights[End Operation] )
        )
    )

     

     

     

     

    Place the measure in the Filter Pane and select "Is not blank" then apply

5 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Ryukatan10 
    Here is a sample file with the solution https://www.dropbox.com/t/08giOHP4Wl2Oy6Q7

    You need first to unpivote the weekday columns in the flights table:

    Rename the "Value" column

    Delete the "Attribute" column 

    Filter out Zero values

    Create the relationship with the Date table (Make sure the Weekday numbering system is the same in both tables). In your real data the relationship will be Many to Many

    Create the table visual (Flight > from Flights table and Date > from Date table)

    Create the Filter measure that will filter only the existing Dates between "Begining" and "End"

     

     

     

     

    Flight Dates = 
    COUNTROWS ( 
        FILTER (  
            'Date',
            'Date'[Date] >= MAX( Flights[Begin Operation] )
                && 'Date'[Date] <= MAX ( Flights[End Operation] )
        )
    )

     

     

     

     

    Place the measure in the Filter Pane and select "Is not blank" then apply

  • Ryukatan10's avatar
    Ryukatan10
    Regular Visitor

    Hi tamerj1  the second table is like this:

     

    DataDia da Semana
    01.jan.20222
    02.jan.20223
    03.jan.20224
    04.jan.20225
    05.jan.20226
    06.jan.20227
    07.jan.20221
    08.jan.20222
    09.jan.20223
    10.jan.20224
    11.jan.20225
    12.jan.20226
    13.jan.20227
    14.jan.20221
    15.jan.20222
    16.jan.20223
    17.jan.20224
    18.jan.20225

    I have the dates and a number that identifies the Weekday Name, 1 for Monday and 7 for Sunday

  • daXtreme's avatar
    daXtreme
    Solution Sage
    // All you need is a middleman table
    // that you'll then join to both
    // the Flights table and the one with
    // dates. It'll be a helper, so it's
    // best to hide it in the UI as it
    // should only be used by measures.
    // It's best to do such things in Power
    // Query but if your data is not too
    // large, then you can also do it via
    // DAX. The Flights table should also
    // be restructured so that the days
    // are not columns but values in a column.
    // The dataset you have right now is not
    // in optimal format for PBI consumption.
    // However, if you insist on this format,
    // here's the code:
    FlightDateMiddleman = // new table
    selectcolumns(
        generate(
            Flights,
            var dateStart = Flights[Begin Operation]
            var dateEnd = Flights[End Operation]
            // because of the format of your table,
            // you have to do such ugly stunts...
            var dayStepSize = 
                maxx(
                    {   Flights[Mon],
                        Flights[Tue],
                        Flights[Wed],
                        Flights[Thu],
                        Flights[Fri],
                        Flights[Sat],
                        Flights[Sun]
                    },
                    [Value]
                )
            var dates = 
                SELECTCOLUMNS(
                    GENERATESERIES( dateStart, dateEnd, dayStepSize ),
                    "@Date", [Value]
                )
            return
                dates
        ),
        "Flight", Flights[Flight],
        "Date", [@Date]
    )
  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    method 1, in PQ editor, only bases on the table of Flights

    NewStep= #table({"Flight","Date"},List.TransformMany(Table.ToRows(PreviousStepName),each List.Select(List.Dates(Date.From(_{9}),Duration.Days(_{10}-_{9})+1,#duration(1,0,0,0)),(x)=>List.Contains(List.Range(_,2,7),Date.DayOfWeek(x,Day.Monday)+1)),(x,y)=>{x{0},y}))

     

    mehod 2, in dax with the same logic

    NewTable=SELECTCOLUMNS(GENERATE(Flights,FILTER(DATESBETWEEN(DateTable[Date],Flights[Begin Operation],Flights[End Operation]), VAR _wn=WEEKDAY(DateTable[Date],2) RETURN Filghts[Mon]=_wn||Filghts[Tue]=_wn||Filghts[Wed]=_wn||Filghts[Thu]=_wn||Filghts[Fri]=_wn||Filghts[Sat]=_wn||Filghts[Sun]=_wn)),"Flight",Filghts[Flight],"Date",DateTable[Date])