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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
maricci
Regular Visitor

Last year for non-existing date

I have a table with 10.000 rows that looks like:

 

DateNumb   Amount now   Amount previous year
31. 12. 2023.  88       250               200
31. 12. 2023.  75       150               105
31. 12. 2022.  88       200               356
31. 12. 2022.  75       105               253
31. 12. 2022.  32       300               253

 

For a certain date with a certain number, we need the amount for the current and previous year, but the problem arises when we have a certain date with a certain number in the last year, but not in the current year. We need that date in the new year, 0 for the current amount and the past amount to withdraw from last year. Zero padding is not possible, because the table is very large.

 

DateNumb   Amount now   Amount previous year
31. 12. 2023.  88        250                200
31. 12. 2023.  75        150                105
31. 12. 2023.  32          0                300
31. 12. 2022.  88        200                356
31. 12. 2022.  75        105                253
31. 12. 2022.  32        300                253
1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@maricci 

With the provided limited scope of the data, I assume this solution should work or provide an idea atleast.
Create a new table with this code:

New TAble = 
ADDCOLUMNS(
	CROSSJOIN( ALLNOBLANKROW( Table04[Date] ) , ALLNOBLANKROW( Table04[Numb] ) ),
	"@CurrentAmount" , 
	CALCULATE( SUM(Table04[Amount now] ) ),	
	
	"@PreviousYearAmount" , 
	VAR __Date = Table04[Date]
	VAR __Numb = Table04[Numb]
	RETURN
		MAXX(
		 	FILTER( table04 , Table04[Date] = EDATE( __Date , -12 ) && Table04[Numb] = __Numb ),
		Table04[Amount now] )
)

Fowmy_0-1703677127671.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

3 REPLIES 3
sjoerdvn
Super User
Super User

Hi, 
A more typical aproach in Power BI would be to skip that "Amount previous year" column altogether and use "current" and "previous year"measures. You really don't need to add extra rows. Check out time intelligence functions like PREVIOUSYEAR for this. Easiest is to leave the current value blank if there's only last year, but it is easy to tweak it to showing zero's.

Fowmy
Super User
Super User

@maricci 

With the provided limited scope of the data, I assume this solution should work or provide an idea atleast.
Create a new table with this code:

New TAble = 
ADDCOLUMNS(
	CROSSJOIN( ALLNOBLANKROW( Table04[Date] ) , ALLNOBLANKROW( Table04[Numb] ) ),
	"@CurrentAmount" , 
	CALCULATE( SUM(Table04[Amount now] ) ),	
	
	"@PreviousYearAmount" , 
	VAR __Date = Table04[Date]
	VAR __Numb = Table04[Numb]
	RETURN
		MAXX(
		 	FILTER( table04 , Table04[Date] = EDATE( __Date , -12 ) && Table04[Numb] = __Numb ),
		Table04[Amount now] )
)

Fowmy_0-1703677127671.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

It works somewhat, but I have a problem, it doesn't pull the data if I don't have any data for that date, it won't create it, to show the previous year, like for example. in the example 31.12.2018. numb 64. It should also pull the date for me only if it has the previous year, it pulls a lot of blank lines here. As for example 31.12.2021. numb 64, the previous year appears in 2022, which is okay, but then a blank line appears in 2023 as well. Thank you.

 

Created Table

DateNumb@CurrentAmount@PreviousYearAmount

31.12.2023.75150105
31.12.2023. 64  
31.12.2023. 88250200
31.12.2023. 53  
31.12.2023. 32 300
31.12.2022. 64 101
31.12.2022. 88200356
31.12.2022. 75105253
31.12.2022. 53  
31.12.2022. 32300 
31.12.2021. 88356406
31.12.2021. 75253233
31.12.2021. 64101 
31.12.2021. 53  
31.12.2021. 32  
31.12.2020. 88406 
31.12.2020. 75233 
31.12.2020. 64  
31.12.2020. 53  
31.12.2020. 32  
31.10.2019. 88  
31.10.2019. 75  
31.10.2019. 64  
31.10.2019. 53233 
31.10.2019. 32  
31.12.2018. 88  
31.12.2018. 75  
31.12.2018. 64101 
31.12.2018. 53  
31.12.2018. 32  

Table with data

Date NumbAmount nowAmount last year

nedelja, 31. decembar 2023.88250200
nedelja, 31. decembar 2023.75150105
subota, 31. decembar 2022.88200356
subota, 31. decembar 2022.75105253
subota, 31. decembar 2022.32300253
petak, 31. decembar 2021.88356406
petak, 31. decembar 2021.64101102
petak, 31. decembar 2021.75253233
četvrtak, 31. decembar 2020.88406456
četvrtak, 31. decembar 2020.75233333
četvrtak, 31. oktobar 2019.53233333
ponedeljak, 31. decembar 2018.64101102

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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