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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
minghaoliang
Frequent Visitor

Issue with computing Prior Sum based on monthly data

I have the following DAX codes:

function to calculate prior cost:
 
Prior Total Cost =
VAR Selected_Value_Current = calculate(firstnonblank(DateDim[Date],true()),
filter(DateDim,DateDim[Standard_MonthName]=selectedvalue(DateDim[Standard_MonthName])))

VAR Selected_Month_Previous = year(eomonth(Selected_Value_Date,-1))&"M"&format(month(eomonth(Selected_Value_Date,-1)),"00")
VAR _Prior_Total_Cost = calculate(sum('Customer Report'[EUR_LINE_TOTAL]),filter(all(DateDim),DateDim[Standard_MonthName]=Selected_Month_Previous))

return if(isblank(_Prior_Total_Cost),0 , _Prior_Total_Cost)


when I tried to run this query, it always return blank values for prior results: 

EVALUATE
VAR country = "NETHERLANDS"
VAR mth = "2025M01"
VAR
 customer = "Customer xx"

VAR table_sum =
    SUMMARIZE(
        FILTER(
            'Customer Report',
            'Customer Report'[Country_Name] = country &&
            'Customer Report'[CONTRACTUAL_CUSTOMER] = customer && 'Customer Report'[Standard_MonthName] = mth
        ),
        'Customer Report'[Standard_MonthName],
        DateDim[MonthIndex],
        'Customer Report'[New_Category],
        'Customer Report'[Service_Group],
        "Current", [Current Total Cost],
        "Prior", [Prior Total Cost]
            )

 

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

Hi @minghaoliang ,
Thanks for posting in Microsoft Fabric Community,

 

Based on your DAX code and the behavior you described, the reasons the Prior Total Cost measure returns blank values could be:

>>In your code, you declared Selected_Value_Current but later referenced Selected_Value_Date, which is undefined. This would cause the prior month calculation to fail.

>>Using ALL(DateDim) inside the FILTER will not remove any filters already applied on the 'Customer Report' table. Since your query already filters on 'Customer Report'[Standard_MonthName], the calculation cannot correctly find the previous month's data.

>>A mismatch in date handling--FIRSTNONBLANK(DateDim[Date], TRUE()) returns a date, but the logic depends on correctly converting it back to your "YYYYMmm" format for the lookup.

Also, please make sure:

   i. 'Customer Report'[Standard_MonthName] values match exactly with 'DateDim'[Standard_MonthName]      format (like 2025M01).

   ii. There is actual data available for the previous month you are querying (for example, if querying 2025M01, there should be 2024M12 data available).

Here is a corrected version of the Prior Total Cost measure you can try:

Prior Total Cost =
VAR SelectedDate = 
    CALCULATE(
        FIRSTNONBLANK(DateDim[Date], 1),
        FILTER(
            DateDim,
            DateDim[Standard_MonthName] = SELECTEDVALUE(DateDim[Standard_MonthName])
        )
    )

VAR PreviousMonthCode = 
    YEAR(EOMONTH(SelectedDate, -1)) & "M" & FORMAT(MONTH(EOMONTH(SelectedDate, -1)), "00")

VAR PriorCost = 
    CALCULATE(
        SUM('Customer Report'[EUR_LINE_TOTAL]),
        FILTER(
            ALL('Customer Report'),
            'Customer Report'[Standard_MonthName] = PreviousMonthCode
        )
    )

RETURN
    IF(ISBLANK(PriorCost), 0, PriorCost)


This version correctly fetches the prior month's total by properly constructing the previous month code and applying the filter on 'Customer Report'.

Let us know if this helps or if you have any further questions.

If this post helps, then please consider to Accept it as the solution to help the other members find it more quickly and a kudos would be appreciated.

Best regards.

Vinay.

View solution in original post

6 REPLIES 6
v-veshwara-msft
Community Support
Community Support

Hi @minghaoliang ,

We’re following up once more regarding your query. If it has been resolved, please mark the helpful reply as the "Accepted Solution" to assist others facing similar challenges.

If you still need assistance, please let us know.
Thank you.

v-veshwara-msft
Community Support
Community Support

Hi @minghaoliang ,
Just wanted to check if the solution provided has met your needs. If yes, Please consider marking it as "Accepted Solution" to assist others with similar queries. If further assistance is needed, please reach out.
Thank you.

v-veshwara-msft
Community Support
Community Support

Hi @minghaoliang ,

Just checking in to see if you query is resolved and if any responses were helpful. If so, kindly consider marking the helpful reply as 'Accepted Solution' to help others with similar queries. 

Otherwise, feel free to reach out for further assistance.

Thank you.

v-veshwara-msft
Community Support
Community Support

Hi @minghaoliang ,
Thanks for posting in Microsoft Fabric Community,

 

Based on your DAX code and the behavior you described, the reasons the Prior Total Cost measure returns blank values could be:

>>In your code, you declared Selected_Value_Current but later referenced Selected_Value_Date, which is undefined. This would cause the prior month calculation to fail.

>>Using ALL(DateDim) inside the FILTER will not remove any filters already applied on the 'Customer Report' table. Since your query already filters on 'Customer Report'[Standard_MonthName], the calculation cannot correctly find the previous month's data.

>>A mismatch in date handling--FIRSTNONBLANK(DateDim[Date], TRUE()) returns a date, but the logic depends on correctly converting it back to your "YYYYMmm" format for the lookup.

Also, please make sure:

   i. 'Customer Report'[Standard_MonthName] values match exactly with 'DateDim'[Standard_MonthName]      format (like 2025M01).

   ii. There is actual data available for the previous month you are querying (for example, if querying 2025M01, there should be 2024M12 data available).

Here is a corrected version of the Prior Total Cost measure you can try:

Prior Total Cost =
VAR SelectedDate = 
    CALCULATE(
        FIRSTNONBLANK(DateDim[Date], 1),
        FILTER(
            DateDim,
            DateDim[Standard_MonthName] = SELECTEDVALUE(DateDim[Standard_MonthName])
        )
    )

VAR PreviousMonthCode = 
    YEAR(EOMONTH(SelectedDate, -1)) & "M" & FORMAT(MONTH(EOMONTH(SelectedDate, -1)), "00")

VAR PriorCost = 
    CALCULATE(
        SUM('Customer Report'[EUR_LINE_TOTAL]),
        FILTER(
            ALL('Customer Report'),
            'Customer Report'[Standard_MonthName] = PreviousMonthCode
        )
    )

RETURN
    IF(ISBLANK(PriorCost), 0, PriorCost)


This version correctly fetches the prior month's total by properly constructing the previous month code and applying the filter on 'Customer Report'.

Let us know if this helps or if you have any further questions.

If this post helps, then please consider to Accept it as the solution to help the other members find it more quickly and a kudos would be appreciated.

Best regards.

Vinay.

AnalyticPulse
Super User
Super User

hi @minghaoliang  
is the format of your 'DateDim'[Standard_MonthName] column same as YYYYMmm format? 

Yes it is

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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