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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
scorbin-j
Frequent Visitor

How to create a new column with the previous row value by group?

I am trying to create a column that displays the previous rows data, but is grouped by both a category column and date column. Right now I have something like this: 

scorbinj_0-1674665035625.png

 

I would like to see results like this:

scorbinj_2-1674665095950.png

 

So the amount from the earlier line based on the area and date appears in the new column. Note that the date is not consecutive as the data is collected at the month end. Is there a way I can accomplish this using DAX? 

1 ACCEPTED SOLUTION
v-zhangti
Community Support
Community Support

Hi, @scorbin-j 

 

You can try the following methods.

Previous = 
Var _Prevdate=MAXX(FILTER('Table',[Date]<EARLIER('Table'[Date])&&[Area]=EARLIER('Table'[Area])),[Date])
Return
CALCULATE(SUM('Table'[Amount]),FILTER('Table',[Date]=_Prevdate&&[Area]=EARLIER('Table'[Area])))

vzhangti_0-1674810499050.png

Is this the result you expect?

Best Regards,

Community Support Team _Charlotte

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

7 REPLIES 7
v-zhangti
Community Support
Community Support

Hi, @scorbin-j 

 

You can try the following methods.

Previous = 
Var _Prevdate=MAXX(FILTER('Table',[Date]<EARLIER('Table'[Date])&&[Area]=EARLIER('Table'[Area])),[Date])
Return
CALCULATE(SUM('Table'[Amount]),FILTER('Table',[Date]=_Prevdate&&[Area]=EARLIER('Table'[Area])))

vzhangti_0-1674810499050.png

Is this the result you expect?

Best Regards,

Community Support Team _Charlotte

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

That seems to be working! Thank you so much 

scorbin-j
Frequent Visitor

So I have a partial solution. I created an index column like this: 

IndexColumn = 
RANKX(
ALL(Amounts),
Amounts[Date],,
ASC,
Dense
)
 
Then I created a this measure: 
PrevRowEmpCount = 
CALCULATE(
'Measure'[SUMAmounts],
TOPN(
1,
FILTER(
ALLSELECTED('Amounts'),
Amounts[Date] < MAX(Amounts[Date])
),
Amounts[Date],
DESC
)
)
This works well when looking at the total Amountsover time in a table, and I can filter by the Area. However when I add the Area to a table, the measure does not work. I think it needs to be a column as well but I'm not sure how to do that. 

OK.  I see the problem now.  I'm not sure how to implement it as a column.

Good luck.

grantsamborn
Solution Sage
Solution Sage

Hi @scorbin-j 

Try this

 

Previous Amount = 
VAR _Area = SELECTEDVALUE( 'Area Amounts'[Area] )
VAR _CurrDt = SELECTEDVALUE( 'Area Amounts'[Date] )
VAR _PrevDt =
    CALCULATE(
        MAX( 'Area Amounts'[Date] ),
        FILTER(
            ALL( 'Area Amounts' ),
            'Area Amounts'[Area] = _Area
                && 'Area Amounts'[Date] < _CurrDt
        )
    )
VAR _PrevAmt =
    CALCULATE(
        SUM( 'Area Amounts'[Amount] ),
        FILTER(
            ALL( 'Area Amounts' ),
            'Area Amounts'[Area] = _Area
                && 'Area Amounts'[Date] = _PrevDt
        )
    )
RETURN
    _PrevAmt

 

 

Tried that out and right now that is just outputting blank values in the new column it created. 

Oops.  My mistake.  I built that as a measure.

 

If you're interested...

https://1drv.ms/u/s!AnF6rI36HAVkhPIouUq80dpJUlOV5w?e=XUbYYb

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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