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
theDarkPrince
Advocate II
Advocate II

Unable to get correct values when using different date format column from same table

Hi everyone. This one's got me stumped. Below is a screenshot that depicts my connections in the Data Model:

theDarkPrince_2-1631625569215.png

The YYYY_MM column has format "2021-09" and MONTH_IN_CAL column has format "Sep-21".

I have created a measure as following:

 

Previous Month Backlog = 
var curr_mon_yyyy_mm = "2021-09"
var prev_mon_yyyy_mm = "2021-08"
var Result = IF( MIN('Calendar'[YYYY_MM]) = curr_mon_yyyy_mm, CALCULATE(SUM('Facts Data'[BACKLOG_AMOUNT]), 'Calendar'[YYYY_MM] = prev_mon_yyyy_mm ), BLANK() )
Return Result

 

What I am trying to achieve in this measure is computing the amount for previous month and display it in current month as below:

theDarkPrince_3-1631625734431.png

With the YYYY_MM column, it works just fine and I get correct amount. However, I want to achieve the same with MONTH_IN_CAL column but am getting blank:

theDarkPrince_4-1631625875780.png

Both YYYY_MM & MONTH_IN_CAL come from the same Calendar table. To try and get to the bottom of things, I obtained the DAX Query using Performance Analyzer and got this:

 

DEFINE
  VAR __DS0Core = 
    SUMMARIZECOLUMNS(
      ROLLUPADDISSUBTOTAL(
        ROLLUPGROUP('Calendar'[MONTH_IN_CAL], 'Calendar'[YYYY_MM]), "IsGrandTotalRowTotal"
      ),
      "Previous_Month_Backlog", 'Facts Data'[Previous Month Backlog]
    )
  VAR __DS0PrimaryWindowed = 
    TOPN(
      502,
      __DS0Core,
      [IsGrandTotalRowTotal],
      0,
      'Calendar'[YYYY_MM],
      1,
      'Calendar'[MONTH_IN_CAL],
      1
    )
EVALUATE
  __DS0PrimaryWindowed
ORDER BY
  [IsGrandTotalRowTotal] DESC, 'Calendar'[YYYY_MM], 'Calendar'[MONTH_IN_CAL]

 

YYYY_MM by itself is ordered but to show MONTH_IN_CAL in proper chronological order, I have sorted it by the YYYY_MM column.

Therefore, it seems, even though the YYYY_MM column is not being used in the table visual, it'll automatically become part of the DAX Query for ordering purposes. I did a little bit of digging around how I can solve this and found this https://blog.crossjoin.co.uk/2015/12/15/power-bi-desktop-sort-by-column-and-dax-calculations-that-us... article. However, the author suggests using ALL('Calendar'[MONTH_IN_CAL], 'Calendar'[YYYY_MM] ), but using that gives me incorrect number which is a total of all values in the table. Any ideas about how this can be resolved. Any help is appreciated. Thanks.

@amitchandak @Greg_Deckler @parry2k @ryan_mayu @v-rzhou-msft 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@theDarkPrince , Create a rank in Calendar table on YYYYMM

 

new column

Month Rank = RANKX(all('Date'),'Date'[YYYY_MM],,ASC,Dense)

 

example measure

This Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

 

Last Year Period = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month]=max(Period[Month]) && 'Date'[Year]=max('Date'[Year])-1))

 

 

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

@theDarkPrince Thinking:

Previous Month Backlog Measure = 
var __Current = MAX('Calendar'[Date])
VAR __PreviousEOMonth = EOMONTH(__Current,-1)
VAR __Previous = DATE(YEAR(__PreviousEOMOnth),MONTH(__PreviousEOMOnth),1)
var Result = CALCULATE(SUM('Facts Data'[BACKLOG_AMOUNT]), 'Calendar'[Date] <= __Previous, 'Calendar'[Date]<=__PreviousEOMonth)
Return Result

@ 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...
amitchandak
Super User
Super User

@theDarkPrince , Create a rank in Calendar table on YYYYMM

 

new column

Month Rank = RANKX(all('Date'),'Date'[YYYY_MM],,ASC,Dense)

 

example measure

This Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

 

Last Year Period = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Month]=max(Period[Month]) && 'Date'[Year]=max('Date'[Year])-1))

 

 

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.