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
Birinder
Helper III
Helper III

How to add previous row value in current row value while having 2 categorical variables ?

Hi Community,
I have a dataset table named as "IBNS".
Under it, I have 3 columns as follow:-
1. KEY (Data type - text)
2. Date (Data type - Short Date)
3. RRT - (Data type - Decimals)
 
I want to add the values of second row into the first one, and I want that for each category.
For example: Add the 12/31/2024 date data to 12/31/2023 date for one category and same like that for othe category as well.

You can get the idea of what I want from the Screenshot attached.

Birinder_0-1684821389267.png

I have tried the code below, But it is not working.

PrevSUM = CALCULATE(SUM(IBNS[RRT]),IBNS[KEY]=EARLIER([KEY]),DATESINPERIOD(IBNS[Date],SELECTEDVALUE(IBNS[Date]),2,DAY))

Can anyone help me here.
Many thanks in Advance.



2 ACCEPTED SOLUTIONS
Ahmedx
Super User
Super User

Hi @Birinder

Sure, please try this CC:

barritown_0-1684916037600.png

And the same in plain text:

PrevSum v2 = 
VAR CurrentKey = [KEY]
VAR CurrentDate =  [Date]
VAR PrevYear = CALCULATE ( YEAR ( MAX ( data[Date] ) ), ALL ( data ), data[KEY] = CurrentKey, data[Date] < CurrentDate )
VAR PrevRRT = MINX ( FILTER ( ALL ( data ), AND ( [KEY] = CurrentKey, YEAR ( [Date] ) = PrevYear ) ), [RRT] )  
RETURN IF ( ISBLANK ( PrevRRT ), 0, [RRT] + PrevRRT )

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

View solution in original post

5 REPLIES 5
barritown
Super User
Super User

Hello @Birinder,

I'd solve your task with the following calculated column:

barritown_0-1684825400782.png

Here's the [DAX] code in plain text:

PrevSum = 
VAR CurrentKey = [KEY]
VAR PrevYear = YEAR ( [Date] ) - 1
VAR PrevRRT = MINX ( FILTER ( ALL ( data ), AND ( [KEY] = CurrentKey, YEAR ( [Date] ) = PrevYear ) ), [RRT] )  
RETURN IF ( ISBLANK ( PrevRRT ), 0, [RRT] + PrevRRT )

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

 

Hi @barritown,
Thanks for the reply.
The code works almost perfectly, however there is still one issue.
The code will work when we have continous series of years i,e 2020,2021,2022 and so on.
There are some cases where there are year skips such as 2020,2022,2023,2026.
In those cases the results are not coming up right and they are zero.

Birinder_0-1684907550212.png
Please ignore the AMT column.

Is there a way only for PrevYear varible , via which we can choose earlier period than the current one.
Like in screenshot, for second last row:
12/31/2045 date should refer to its very previous date i.e. 12/31/2043, which is picking 12/31/2044 as of current solution and giving the values as zero.
Do you know a way by which we can achieve this.
Many thanks in Advance.

Hi @Birinder

Sure, please try this CC:

barritown_0-1684916037600.png

And the same in plain text:

PrevSum v2 = 
VAR CurrentKey = [KEY]
VAR CurrentDate =  [Date]
VAR PrevYear = CALCULATE ( YEAR ( MAX ( data[Date] ) ), ALL ( data ), data[KEY] = CurrentKey, data[Date] < CurrentDate )
VAR PrevRRT = MINX ( FILTER ( ALL ( data ), AND ( [KEY] = CurrentKey, YEAR ( [Date] ) = PrevYear ) ), [RRT] )  
RETURN IF ( ISBLANK ( PrevRRT ), 0, [RRT] + PrevRRT )

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

Thanks @barritown 
It is working now.
Thanks a lot for this.
Also ,Thanks to @Ahmedx for the soluton.
Peace.

Ahmedx
Super User
Super User

pls try this

Sample PBIX file attached

https://1drv.ms/u/s!AiUZ0Ws7G26RiDCBBmeuF22LQvUS?e=UxGIAa

Screen Capture #1156.pngScreen Capture #1157.pngScreen Capture #1159.png

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.