Forum Discussion

fenixen's avatar
fenixen
Icon for Advocate II rankAdvocate II
9 years ago

Count items in a given datetime interval across several days

 

I want a chart that shows number of agreements per day (no problem), but i want to be able to drill down to hours and still get the correct count. 

 

I have a Calendar table connected to the fact. 

I have a Time table with 1440 minutes/24hours, not connected to the fact. 

 

Fact table

I have generated one fact row per rented day per agreement ID to be able to connect the fact table to a calendar. 

 

| Agreement ID | Date From | Time From | Date To | Time To |    Date

        1                     01.03            09:00         05.03       15:00       01.03 (1row per date in interval)

        2                     02.03            12:00         02.03       15:00         etc..

        3                     05.03             07:00        10.03       16:00

 

Base measure:      Count: =distinctcount([Agreement ID]

Then i have to somehow only count between the correct time intervall.. 

 

Using a single calculate doesn't seem to be possible.. I think i need one for the start date and one for the end date. 

 

I tried controlling which Calculate to run by using an IF statement. One measure for the start date, one for the interval and one for the end date. This works fine as long as Agreement ID is present in the visualizaton, if its not visible the IF statement will iterate on the whole data set instead of on each unique agreement ID.

 

Can i force the IF to check each row without having Agreement ID in the visualization? 

 

 

=if( max([Calender[Date])=min(Fact[DateFrom))

     then  [Measure X]

   else if( max(Calender[Date]) = max(Fact[Date To]) ; 

                 then [Measure Y]

       else [Count]

 

 

 

 

 

7 Replies

  • v-huizhn-msft's avatar
    v-huizhn-msft
    Icon for Microsoft Employee rankMicrosoft Employee

    Hi fenixen,

    I am still confusing your requirement, could you please share more details for further analysis?

    Based on my understanding, you should use the following formula to get the every day's active number of agreements.

    Number of Agreement=CALCULAT(COUNTA(Fact[AgreementID]),FILTER(Fact,AND(Calendar[date]<=MIN(Fact[Date to]),Calendar[date]>=MAX(Fact[Date From]))))


    In addition, what's your mean of [Measure X] and [Measure Y]?

    Best Regards,
    Angelia

    • fenixen's avatar
      fenixen
      Icon for Advocate II rankAdvocate II

      Hi v-huizhn-msft! 

       

      Sorry for being unclear in my first post and thanks for the interest in my problem. 

      Getting every day's active numnber of agreements is OK, but I want to account for the time of day. If the agreement ends at 15:00 it shouldn't be counted at 15:01. My current problem is that the agreements are counted for all 24 hours of the day. 

       

       

      ExampleAgreement:   Starts on the 10th of april at 10:00 and ends at the 12th of april 15:00

       

      The agreement should count from 10:00 the first day, then it should count for all minutes/hours of the second day (11.04) and stop counting at 15:00 the third day.  This would be easy to solve if the agreements started and ended on the same or adjacent days, but an agreement could be active for 2 weeks for instance.. 

       

      If possible please have a look at the demo .pbix I've created to help understanding and hopefully solve the issue :) 

      Dropbox download url

       

       

      • v-huizhn-msft's avatar
        v-huizhn-msft
        Icon for Microsoft Employee rankMicrosoft Employee

        Hi fenixen,

        I create three measures using the following.

        Min = MIN('dimTime/Clock'[Hour Number])
        Max = MAX('dimTime/Clock'[Next Hour])
        #Measure = CALCULATE([#Agreements],filter(factReservations1rowperday,and(factReservations1rowperday[Time from]<='dimTime/Clock'[Min],factReservations1rowperday[Time to]>='dimTime/Clock'[Max])))


        Then the #Measure as value level in Matrix preview, please see the screenshot.



        Best Regards,
        Angelia