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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
sunyangjun1115
Frequent Visitor

Identify the companies that have a profit for 4 or more successive months

Hi Experts,

 

in DAX Studio, i  write below DAX to create a temp table :

 

DEFINE
VAR TempTable =

       DATATABLE(
           "CompanyCode", STRING,
           "YearMonth", STRING,
           "ProfitIndicator", INTEGER,
           {
               {"A", "202301", 1},
               {"A", "202302", 1},
               {"A", "202303", 1},
               {"A", "202304", 0},
               {"A", "202305", 1},
               {"A", "202306", 1},
               {"B", "202301", 1},
               {"B", "202302", 1},
               {"B", "202303", 1},
               {"B", "202304", 1},
               {"B", "202305", 1},
               {"B", "202306", 0}
           }
  
   )
 

the data in this table is below: 

CompanyCodeYearMonthProfitIndicator
A2023011
A2023021
A2023031
A2023040
A2023051
A2023061
B2023011
B2023021
B2023031
B2023041
B2023051
B2023060

 

 

my requiremnet is to idenitfy the companies which have a profit(ProfitIndicator =1) for 4 or more successive months.  as only company B has the profit for 5 successive months(202301/202302/202303/202304/202305),   the result should show company B. how to realize this by DAX ?  thanks in advance for your time and support.

 

1 ACCEPTED SOLUTION

@Greg_Deckler  , thanks so much for your prompt support.  i  tried the Cthulhu that you shared.  at the begining ,it's a bit difficult for me to understand how the DAX works.  afte tried in DAX studio for multiply times. now i can understand your DAX better.  

comparing the Cthulhu with my requirment,  it has some difference.  after  sometime's testing, i have already figured it out . below are the DAX code that i wrote in DAX Studio. 

 

DEFINE
    VAR TempTable =
        DATATABLE (
            "CompanyCode", STRING,
            "YearMonth", STRING,
            "ProfitIndicator", INTEGER,
            {
                { "A", "202301", 1 },
                { "A", "202302", 1 },
                { "A", "202303", 1 },
                { "A", "202304", 0 },
                { "A", "202305", 1 },
                { "A", "202306", 1 },
                { "B", "202301", 1 },
                { "B", "202302", 1 },
                { "B", "202303", 1 },
                { "B", "202304", 1 },
                { "B", "202305", 1 },
                { "B", "202306", 0 }
            }
        )
    VAR tempTable2 =
        ADDCOLUMNS (
            tempTable,
            "YearMonth_Indicator_0",
                MAXX (
                    FILTER (
                        tempTable,
                        [CompanyCode] = EARLIER ( [CompanyCode] )
                            && [YearMonth] < EARLIER ( [YearMonth] )
                            && [ProfitIndicator] = 0
                    ),
                    [YearMonth]
                )
        )
    VAR tempTable3 =
        ADDCOLUMNS (
            tempTable2,
            "IndexNo",
                IF (
                    [ProfitIndicator] = 0,
                    0,
                    IF (
                        [YearMonth_Indicator_0] <> BLANK (),
                        COUNTROWS (
                            FILTER (
                                tempTable2,
                                [CompanyCode] = EARLIER ( [CompanyCode] )
                                    && [YearMonth] >= EARLIER ( [YearMonth_Indicator_0] )
                                    && [YearMonth] <= EARLIER ( [YearMonth] )
                            )
                        ) - 1,
                        COUNTROWS (
                            FILTER (
                                tempTable2,
                                [CompanyCode] = EARLIER ( [CompanyCode] )
                                    && [YearMonth] <= EARLIER ( [YearMonth] )
                            )
                        )
                    )
                )
        )
    VAR result =
        GROUPBY ( FILTER ( tempTable3, [IndexNo] >= 4 ), [CompanyCode] )

EVALUATE
tempTable3

EVALUATE
result

 

Identify companies.png

Company.JPG

 

 

View solution in original post

5 REPLIES 5
v-kaiyue-msft
Community Support
Community Support

Hi @sunyangjun1115 ,

 

Thanks for the reply from Greg_Deckler , please allow me to provide another insight: 

 

1. Create the index column in the power query editor.

vkaiyuemsft_0-1723018557118.png


2. Create calculated columns.

