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
BigKev
Helper III
Helper III

Flag on the second max month

Hi All,

 

Does anyone know how I can create a flag to mark on the second Max month?

 

Below is my data set

DateFlag
1/01/20210
1/02/20210
1/03/20210
1/04/20210
1/05/20211
1/06/20210

 

As you can see that 1/06/21 is my max month in this table, but I want to put a flag on the month before 1/05/21.

 

When 01/07/21 data is available, then the flag "1" will be moved to 01/06/21. 

DateFlag
1/01/20210
1/02/20210
1/03/20210
1/04/20210
1/05/20210
1/06/20211
1/07/20210

 

Could someone please help me?

 

Thanks.

 

Cheers,

Kev

1 ACCEPTED SOLUTION

Hi Kev,

Use this in calculated column.

 

Flag =
IF (
'Table'[Date]
= CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( 'Table', 'Table'[Date] <> MAX ( 'Table'[Date] ) )
),
1,
0
)

Thanks

View solution in original post

9 REPLIES 9
v-rzhou-msft
Community Support
Community Support

Hi @BigKev 

You can try my way to build a rank by var function and use if to show result.

Flag = 
VAR _RANK = RANKX('Table','Table'[Date],,DESC,Dense)
Return
IF(_RANK = 2,1,0)

Result:

2.png

Update the data:

3.png

 

Best Regards,

Rico Zhou

 

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

CNENFRNL
Community Champion
Community Champion

Seems other solutions aren't robust enough when a month contains more than one date.

Flag = 
VAR __max = MAX( 'Table'[Date] )
RETURN
    IF( ISEMPTY(
            FILTER(
                CALCULATETABLE(
                    PREVIOUSMONTH( 'Table'[Date] ),
                    'Table'[Date] = __max,
                    REMOVEFILTERS()
                ),
                'Table'[Date] = EARLIER( 'Table'[Date] )
            )
        ),
        0,1
    )

Screenshot 2021-06-16 130754.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hariharan_R
Solution Sage
Solution Sage

Hi Kev,

   Use the below formula. 

Flag =
VAR MaxMonth =
CALCULATE ( MAX ( 'Table'[Date] ), ALL ( 'Table'[Date] ) )
VAR PreMax =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALL ( 'Table' ), 'Table'[Date] < MaxMonth )
)
RETURN
IF ( MIN ( 'Table'[Date] ) = PreMax, 1, 0 )

 

Hariharan_R_0-1623823497023.png

Thanks

Hari 

Please mark answer if it resolves your problem

Jihwan_Kim
Super User
Super User

Hi, @BigKev 

Please check the below picture and the sample pbix file's link down below.

It is for creating a measure, not a column.

 

Picture1.png

 

Second Max Flag Measure =
VAR newtable =
TOPN ( 2, ALL ( 'Table'[Date] ), CALCULATE ( MAX ( 'Table'[Date] ) ), DESC )
RETURN
IF( ISFILTERED('Table'[Date]),
IF ( MAX ( 'Table'[Date] ) = MINX ( newtable, 'Table'[Date] ), 1, 0 )
)
 
 
 

Hi, My name is Jihwan Kim.


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


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

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


Hey Jihwan,

 

Do you know how I can achieve this on the calculated column instead of measures?

 

Cheers,

Kev

Hi, @BigKev 

Thank you for your feedback.

Please check the link down below.

 

Second Max Flag CC =
VAR newtable =
TOPN ( 2, 'Table', 'Table'[Date], DESC )
RETURN
IF ( 'Table'[Date] = MINX ( newtable, 'Table'[Date] ), 1, 0 )

 

 

Picture1.png

 

https://www.dropbox.com/s/5ga7njcyc4w47rh/bigkevv2.pbix?dl=0 

 

 

Hi, My name is Jihwan Kim.

 

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

 

Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

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


Hi Kev,

Use this in calculated column.

 

Flag =
IF (
'Table'[Date]
= CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( 'Table', 'Table'[Date] <> MAX ( 'Table'[Date] ) )
),
1,
0
)

Thanks

Hey @Hariharan_R,

 

Thanks for the answer. Just wondering, instead of just flag the second max month, how can I flag the second max month and before?

 

This is what I want to achieve:

Date              Flag

1/01/2021       1

1/02/2021       1

1/03/2021       1

1/04/2021       1

1/05/2021       1

1/06/2021       0

Hi @BigKev 

Try this measure:

Flag = 
VAR _Max = MAX ( 'Table'[Date] ) RETURN IF ( 'Table'[Date] < _Max, 1, 0 )

Result is as below.

3.png

Best Regards,

Rico Zhou

 

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

 

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.