cancel
Showing results for
Did you mean:
Frequent Visitor

## Calculate Previous year sales if current year is 0

Hi,

My data is in a simple model with a sales table like below, and for each client I want to calculate 2021 sales only if they have 0 sales for 2022 and vice- versa.

I also need to be able to aggregate this up to Website level

 Website Client Product Date Amount WebA ClientA ProductA 01/01/2021 85 WebB ClientA ProductB 01/01/2021 17 WebB ClientB ProductC 01/01/2021 82 WebA ClientC ProductD 01/01/2021 65 WebA ClientA ProductE 01/01/2022 69
1 ACCEPTED SOLUTION
Community Support

Hi @Atifz ,

This is my test table:

``````Sum_of_client =
var sum2021 = CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Client] = SELECTEDVALUE('Table'[Client]) && YEAR('Table'[Date]) = 2021))
var sum_amount = CALCULATE(MIN('Table'[Amount]),FILTER('Table','Table'[Client] = SELECTEDVALUE('Table'[Client]) && YEAR('Table'[Date]) = 2022))
return
IF(sum_amount = 0,sum2021)

Sum_of_website =
var sum2021 = CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Website] = SELECTEDVALUE('Table'[Website]) && YEAR('Table'[Date]) = 2021))
var sum_amount = CALCULATE(MIN('Table'[Amount]),FILTER('Table','Table'[Website] = SELECTEDVALUE('Table'[Website]) && YEAR('Table'[Date]) = 2022))
return
IF(sum_amount = 0,sum2021)``````

Then you can calculate Previous year sales if current year is 0.

Best regards,

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

6 REPLIES 6
Community Support

Hi @Atifz ,

This is my test table:

``````Sum_of_client =
var sum2021 = CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Client] = SELECTEDVALUE('Table'[Client]) && YEAR('Table'[Date]) = 2021))
var sum_amount = CALCULATE(MIN('Table'[Amount]),FILTER('Table','Table'[Client] = SELECTEDVALUE('Table'[Client]) && YEAR('Table'[Date]) = 2022))
return
IF(sum_amount = 0,sum2021)

Sum_of_website =
var sum2021 = CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Website] = SELECTEDVALUE('Table'[Website]) && YEAR('Table'[Date]) = 2021))
var sum_amount = CALCULATE(MIN('Table'[Amount]),FILTER('Table','Table'[Website] = SELECTEDVALUE('Table'[Website]) && YEAR('Table'[Date]) = 2022))
return
IF(sum_amount = 0,sum2021)``````

Then you can calculate Previous year sales if current year is 0.

Best regards,

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

Super User

@Atifz Well, maybe something like:

``````Measure =
VAR __Client = MAX('Table'[Client])
VAR __Table2021 = FILTER('Table',YEAR([Date])=2021 && [Client] = __Client)
VAR __Table2022 = FILTER('Table',YEAR([Date])=2022 && [Client] = __Client)
RETURN
IF(ISBLANK(__Table2022),SUMX(__Table2021,[Amount]), SUMX(__Table2022,[Amount]))``````

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...
Frequent Visitor

Thanks for this but I get the error "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value".

Super User

``````Measure =
VAR __Client = MAX('Table'[Client])
VAR __Table2021 = FILTER('Table',YEAR([Date])=2021 && [Client] = __Client)
VAR __Table2022 = FILTER('Table',YEAR([Date])=2022 && [Client] = __Client)
RETURN
IF(COUNTROWS(__Table2022) + 0 = 0,SUMX(__Table2021,[Amount]), SUMX(__Table2022,[Amount]))``````

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...
Frequent Visitor

@Greg_Deckler Thanks for this, it's working now at client level but the totals are not correct. If I want to summarise at a website level, the numbers don't match

Super User

Also: This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Announcements

#### CollabDays Lisbon - 26 November 2022

Sessions include practical, hands-on experience that will help you take the next step in your career and know-how.

#### Difinity Conference 2022

Difinity is the largest Microsoft Data, AI, Power BI, Power Platform and Business Applications Conference in New Zealand.

#### European Share Point Conference

This conference returns live and in-person in Copenhagen this 28 November-01 December with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

#### Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Top Solution Authors
Top Kudoed Authors