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
abbytank
Frequent Visitor

calculate and sameperiodlastyear not working IF function

Hi, 
I am having issue with the following calculation, I want to calculate the total sales, which is working correct, but when I select a year whose previous year had no sales like the initial year, it show blank, I needed to use if to avoid showing blank, but it returned error. Here is the code:
Previous_year_sales = if(ISBLANK(SAMEPERIODLASTYEAR('Date'[Date])),

"No Sales",

CALCULATE([TotalSales],

        SAMEPERIODLASTYEAR('Date'[Date]))
)
Please someone help.
 
Thank you in advance.
 
1 ACCEPTED SOLUTION

Hi @abbytank ,

Actually, the function ISBLANK is to check whether a value is blank. But it will return a table when you apply the function SAMEPERIODLASTYEAR. You can find the details in the following links.

ISBLANK

SAMEPERIODLASTYEAR

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @abbytank ,

You can update the formula of measure [Previous_year_sales] as below, please notice that the data type of returned result will be Text type instead of Number type. If you still want to keep the data type as Number type, you can change "No Sales" to 0.

Previous_year_sales =
VAR _pysales =
    CALCULATE ( [TotalSales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
    IF ( ISBLANK ( _pysales ), "No Sales", _pysales )

yingyinr_0-1670901130132.png

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you very much @v-yiruan-msft  for your supportive response.

 

I see your solutions is 100% working and providing the very desired result, but concern is:

This is working:

VAR _pysales =
    CALCULATE ( [TotalSales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
return _pysales

And also, the following is working:

ISBLANK(SAMEPERIODLASTYEAR('Date'[Date]))

And even the first the part of the code is working, because when the previous years sales is empty (no sales on previous year) the result is working and showing "No Sales" result:

Previous_year_sales = if(ISBLANK(SAMEPERIODLASTYEAR('Date'[Date])),

"No Sales",

 

But the problem is, when the calculate function or even sumx function put on the IF functions as (if function's) return values (return values on true or false) according to the following:

Previous_year_sales = if(ISBLANK(SAMEPERIODLASTYEAR('Date'[Date])),

"No Sales",

CALCULATE([TotalSales],

        SAMEPERIODLASTYEAR('Date'[Date]))
)

So, why calculate is not working in IF function's argument, that is my concern?

 

Thank you very much.

Hi @abbytank ,

Actually, the function ISBLANK is to check whether a value is blank. But it will return a table when you apply the function SAMEPERIODLASTYEAR. You can find the details in the following links.

ISBLANK

SAMEPERIODLASTYEAR

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.