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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
SachinC
Helper V
Helper V

Sort by Date X - Axis

Hi,

I am connecting to a SQL server instance. I have a date/time field called DateInspected.

I want to use this field but want to convert this date field to mm-yyyy and use this in a bar chart and sort by this date, so 01-2019, 02-2019 ....etc....

I am blowing my brains out.  I tried a few suggestions online and in this forum but it doesn't work. I created a new field called DateInspected-M-Y = month([DateInspected])&"-"&year([DateInspected]), but this didn't work when sorting.

Help please!

Thanks!

1 ACCEPTED SOLUTION

Hi @SachinC ,

 

Syntax I gave you is for a DAX column you should replace the dot comma by comma (it's regional setting thing).

FORMAT([DateInspected],"mm-yyyy")

 

 

On the query editor the formula should be:

Text.Combine({Date.ToText([DateInspected], "MM"), "-", Date.ToText([DateInspected], "yyyy")})

 

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

8 REPLIES 8
MFelix
Super User
Super User

Hi @SachinC ,

 

When you are using Month(Table[Column]) this will give you a result of  1 , 2, 3 so when you sort out with the rest of your formula that will give you a text so 10, 11 and 12 will come before 2.

 

You need to do the following formula:

DateInspected-M-Y = FORMAT([DateInspected];"mm-yyyy")

 

this will return the format 01-2020 and so on and you can sort as you need.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



MFelix,

Good shout - that didn't work.

 

In Edit Query, when I try to create a custom field called DateInspectedNEW, using: FORMAT([DateInspected];"mm-yyyy"),  it says: Token Comma expected.

When I create a new field in the Fields list; I get: The syntax for ';' is incorrect. DAX(FORMAT([DateInspected];"mm-yyyy"))).

 

The original field: DateInspected is of Data type: Date, Format: 13/03/2001 (dd/MM/yyyy)

 

Please help!

 

Try replacing the semi-colons in @MFelix 's solution with commas. It varies based on language version of Power BI Desktop.



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Using commas didn't work; this is what doesn't work:  

DateInspection-M-Y = FORMAT([DateInspected],"mm-yyyy")

You are adding a calculated column correcr?

Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Do you mean a 'Measure'? Thanks.

I'm refering to columns not measures.

Measure have different syntax due to the way they are calculated.

You cannot use measures in axis of visualizations.

Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @SachinC ,

 

Syntax I gave you is for a DAX column you should replace the dot comma by comma (it's regional setting thing).

FORMAT([DateInspected],"mm-yyyy")

 

 

On the query editor the formula should be:

Text.Combine({Date.ToText([DateInspected], "MM"), "-", Date.ToText([DateInspected], "yyyy")})

 

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors