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
bsheffer
Continued Contributor
Continued Contributor

Is there a recursive way to get sequential counts? attempt 2

Is there a recursive way to get sequential counts?

for each account I'm counting the number of consecutive months that they have a status of seasonal but they can't have any other status in that month.  My code is very long.  Is there a way to write this recursively?

 

_consecutive_months_seasonal (m) =
if(calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = 1), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] = "Seasonal") = 0
|| calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = 1), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] <> "Seasonal") = 1
, 0,
if(calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = 0), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] = "Seasonal") = 0
|| calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = 0), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] <> "Seasonal") = 1
, 1,
if(calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = -1), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] = "Seasonal") = 0
|| calculate(DISTINCTCOUNT('Fact MID_Status_History'[ACTIVITY_MONTH]), filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = -1), 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] <> "Seasonal") = 1
, 2,
3)))
 
there are actually 38 of these sets but when I posted the entire measure the message was marked as spam.
16 REPLIES 16
bsheffer
Continued Contributor
Continued Contributor

So I'm trying instead to find the last month the merchant was not seasonal and then do the datediff between that month and current month to find the total number of full seasonal months.

 

_test =
var _max_month = max('Fact MID_Status_History'[ACTIVITY_MONTH])
var _mid = min('Fact MID_Status_History'[MERCHANT_NUMBER])
var _table = calculatetable (
summarize('Fact MID_Status_History', 'Fact MID_Status_History'[MERCHANT_NUMBER], 'Fact MID_Status_History'[ACTIVITY_MONTH]
, "seasonal count", calculate(COUNTROWS('Fact MID_Status_History'), filter('Fact MID_Status_History', 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] = "Seasonal"))
, "non-seasonal count", calculate(COUNTROWS('Fact MID_Status_History'), filter('Fact MID_Status_History', 'Fact MID_Status_History'[CHANGE_DETAILS_FROM] <> "Seasonal"))),
'Fact MID_Status_History'[MERCHANT_NUMBER] = _mid
)
return
CALCULATE(
maxx(_table, [ACTIVITY_MONTH])
, filter(_table, [non-seasonal count] > 0))
 
for merchant 941000132272 this should be april 2021 but it is returning august 2021
 
merchantactivity_monthseasonal countnon-seasonal count
9410001322728/1/2021 12:00:00 AM31 
9410001322727/1/2021 12:00:00 AM31 
9410001322726/1/2021 12:00:00 AM30 
9410001322725/1/2021 12:00:00 AM31 
9410001322724/1/2021 12:00:00 AM427
9410001322723/1/2021 12:00:00 AM 31

 

the counts are the number of days in the month it was in that status

 

please tell me how to fix my code

Hi @bsheffer ,

Could you please provide more example data from the table Fact MID_Status_History and table Dim Calendar ActivityMonth (exclude sensitive data)and your expected result? Also, please describe the detailed calculation logic and explain when it need to calculate consective count? Thank you.

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Merchantmonthrealtive month numberStatus
16/1/2021    -1Open
16/1/2021    -1Seasonal
17/1/2021     0Seasonal
18/1/2021     1Seasonal

I expect this merchant to have a consecutive seasonal month count of 2 because we only count months where they are only seasonal.

 

dim calendar activity month has date, activity_month, and relative_month_flag so

 

8/1/2021     8/1/2021, 1

8/2/2021     8/1/2021, 1

...

7/3/2021     7/1/2021, 0

....

6/15/2021  6/1/2021, -1

 

the measure tests for seasonal not found or a non-seasonal status found in a specific month.  if either is true then it returns a value.  Otherwise it is consecutive seasonal and drops to the next if statement where it is tested again for the prior month.  This continues until there are no more months to test or a non-full seasonal month is found. 

 

this count is calculated for each merchant.

Hi @bsheffer ,

I created a sample pbix file(see attachment), please check whether that is what you want. You can create two measures as below to get the count of consecutive seasonal months:

Measure = 
VAR _selmerc =
    SELECTEDVALUE ( 'Fact MID_Status_History'[Merchant] )
VAR _selmonth =
    SELECTEDVALUE ( 'Fact MID_Status_History'[month] )
