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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Phuonghm22
Frequent Visitor

Generate values for previous row and current one in a calcalated column

Hi guys,
I have a problem with this situation.

I have column A and I want to create column B using DAX by this logic: If row n in column A have value equal or less than 2, then values of row n and (n-1) (current and previous rows of calculated columns) of columns B will be 1.

Plz help me. Thank you so much.Screenshot_2.png

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your datamodel looks like, but I assume your model has a kind of order column (or Date_column) for ordering [Column A], like the below example.

Please check the below picture and the attached pbix file whether it suits your requirement.

 

Jihwan_Kim_0-1667187814415.png

 

Column B CC =
VAR _currentColumnA = Data[ColumnA]
VAR _nextColumnA =
    MAXX (
        FILTER ( Data, Data[ColumnA_order] = EARLIER ( Data[ColumnA_order] ) + 1 ),
        Data[ColumnA]
    )
RETURN
    SWITCH (
        TRUE (),
        _nextColumnA <= 2
            && _nextColumnA > 0, 1,
        _currentColumnA <= 2, 1
    )

 

If you have other groups in the dataset, the group has to be considered in the DAX formula.

 

 

 

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your datamodel looks like, but I assume your model has a kind of order column (or Date_column) for ordering [Column A], like the below example.

Please check the below picture and the attached pbix file whether it suits your requirement.

 

Jihwan_Kim_0-1667187814415.png

 

Column B CC =
VAR _currentColumnA = Data[ColumnA]
VAR _nextColumnA =
    MAXX (
        FILTER ( Data, Data[ColumnA_order] = EARLIER ( Data[ColumnA_order] ) + 1 ),
        Data[ColumnA]
    )
RETURN
    SWITCH (
        TRUE (),
        _nextColumnA <= 2
            && _nextColumnA > 0, 1,
        _currentColumnA <= 2, 1
    )

 

If you have other groups in the dataset, the group has to be considered in the DAX formula.

 

 

 

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Thank you so much for your reply.

Yes, it does have a date column (sorry for not bring in entirely context).

But I wonder if there is any solution that we don't have to use VAR, because my dataset is kind of big, and if I use VAR and FILTER at the same time, it works so long that I could not see the result.

Thank you and wait for your reply.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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