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
DDalmas
Frequent Visitor

[DAX] Counting consecutive days of an event

Hello everyone, 

I have to count the maximum of consecutive days that a vehicle has worked. 
The fact table contains the vehicle_ID, the date and the journey_id. 
Journey_id is the register of the every single time the driver turns on the vehicle. It can happen many times during the day. 

DDalmas_0-1605205467803.png

Is it possible? 

Thanks in advance 🙂 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

@DDalmas 

Following a similar approach to earlier

1. Place Vehicle_Id in a table visual

2. Create this measure

 

Max number Inactive Days per vehicle = 
    VAR summT0_ = DISTINCT( Table1[Date] )
    VAR allDatesInPeriod_ = SELECTCOLUMNS(GENERATESERIES(MIN(Table1[Date]), MAX(Table1[Date])), "Date",[Value])
    VAR summT_ = EXCEPT(allDatesInPeriod_,summT0_)
    VAR auxT_ =
        FILTER (
            ADDCOLUMNS (
                summT_,
                "index_",
                    COUNTROWS ( FILTER ( summT_, [Date] <= EARLIER ( [Date] ) ) ) 
            ),
            VAR nextDate_ =
                MINX ( FILTER ( summT_, [Date] > EARLIER ( [Date] ) ), [Date] )
            RETURN
                 ( nextDate_ - [Date] ) <> 1
        )
    VAR res_ =
        MAXX (
            auxT_,
            [index_]
                - MAXX ( FILTER ( auxT_, [index_] < EARLIER ( [index_] ) ), [index_] )
        )
    RETURN
         res_

 

3. Create this other measure that uses the previous one:

 

Max number Inactive Days per vehicle TOT = 
MAXX ( DISTINCT ( Table1[Vehicle_ID] ), [Max number Inactive Days per vehicle] )

 

 

4. Place [Max number Inactive Days per vehicle TOT] in the visual to see the maximum number of consecutive INACTIVE days per vehicle and the total (i.e the max of the max per vehicle). You can also place this measure in a card visual to see only the max of the max  (for all vehicles)

5. See it all at work in the attached file with a very simplified fact table 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

SU18_powerbi_badge

 

 

View solution in original post

6 REPLIES 6
AlB
Super User
Super User

@DDalmas 

Following a similar approach to earlier

1. Place Vehicle_Id in a table visual

2. Create this measure

 

Max number Inactive Days per vehicle = 
    VAR summT0_ = DISTINCT( Table1[Date] )
    VAR allDatesInPeriod_ = SELECTCOLUMNS(GENERATESERIES(MIN(Table1[Date]), MAX(Table1[Date])), "Date",[Value])
    VAR summT_ = EXCEPT(allDatesInPeriod_,summT0_)
    VAR auxT_ =
        FILTER (
            ADDCOLUMNS (
                summT_,
                "index_",
                    COUNTROWS ( FILTER ( summT_, [Date] <= EARLIER ( [Date] ) ) ) 
            ),
            VAR nextDate_ =
                MINX ( FILTER ( summT_, [Date] > EARLIER ( [Date] ) ), [Date] )
            RETURN
                 ( nextDate_ - [Date] ) <> 1
        )
    VAR res_ =
        MAXX (
            auxT_,
            [index_]
                - MAXX ( FILTER ( auxT_, [index_] < EARLIER ( [index_] ) ), [index_] )
        )
    RETURN
         res_

 

3. Create this other measure that uses the previous one:

 

Max number Inactive Days per vehicle TOT = 
MAXX ( DISTINCT ( Table1[Vehicle_ID] ), [Max number Inactive Days per vehicle] )

 

 

4. Place [Max number Inactive Days per vehicle TOT] in the visual to see the maximum number of consecutive INACTIVE days per vehicle and the total (i.e the max of the max per vehicle). You can also place this measure in a card visual to see only the max of the max  (for all vehicles)

5. See it all at work in the attached file with a very simplified fact table 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

SU18_powerbi_badge

 

 

DDalmas
Frequent Visitor

@AlB Thank you very much for this idea. I will be trying to do it tomorrow.

