Forum Discussion

vibhoryadav23's avatar
vibhoryadav23
Helper II
2 years ago
Solved

Formulae for repeat usage across rows

Hi,

 

I need to find users who have been present in the data in last 3 month and twice in last three month (incl. current month) like shown in the examples below respectively. Column 1 and 2 are inputs and 2 and 3 are the required outcomes. In the example, the 'User1' is present in all the 6 month and hence we know from 3rd month onwards, it has been present in all the month and hence yes for all.

Users   Months   Present in last 3 months
(incl. current month)   
Present in 2 of last 3 months
(incl. current month)
User1   100
User1   201
User1   311
User1   411
User1   511
User1   611
User2   300
User2   401
User2   511
User2   611
User3   500
User4   400
User4   501
User5   300
User5   501

 

++ adding an example of a user. Clubbed year and month to form 'Period' to take into account historic data.

UsersPeriod Present all in last 3 monthsPresent 2 in last 3 months
X202403 01
X202401 01
X202311 01
X202310 00
X202303 01
X202302 00
X202211 11
X202210 11
X202209 11
X202208 11
X202207 11
X202206 11
X202205 11
X202204 11
X202203 11
X202202 11
X202201 11
X202112 01
X202111 00



  • Greg_Deckler's avatar
    Greg_Deckler
    2 years ago

    vibhoryadav23 OK, this should do it. PBIX is attached below signature.

    Column = 
        VAR __User = [Users]
        VAR __Period = [Period]
        VAR __PeriodDate = DATE(LEFT(__Period,4), RIGHT(__Period, 2), 1)
        VAR __EndDate = EOMONTH(__PeriodDate, -2)
        VAR __EndDateNum = YEAR(__EndDate) * 100 + MONTH(__EndDate)
        VAR __Count = COUNTROWS( FILTER( 'Table2', [Users] = __User && [Period] <= __Period && [Period] >= __EndDateNum ) )
        VAR __Result = IF( __Count >= 3, 1, 0 )
    RETURN
        __Result
    
    
    Column 2 = 
        VAR __User = [Users]
        VAR __Period = [Period]
        VAR __PeriodDate = DATE(LEFT(__Period,4), RIGHT(__Period, 2), 1)
        VAR __EndDate = EOMONTH(__PeriodDate, -2)
        VAR __EndDateNum = YEAR(__EndDate) * 100 + MONTH(__EndDate)
        VAR __Count = COUNTROWS( FILTER( 'Table2', [Users] = __User && [Period] <= __Period && [Period] >= __EndDateNum ) )
        VAR __Result = IF( __Count >= 2, 1, 0 )
    RETURN
        __Result

     