Column = 
RANKX (
    FILTER (
        'Table',
        'Table'[ProfitIndicator] = 0
            && 'Table'[CompanyCode] = EARLIER ( 'Table'[CompanyCode] )
    ),
    'Table'[Index],
    ,
    ASC,
    DENSE
)
Column 2 = 
IF (
    'Table'[ProfitIndicator] <> 0,
    RANKX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[CompanyCode] = EARLIER ( 'Table'[CompanyCode] )
                && 'Table'[Column] = EARLIER ( 'Table'[Column] )
        ),
        'Table'[YearMonth],
        ,
        ASC
    )
)

vkaiyuemsft_1-1723018610762.png


3. Create a calculation table.

FilteredCompanies = 
FILTER(
    GROUPBY(
        'Table',
        'Table'[CompanyCode],
        "MaxSuccessiveCount", MAXX(CURRENTGROUP(), [Column 2])
    ),
    [MaxSuccessiveCount] >= 4
)

vkaiyuemsft_2-1723018649632.png

 

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.

@v-kaiyue-msft  , Many thanks for your great help.   it works if  i have the table  with 3 columns(Company Code/YearMonth/ProfitIndicator) that i showed as the example for my requirement. but actually, the third column "ProfitIndicator" is a measure , this measure is used to calculate if a company is profitable or not . 

seems that your method(using Power Query and create calculate column/Table) will not applicable for my requirement. 

 

thanks again for your time and great support.  

Greg_Deckler
Super User
Super User

@sunyangjun1115 Looks like an application for Cthulhu. Cthulhu - Microsoft Fabric Community

 

If this solution helped, please vote for my sticker! Banana Pickle Math - Microsoft Fabric Community



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  , thanks so much for your prompt support.  i  tried the Cthulhu that you shared.  at the begining ,it's a bit difficult for me to understand how the DAX works.  afte tried in DAX studio for multiply times. now i can understand your DAX better.  

comparing the Cthulhu with my requirment,  it has some difference.  after  sometime's testing, i have already figured it out . below are the DAX code that i wrote in DAX Studio. 

 

DEFINE
    VAR TempTable =
        DATATABLE (
            "CompanyCode", STRING,
            "YearMonth", STRING,
            "ProfitIndicator", INTEGER,
            {
                { "A", "202301", 1 },
                { "A", "202302", 1 },
                { "A", "202303", 1 },
                { "A", "202304", 0 },
                { "A", "202305", 1 },
                { "A", "202306", 1 },
                { "B", "202301", 1 },
                { "B", "202302", 1 },
                { "B", "202303", 1 },
                { "B", "202304", 1 },
                { "B", "202305", 1 },
                { "B", "202306", 0 }
            }
        )
    VAR tempTable2 =
        ADDCOLUMNS (
            tempTable,
            "YearMonth_Indicator_0",
                MAXX (
                    FILTER (
                        tempTable,
                        [CompanyCode] = EARLIER ( [CompanyCode] )
                            && [YearMonth] < EARLIER ( [YearMonth] )
                            && [ProfitIndicator] = 0
                    ),
                    [YearMonth]
                )
        )
    VAR tempTable3 =
        ADDCOLUMNS (
            tempTable2,
            "IndexNo",
                IF (
                    [ProfitIndicator] = 0,
                    0,
                    IF (
                        [YearMonth_Indicator_0] <> BLANK (),
                        COUNTROWS (
                            FILTER (
                                tempTable2,
                                [CompanyCode] = EARLIER ( [CompanyCode] )
                                    && [YearMonth] >= EARLIER ( [YearMonth_Indicator_0] )
                                    && [YearMonth] <= EARLIER ( [YearMonth] )
                            )
                        ) - 1,
                        COUNTROWS (
                            FILTER (
                                tempTable2,
                                [CompanyCode] = EARLIER ( [CompanyCode] )
                                    && [YearMonth] <= EARLIER ( [YearMonth] )
                            )
                        )
                    )
                )
        )
    VAR result =
        GROUPBY ( FILTER ( tempTable3, [IndexNo] >= 4 ), [CompanyCode] )

EVALUATE
tempTable3

EVALUATE
result

 

Identify companies.png

Company.JPG

 

 

@sunyangjun1115 Glad you got it. In retrospect, I probably should have pointed you to Streaks! which is based on Cthulhu. Streaks! - Microsoft Fabric Community



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.