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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
learner03
Post Partisan
Post Partisan

Sorting month last 6 months

I have created a column for last 6 months using below-

6 months =
IF (
YEAR ( 'Calendar'[Date] ) = YEAR ( MAX('Calendar'[Date] ))
&& MONTH ( 'Calendar'[Date] ) = MONTH (MAX('Calendar'[Date])),
'Calendar'[MonthYear],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-1,
'Calendar'[MonthYear],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-2,
'Calendar'[MonthYear],IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-3,
'Calendar'[MonthYear],IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-4,
'Calendar'[MonthYear],IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-5,
'Calendar'[MonthYear],
"Neither"
)
)))))
and for sorting, created another column as-
6 months Sort =
IF (
YEAR ( 'Calendar'[Date] ) = YEAR ( MAX('Calendar'[Date] ))
&& MONTH ( 'Calendar'[Date] ) = MONTH (MAX('Calendar'[Date])),
'Calendar'[MonthID],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-1,
'Calendar'[MonthID],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-2,
'Calendar'[MonthID],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-3,
'Calendar'[MonthID],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-4,
'Calendar'[MonthID],
IF (
YEAR ('Calendar'[Date]) = YEAR ( MAX('Calendar'[Date]))
&& MONTH ('Calendar'[Date]) = MONTH (MAX('Calendar'[Date]))-5,
'Calendar'[MonthID],
0
)
)))))
After that I used Sort by Column and used 6 Month Sort which gives me correct results as -
akapoor03_0-1638758263156.png

 

But, this gives starting from July to Dec. How can  I get it that starts from Current month and goes to 6 months, i.e. December, Nov, Oct, Sept, Aug and July.

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @learner03 ,

 

I have built a data sample by creating a calculated table:

Calendar = ADDCOLUMNS( CALENDAR(DATE(2021,1,1),DATE(2021,12,31)) ,"MonthYear",FORMAT([Date],"mmm-yy") ,"MonthID",MONTH([Date]), "Type","A")

Eyelyn9_0-1638944772704.png

Please try to use RANK() to sort descending:

Rank = RANKX('Calendar',YEAR([Date])*100+MONTH([Date]),,DESC,Dense) 

Click MonthYear column --> Sort by Rank:

Eyelyn9_1-1638945004123.png

 

Output:

Eyelyn9_2-1638945019570.png

 

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

View solution in original post

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @learner03 ,

 

I have built a data sample by creating a calculated table:

Calendar = ADDCOLUMNS( CALENDAR(DATE(2021,1,1),DATE(2021,12,31)) ,"MonthYear",FORMAT([Date],"mmm-yy") ,"MonthID",MONTH([Date]), "Type","A")

Eyelyn9_0-1638944772704.png

Please try to use RANK() to sort descending:

Rank = RANKX('Calendar',YEAR([Date])*100+MONTH([Date]),,DESC,Dense) 

Click MonthYear column --> Sort by Rank:

Eyelyn9_1-1638945004123.png

 

Output:

Eyelyn9_2-1638945019570.png

 

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

Ashish_Mathur
Super User
Super User

Hi,

See if my solution here helps - Flex a Pivot Table to show data for x months ended a certain user defined month.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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