11 Replies

  • Hi vibhoryadav23 ,

    Can you try the below measures:

    Present in last 3 months (incl. current month):

    Op1_Measure =
    IF (
        COUNTX (
            WINDOW (
                -2,
                REL,
                0,
                REL,
                SUMMARIZE ( ALL ( 'Table' ), 'Table'[Users], 'Table'[Months] ),
                ORDERBY ( 'Table'[Months] ),
                DEFAULT,
                PARTITIONBY ( 'Table'[Users] )
            ),
            CALCULATE ( COUNT ( 'Table'[Months] ) )
        ) = 3,
        1,
        0
    )

    Present in 2 of last 3 months (incl. current month):

    Op2_Measure =
    IF (
        COUNTX (
            WINDOW (
                -2,
                REL,
                0,
                REL,
                SUMMARIZE ( ALL ( 'Table' ), 'Table'[Users], 'Table'[Months] ),
                ORDERBY ( 'Table'[Months] ),
                DEFAULT,
                PARTITIONBY ( 'Table'[Users] )
            ),
            CALCULATE ( COUNT ( 'Table'[Months] ) )
        ) >= 2,
        1,
        0
    )

     

    Tested:

    Op1_Measure and Op2_Measure are the result of above formulas and next to each of them is the column from the sample data you had provided.

     

    Upvote and accept as a solution if it helped!

     

    • vibhoryadav23's avatar
      vibhoryadav23
      Helper II

      Thanks govindarajan_d
      It doesnt work if there's a month missing in the order. Example: Observe that in first 2 rows, 202402 is missing (2024 February)

      (I have clubbed year and month to from 'Period' accomodate historic data)

       

      I have added an example of a user in the original post for more clarity

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    vibhoryadav23  Try these. PBIX file is attached:

     

    Column = 
        VAR __User = [Users]
        VAR __Month = [Months]
        VAR __Count = COUNTROWS( FILTER( 'Table', [Users] = __User && [Months] <= __Month ) )
        VAR __Result = IF( __Count >= 3, 1, 0 )
    RETURN
        __Result
    
    
    Column 2 = 
        VAR __User = [Users]
        VAR __Month = [Months]
        VAR __Count = COUNTROWS( FILTER( 'Table', [Users] = __User && [Months] <= __Month ) )
        VAR __Result = IF( __Count >= 2, 1, 0 )
    RETURN
        __Result

    Alternate forms that are perhaps a bit more selective are these:

    Column = 
        VAR __User = [Users]
        VAR __Month = [Months]
        VAR __Count = COUNTROWS( FILTER( 'Table', [Users] = __User && [Months] <= __Month && [Months] >= __Month - 2 ) )
        VAR __Result = IF( __Count >= 3, 1, 0 )
    RETURN
        __Result
    
    
    Column 2 = 
        VAR __User = [Users]
        VAR __Month = [Months]
        VAR __Count = COUNTROWS( FILTER( 'Table', [Users] = __User && [Months] <= __Month && [Months] >= __Month - 2 ) )
        VAR __Result = IF( __Count >= 2, 1, 0 )
    RETURN
        __Result

     

    • vibhoryadav23's avatar
      vibhoryadav23
      Helper II

      Greg_Deckler I took column 1 form your first set and column 2 from second. Your solution mostly works well except when the months are missing in between, it doesnt take them into account. That's the main problem I am trying to solve. Example: I have combined year and month to form 'Period' as the data can back years in time. If you observe first three rows, there are some months missing for the user but its still being counted in Column 1. For column 2, in second row it should be 1 as its present in 202401 and 202311 (two of last 3 months)



      Thanks for the effort btw. Much appriciated!

      I have added an example of a user in the original post for more clarity.

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion

        vibhoryadav23 That was the purpose of the second set of example column formulas. Those *should* work in the case you are describing. You would want to use column1 and column2 from the second set as these filter the number of rows that are looked back upon so if there are missing months then you *should* get the correct results.

  • Hi,

    I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

    Please check the below picture and the attached pbix file.

    It is for creating a measure.

     

     

     

     

     

    WINDOW function (DAX) - DAX | Microsoft Learn

     

    Present all in last 3 months: = 
    IF (
        COUNTROWS (
            WINDOW (
                -2,
                REL,
                0,
                REL,
                SUMMARIZE ( ALL ( Data ), Months[Months], Users[Users] ),
                ORDERBY ( Months[Months], ASC ),
                ,
                PARTITIONBY ( Users[Users] )
            )
        ) >= 3,
        1,
        0
    )

     

    Present 2 in last 3 months: = 
    IF (
        COUNTROWS (
            WINDOW (
                -2,
                REL,
                0,
                REL,
                SUMMARIZE ( ALL ( Data ), Months[Months], Users[Users] ),
                ORDERBY ( Months[Months], ASC ),
                ,
                PARTITIONBY ( Users[Users] )
            )
        ) >= 2,
        1,
        0
    )
    • vibhoryadav23's avatar
      vibhoryadav23
      Helper II

      Jihwan_Kim Thanks for the effort. This doesnt work unfortunately.

      I have added an example of a user in the original post for more clarity.