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
Jack_Reacher
Helper II
Helper II

How to sort months by name on rolling 12 months using DAX measures

Hello Power BI Community,

 

I am facing an issue with the x-axis on my line chart in Power BI. Specifically, when I apply the "PeriodCal" dax measure, the months for 2023 are shown on the left side of the line chart, while the months of the year 2022 are shown on the right side of the line chart in no specific order. I want to reverse the order so that the months for 2022 are shown on the left side and the months for 2023 are shown on the right side. I also attempted to sort the x-axis by "Current 12 months rolling" and "Prior 12 months rolling" (and by PeriodCal also) but to no avail. My objective is to sort the months from left (oldest month) to right (newest month) in a rolling 12 months format (March 2022, April ... December 2022, .... Jan 2023... March 2023) 

Jack_Reacher_0-1682954191948.png


Here is the code I attempted so far for the "PeriodCal" measure I am using:

PeriodCal = VAR __maxDate = MAX(Calendrier[Date]) VAR __minDate = MIN(Calendrier[Date]) RETURN SWITCH( TRUE(), 
Calendrier[Date] = __maxDate, "Current Month", Calendrier[Date] = __minDate, "Oldest Month",
FORMAT(Calendrier[Date], "mmm yyyy") )

The following are my two measures applied to the graph to calculate the prior and current 12 months rolling :

 

Prior rolling 12 months = CALCULATE([DAX_Measure_Sales], DATESINPERIOD('Calendrier'[Date],ENDOFMONTH(dateadd(Data[MyDate],-12,month)),-12,MONTH))
Current rolling 12 months = CALCULATE([DAX_Measure_Sales], DATESINPERIOD('Calendrier'[Date], ENDOFMONTH(Data[MyDate]), -12, MONTH))
 

I would greatly appreciate any help or suggestions on how to fix this issue.

 

Thank you!

1 ACCEPTED SOLUTION

@Jack_Reacher OK, so all you need is to create a numeric sorting column corresponding to calculated colunn and set the Sort By column for your calculated column to this sorting column.


@ 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

6 REPLIES 6
Greg_Deckler
Super User
Super User

@Jack_Reacher First, there should be ellipses ( ... ) in the upper right or lower right corner of your visual when you have it selected where you can change the sort order for the x-axis ( ascending/descending) or change the sorting to be based on the y-axis.

 

Second, I don't understand where you are using the PeriodCal measure because you traditionally cannot use a measure in an x-axis exactly what you are doing or trying to do.


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

It's rather a new column not a new measure, which is applied to the x-axis. 

@Jack_Reacher OK, so all you need is to create a numeric sorting column corresponding to calculated colunn and set the Sort By column for your calculated column to this sorting column.


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

you're right @Greg_Deckler , that's what I did, I created a numeric second calculated column and included it above the PeriodCal column in the x-axis as follows:

Numeric_Rolling_Outputs = YEAR(Calendrier[Date]) * 100 + MONTH(Calendrier[Date])

I got the following graph, but the label values seem to be redundant, is there a way for me to keep the names of the months, while maintaining the same order (I tried hiding the new column) :

Jack_Reacher_0-1682964269215.png

 



@Jack_Reacher You don't need to include the 2nd column in the axis. You only need to set the Sort By column for PeriodCal to be the numeric column. Click on the PeriodCal column in your Data view. In the Column Tools section of the ribbon, look for Sort By and use the drop down.


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

Thank you Greg for your assistance, you rock!

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.

Top Solution Authors