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
Atifz
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

 

WebsiteClientProductDateAmount
WebAClientAProductA01/01/2021            85
WebBClientAProductB01/01/2021            17
WebBClientBProductC01/01/2021            82
WebAClientCProductD01/01/2021            65
WebAClientAProductE01/01/2022            69
1 ACCEPTED SOLUTION
v-yadongf-msft
Community Support
Community Support

Hi @Atifz ,

 

This is my test table:

vyadongfmsft_0-1665479199279.png

 

Please ccreate two measures:

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.

vyadongfmsft_1-1665479290763.png

Best regards,

Yadong Fang

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

6 REPLIES 6
v-yadongf-msft
Community Support
Community Support

Hi @Atifz ,

 

This is my test table:

vyadongfmsft_0-1665479199279.png

 

Please ccreate two measures:

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.

vyadongfmsft_1-1665479290763.png

Best regards,

Yadong Fang

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

 

 

Greg_Deckler
Super User
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]))

@ 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...

@Greg_Deckler 

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

@Atifz My bad:

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]))

@ 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...

@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

@Atifz See this: Matrix Measure Total Triple Threat Rock & Roll - Microsoft Power BI Community

 

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


@ 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...

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.