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
Commons94
Frequent Visitor

How to reference previous values from the same column where the DAX program/code is being written?

Dear Community,

 

I have a problem facing me since the past few months. I have multiple conditions (mentioned below) to develop 2 result columns - "Result Date" & "Result Qty". The problem I'm facing is that in some of the conditions, the result has to be the the same result as in the previous rows of the same column where the DAX code is being written. I'm currently performing it in Excel that's why I'm using "XLOOKUP" function. If you take a closer at the XLOOKUP formula, you can see that the result part of the formula references the "Result Date"/"Result Qty" columns which are the columns where the results are printed.

Any suggestions on how this could be done with Power BI would be helpful, as the dataset is large and performing these calculations in Excel doesn't make much sense now.

Thanks in advance!

 

Conditions for the 1st results column - "Result Date"

 

Result Date:

  • IF "Previous ID" =""
    • Then ""
  • IF "SL Type" = "First SL"
    • Then "Date"
  • IF "SL Type" = "Unconfirmed"
    • Then XLOOKUP("Previous ID", "Current ID", "Result Date","")
  • IF "SL Type" = "Confirmed" && "Check">1
    • Then "Date"
  • IF "SL Type" = "Confirmed" && Check =1 &&
    • Then "Date"
    • Then XLOOKUP("Previous ID", "Current ID", "Result Date","")
    • IF "Change Required?" = "Change date" || "Change Required" = "Change both"
    • IF "Change Required?" = "Change Qty" || "Change Required" = "no"
  • Else ""

Conditions for the 2nd results column - "Result Qty"

 

Result Qty:

  • IF "Previous ID" =""
    • Then ""
  • IF "SL Type" = "First SL"
    • Then "Qty"
  • IF "SL Type" = "Unconfirmed"
    • Then XLOOKUP("Previous ID", "Current ID", "Result Qty","")
  • IF "SL Type" = "Confirmed" && "Check">1
    • Then "Qty"
  • IF "SL Type" = "Confirmed" && Check =1 &&
    • IF "Change Required?" = "Change Qty" && "Qty decrease?" = "yes" 
      • Then "Qty"
    • IF "Change Required?" = "Change Qty" && "Qty increase?" = "yes"
      • Then XLOOKUP("Previous ID", "Current ID", "Result Qty","")
    • IF "Change Required?" = "Change both" && "Qty decrease?" = "yes" 
      • Then "Qty"
    • IF "Change Required?" = "Change both" && "Qty increase?" = "yes"
      • Then XLOOKUP("Previous ID", "Current ID", "Result Qty","")
    • IF "Change Required?" = "Change Date" || "Change Required" = "no"
      • Then XLOOKUP("Previous ID", "Current ID", "Result Qty","")
  • Else ""
6 REPLIES 6
Commons94
Frequent Visitor

Hi @Anonymous 

Thanks once again for your reply. below you find the expected results, in the "Comment" column you find the reason why these are the expected results. Please take a closer on the ID "5337836_10_037/13", here with the existing formula gives a blank because the lookup value of previous ID is also blank (******/13 has been confirmed 1st time in the sequence that's why).

Anonymous
Not applicable

Hi @Commons94 ,

I updated the sample pbix file, please check if that is what you want. Any comment or problem, please feel free to let me know.

vyiruanmsft_0-1700557521987.png

Best Regards

Commons94
Frequent Visitor

Hello @Anonymous ,

 

thanks a lot for your reply and dax-program. It seems to work correctly for the given sample dataset but when I apply it to the complete dataset I don't get the some results where "SL Type" = "Confirmed". Can you please provide your suggestion on how the code can be altered for the following sample dataset? thanks a lot... 

Anonymous
Not applicable

Hi @Commons94 ,

Could you please provide your expected [Result Date] and [Result Qty] base on above sample data when "SL Type" = "Confirmed"? Thank you.

Best Regards

Commons94
Frequent Visitor

 Sample

Anonymous
Not applicable

Hi @Commons94 ,

I create a sample pbix file(see the attachment), please check if that is what you want.

1. Create two calculated columns as below to get the qty and date for the previous ID

Previous ID Date = 
VAR _pid = 'Table'[Previous ID]
VAR _rdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( 'Table', 'Table'[Current ID] = _pid )
    )
RETURN
    _rdate
Previous ID Qty = 
VAR _pid = 'Table'[Previous ID]
VAR _rQty =
    CALCULATE (
        MAX ( 'Table'[Qty] ),
        FILTER ( 'Table', 'Table'[Current ID] = _pid )
    )
RETURN
    _rQty

2. Create two calculated column as below to get the result data and result qty

Result Date = 
IF (
    ISBLANK ( [Previous ID] ),
    BLANK (),
    SWITCH (
        TRUE (),
        [SL Type] = "First SL", [Date],
        [SL Type] = "Unconfirmed", [Previous ID Date],
        [SL Type] = "Confirmed"
            && [Check] > 1, [Date],
        [SL Type] = "Confirmed"
            && [Check] = 1
            && [Change Required?] IN { "Change date", "Change both" }, [Date],
        [SL Type] = "Confirmed"
            && [Check] = 1
            && [Change Required?] IN { "Change Qty", "no" }, [Previous ID Date]
    )
)
Result Qty = 
IF (
    ISBLANK ( [Previous ID] ),
    BLANK (),
    SWITCH (
        TRUE (),
        [SL Type] = "First SL", [Qty],
        [SL Type] = "Unconfirmed", [Previous ID Qty],
        [SL Type] = "Confirmed"
            && [Check] > 1, [Qty],
        [SL Type] = "Confirmed"
            && [Check] = 1
            && [Change Required?]
            IN { "Change Qty", "Change both" }
                && 'Table'[Qty decrease?] = "yes", [Qty],
        [SL Type] = "Confirmed"
            && [Check] = 1
            && ( ( [Change Required?]
            IN { "Change Qty", "Change both" }
                && 'Table'[Qty Increase?] = "yes" )
            || 'Table'[Change Required?] IN { "Change Date", "no" } ), [Previous ID Qty]
    )
)

vyiruanmsft_0-1700448890979.png

Best Regards

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.