I am new here, sorry for the print screen. Next time, I will attach the pbix. 

AlB
Super User
Super User

Hi @DDalmas 

Please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. A screen cap doesn't allow people to readily copy the data and run a quick test and thus decreases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here. Or, ideally, share the pbix.

 

1. Place Vehicle_Id in a table visual

2. Create this measure

 

 

 

Max per vehicle = 
VAR summT_ =
    SUMMARIZE ( Table1, Table1[Vehicle_ID], Table1[Date] )
VAR auxT_ =
    FILTER (
        ADDCOLUMNS (
            summT_,
            "index_",
                COUNTROWS ( FILTER ( summT_, [Date] <= EARLIER ( [Date] ) ) ) 
        ),
        VAR nextDate_ =
            MINX ( FILTER ( summT_, [Date] > EARLIER ( [Date] ) ), [Date] )
        RETURN
             ( nextDate_ - [Date] ) <> 1
    )
VAR res_ =
    MAXX (
        auxT_,
        [index_] - MAXX ( FILTER ( auxT_, [index_] < EARLIER ( [index_] ) ), [index_] )
    )
RETURN
    res_

 

 

 

3. Create this other measure that uses the previous one:

 

 

 

Max of Max per vehicle = 
MAXX ( DISTINCT ( Table1[Vehicle_ID] ), [Max per vehicle] )

 

 

 

 4. Place [Max of Max per vehicle] in the visual to see the  maximum number of consecutive days per vehicle and the total (i.e the max of the max per vehicle). You can also place this measure in a card visual to see only the max of the max  (for all vehicles)

 5. See it all at work in the attached file with a very simplified fact table 

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

SU18_powerbi_badge

DDalmas
Frequent Visitor

Dear @AlB 

I've tryed to do it, but it does not seems to work properly. 
Maybe, I haven't explained very well. I have to display in a table with all vehicles, a colum containing the maximum number of day that every single vehicle has not worked. 
Maybe the measure has to count the consecutive blank days, because I did the croossjoin between all vehicles_ids and all possible dates. I don't know if it will work. 
I will attach a sample of the original table from the datawarehouse. It contains the last 10 days. 

table1 

Thanks for your help, again 🙂 


@DDalmas 

I cannot access the link. Seems to require a sign in with a Google account. Either remove that requirement or post it elsewhere (Dropbox, tinyupload.com, etc)

You explained exactly the opposite on your initial posting:

I have to count the maximum of consecutive days that a vehicle has worked.

 

I'd need a more detailed, accurate explanation before maknig another attempt. You talk about inactive days. What will be considered inactive days in your data? Is this on a specific period of time? For instance, if a vehicle has data on these dates (MM/DD/Year):

01/01/2019

01/02/2019

01/04/2019

01/06/2019  

How many inactive days is that? Do we count any inactive day before 01/01/2019 or after 01/06/2019? How?

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

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

Cheers 

 

SU18_powerbi_badge

 

 

DDalmas
Frequent Visitor

Hi, 

Please find the file here - TABLE1 (fact journey) TABLE2  (dim vehicles)

Inactive day will be considered any day that there is no journey ID for the vehicle. 

The dashboard has a date filter, so the client can check the how many days the vehicle has not worked in the last semester or in the last week. We can start counting from the first date registered on the table. It will be date one. 

I am not sure if it is possible yet. 

Example: 

I tryed to create a crossjoin table to summarize all vehicles and dates possibilities: 

TESTE = 
    CROSSJOIN(SELECTCOLUMNS('corteva dim_vehicles',"Vehicle_id",'corteva dim_vehicles'[vehicle_id]),DISTINCT('corteva fact_journeys'[Data]))

And counted the number of journeys that the vehicles has done in that day:

Journeys = CALCULATE(DISTINCTCOUNT('corteva fact_journeys'[journey_id]), FILTER('corteva fact_journeys','corteva fact_journeys'[vehicle_id]=TESTE[Vehicle_id]),FILTER('corteva fact_journeys','corteva fact_journeys'[Data]=TESTE[Data]))


The wanted output is something like that: 

Sem título.png

I hope I was able to explain it better. 

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