Donate

Line Chart X-Axis Month Values Not Sorting Correctly In Power BI LineChart

Gents,

Today, I had an issue with month values in my line chart visual not sorting correctly. The month values were plainly text in type and it seems the line chart does not recognize it's order.
Line Chart X-Axis Month Values Not Sorting Correctly In Power BI LineChart
In order for these to work, I need to create a month column to explicitly include the year and retrieve the value using DateValue() function. If the measure is added in the visual, set the proper date format in Power BI desktop for the line chart to recognize the proper sequence.
MonthYear = 
var monthName = TRIM(GrossProfitTrend[MonthName])
Return
  SWITCH(monthName, "Jan", DATEVALUE(MONTH(DATE(YEAR(NOW()),1, 1)) & " " & YEAR(NOW()))
      , "Feb", DATEVALUE(MONTH(DATE(YEAR(NOW()),2, 1)) & " "  & YEAR(NOW()))
      , "Mar", DATEVALUE(MONTH(DATE(YEAR(NOW()),3, 1)) & " "  & YEAR(NOW()))
      , "Apr", DATEVALUE(MONTH(DATE(YEAR(NOW()),4, 1)) & " "  & YEAR(NOW()))
      , "May", DATEVALUE(MONTH(DATE(YEAR(NOW()),5, 1)) & " "  & YEAR(NOW()))
      , "Jun", DATEVALUE(MONTH(DATE(YEAR(NOW()),6, 1)) & " "  & YEAR(NOW()))
      , "Jul", DATEVALUE(MONTH(DATE(YEAR(NOW()),7, 1)) & " "  & YEAR(NOW()))
      , "Aug", DATEVALUE(MONTH(DATE(YEAR(NOW()),8, 1)) & " "  & YEAR(NOW()))
      , "Sep", DATEVALUE(MONTH(DATE(YEAR(NOW()),9, 1)) & " "  & YEAR(NOW()))
      , "Oct", DATEVALUE(MONTH(DATE(YEAR(NOW()),10, 1)) & " "  & YEAR(NOW()))
      , "Nov", DATEVALUE(MONTH(DATE(YEAR(NOW()),11, 1)) & " "  & YEAR(NOW()))
      , "[Dec]", DATEVALUE(MONTH(DATE(YEAR(NOW()),12, 1)) & " "  & YEAR(TODAY())))
Output
Line Chart X-Axis Month Values Not Sorting Correctly In Power BI LineChart

The solution is a bit tricky, but it works!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

Remove Cookies From CookieContainer Class

How To Test Or Check Dead Pixels And Backlight Bleed On Your Monitor Using Dead Pixel Checker In Windows 11