Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
vibhoryadav23
Helper II
Helper II

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



1 ACCEPTED SOLUTION

@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

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

11 REPLIES 11
Greg_Deckler
Super User
Super User

@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

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@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)

vibhoryadav23_1-1710239105976.png

Thanks for the effort btw. Much appriciated!

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

 

@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.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

These are the result from 2nd set. Mostly works with some exceotions. I believe it doesnt work when there is change in year. So from 202401 and 202312, it wont be able to take this as a previous month with simple subtraction.Can we fix these as well?

vibhoryadav23_0-1710249773307.png

 

@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

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Amazing. This works.

Btw I was able to solve it by ranking the period column and using it in your formula instead of period, but your new solution is even better.

Thanks @Greg_Deckler 

@vibhoryadav23 Oh, it's the year changes. I had actually wondered about that originally but the original data didn't include it. Hold please.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
govindarajan_d
Solution Supplier
Solution Supplier

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.

govindarajan_d_0-1710182391090.png

 

Upvote and accept as a solution if it helped!

 

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)

vibhoryadav23_2-1710239608947.png

 

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

Jihwan_Kim
Super User
Super User

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.

 

Jihwan_Kim_1-1710182303117.png

 

 

Jihwan_Kim_0-1710182291576.png

 

 

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
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


@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.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors