Forum Discussion

NorwoodFrog's avatar
NorwoodFrog
Regular Visitor
4 years ago
Solved

Counting objects/month based on Start and End Date

Hi,

 

I have a list of work orders (WO) and for each of them their start date, end date and $value. 

We would like to count the number of WOs which are active for every month. Some WOs start and finish within the month, but some spread over several months - and these are the ones causing the problem, we do not physically have a table listing the WO for each month. 

 

Below is an example of the dataset (let's call it Transactions):

 

WO_NoStartDateEndDate$_Value
WO0114/08/202127/08/2021100
WO0214/08/202124/08/2021100
WO0315/08/202112/12/2021100
WO0415/08/202120/12/2021100
WO053/09/20217/09/2021100
WO065/09/202114/09/2021100
WO0712/09/202118/09/2021100
WO0820/09/202115/11/2021100
WO093/10/202115/10/2021100
WO105/10/202112/11/2021100
WO116/10/20216/12/2021100
WO121/11/20215/11/2021100
WO132/11/202115/11/2021100
WO143/11/20215/12/2021100
WO152/12/202115/12/2021100
WO165/12/20215/01/2022100

 

As an example, WO03 covers the period Aug-21 to Dec-21 and should therefore be accounted for each month. 

 

The expected result would look something like that - note, the last column wouldn't be included in the result, it is a reference to the WOs that should be included in the COUNT. 

 

MONTHCOUNT OF ACTIVE WosWO Numbers
Aug-214WO01, WO02, WO03, WO04
Sep-216WO03, WO04, WO05, WO06, WO07, WO08
Oct-216WO03, WO04, WO08, WO09, WO10, WO11
Nov-218WO03, WO04, WO08, WO10, WO11, WO12, WO13, WO14
Dec-216WO03, WO04, WO11, WO14, WO15, WO16

 

Any help here would be greatly appreciated. I don't know how to tackle this one. 

 

Cheers

Christine 

 

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi NorwoodFrog 

     

    Here is one way, I have a disconnected Date Table called MonthTable with Date, MonthYear columns

     

    Count of Wos = 
    VAR T1 =
        GENERATE (
            Transactions,
            DATESBETWEEN (MonthTable[Date],[StartDate],[EndDate])
        )
    RETURN
       CALCULATE(COUNTROWS(VALUES(Transactions[WO_No])), ( FILTER ( T1, [Date] IN VALUES ( MonthTable[Date] ) ) ))
    
    
    WO Numbers = 
    VAR T1 =
        GENERATE (
            Transactions,
            DATESBETWEEN (MonthTable[Date],[StartDate],[EndDate])
        )
    RETURN
       CONCATENATEX( DISTINCT(SELECTCOLUMNS(FILTER ( T1, [Date] IN VALUES ( MonthTable[Date] ) ) ,"Wo",Transactions[WO_No])),[Wo],",")

     

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi NorwoodFrog 

     

    Here is one way, I have a disconnected Date Table called MonthTable with Date, MonthYear columns

     

    Count of Wos = 
    VAR T1 =
        GENERATE (
            Transactions,
            DATESBETWEEN (MonthTable[Date],[StartDate],[EndDate])
        )
    RETURN
       CALCULATE(COUNTROWS(VALUES(Transactions[WO_No])), ( FILTER ( T1, [Date] IN VALUES ( MonthTable[Date] ) ) ))
    
    
    WO Numbers = 
    VAR T1 =
        GENERATE (
            Transactions,
            DATESBETWEEN (MonthTable[Date],[StartDate],[EndDate])
        )
    RETURN
       CONCATENATEX( DISTINCT(SELECTCOLUMNS(FILTER ( T1, [Date] IN VALUES ( MonthTable[Date] ) ) ,"Wo",Transactions[WO_No])),[Wo],",")

     

    • NorwoodFrog's avatar
      NorwoodFrog
      Regular Visitor

      Hi Vera,

      This works well too and is perhaps more in line with what I was trying to achieve, which is creating that virtual table of date values. I wonder though if, with a much bigger dataset like the one I'm dealing with, there would be performance issues? 

      Cheers

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi NorwoodFrog 

         

        It could, if you have large table. Last time it took 1300 ms to scan 350k rows in Date table when I used DAX Studio to check which was still acceptable. Go test it:) If you can use Power Query and are willing to expand the orginal table to month level, you can do that as well.

    • NorwoodFrog's avatar
      NorwoodFrog
      Regular Visitor

      Thank You CNENFRNL, this works well. Rather advanced nesting of functions, no wonder I struggled 😉

       

      I am wondering about performance when looking at both solutions, yours and Vera's. My dataset is really large. I understand Vera's solution better than yours tbh, but if I get it right, your CALCULATETABLE only looks at the date range for each WO, correct?

      Cheers

  • NorwoodFrog's avatar
    NorwoodFrog
    Regular Visitor

    Both solutions work and on my dataset returned a very similar performance. I can't accept both as solution, so will go with the first one.