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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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