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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
scorbin-j
Helper I
Helper I

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
Helper I
Helper I

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
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.