VAR _countofstatus =
    CALCULATE (
        DISTINCTCOUNT ( 'Fact MID_Status_History'[Status] ),
        FILTER (
            ALLSELECTED ( 'Fact MID_Status_History' ),
            'Fact MID_Status_History'[Merchant] = _selmerc
                && 'Fact MID_Status_History'[month] = _selmonth
        )
    )
VAR _countofseasonal =
    CALCULATE (
        DISTINCTCOUNT ( 'Fact MID_Status_History'[Status] ),
        FILTER (
            ALLSELECTED ( 'Fact MID_Status_History' ),
            'Fact MID_Status_History'[Merchant] = _selmerc
                && 'Fact MID_Status_History'[month] = _selmonth
                && 'Fact MID_Status_History'[Status] = "Seasonal"
        )
    )
RETURN
    IF (
        _countofstatus = 1
            && _countofseasonal = 1,
        CALCULATE ( DISTINCTCOUNT ( 'Fact MID_Status_History'[month] ) ),
        BLANK ()
    )
Count of consecutive seasonal months =
SUMX (
    GROUPBY (
        'Fact MID_Status_History',
        'Fact MID_Status_History'[Merchant],
        'Fact MID_Status_History'[month]
    ),
    [Measure]
)

yingyinr_0-1630488835586.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

thank you for this very interesting piece of code.  I appreciate the use of allselected.

 

My issue is that if there is another seasonal month record (merchant = 1, activity month = 5/1/2021, relative month -2, Seasonal) then it counts that earlier month even though there was a break in full months on 6/1/2021.  It should still count only 2 even if there is earlier activity.

 

thanks again.

Hi @bsheffer ,

Please check the cases and solutions in the following two cases, and don't confirm if it is what you want.

Column counting multiple consecutive months for every IDyingyinr_0-1630569131354.png

count number of consecutive days

Besides that, you can provide some sample data plus backend logic and specific examples to illustrate the results you expect. We will provide a solution based on the information you provide later... Thank you.

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
ziyabikram96
Helper V
Helper V

Consective count =
var rowPickup = CALCULATE(MAX('T1'[Date]),
FILTER('Working File',
'Working File'[status] = EARLIER('Working File'[status])
&& 'T1'[Date] <= EARLIER('T1'[Date])
&& 'T1'[Data] <> EARLIER('T1'[Data])))
return
IF(rowPickup = BLANK(),
COUNTROWS(FILTER('T1',
'T1'[status] = EARLIER('T1'[status])
&& 'T1'[Date] <= EARLIER('T1'[Date]))) + 1 ,
'T1'[Date] - rowPickup)
Change T1 with your respective table name
and  status with [CHANGE_DETAILS_FROM] 

 

what is 'working file' ?  How is it different from 'T1'?  What is 'T1'[DATA]?

Just change with your required tables and columns this will help you 

bsheffer_0-1630352627338.png

seems to produce errors.

VahidDM
Super User
Super User

Hi @bsheffer 

 

I think you can create variables and use them in calculation code rather than the filter , something like this:

Var _Filter = 

 filter(all('Dim Calendar ActivityMonth'), [_ACTUALS RELATIVE MONTH FLAG] = 1)
 
 
beside, you can use SWITCH code.
 

Did I answer your question? Mark my post as a solution!

Appreciate your KudosVahidDM_0-1630077524930.png!!

 
 
 
 
bsheffer
Continued Contributor
Continued Contributor

Not sure how this would help me.  I'm testing for failure and when it succeeds (meaning that the month is good) I move to the next month.  Replacing pieces of code with up to 32 filter variables might make the code shorter but I don't see how this improves the logic.

parry2k
Super User
Super User

@bsheffer can you share sample data and expected output.

 

Read this post to get your answer quickly.

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

bsheffer
Continued Contributor
Continued Contributor

bsheffer_0-1630077558556.png

in this case the merchant has a status of seasonal in august and july.  he has a status of seasonal in jun but also had a status of open for part of the month.  Therefore we only want to count the full sequential months where the status is seasonal.  In this example the result would be 2.

@bsheffer no idea what you are referring to, I requested sample raw data with the expected output. Read this post to get your answer quickly.

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

bsheffer
Continued Contributor
Continued Contributor

 

Merchantmonthrealtive month numberStatus
16/1/2021    -1Open
16/1/2021    -1Seasonal
17/1/2021     0Seasonal
18/1/2021     1Seasonal

      I expect the consecutive count of full seasonal months to be 2  for this merchant.

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.